Production best practices
Recommended configuration before sending production traffic: timeouts, retries, long documents, rate limits and balance monitoring.
Timeouts#
The response time of a humanize request increases with the length of the text. The API always responds within 170 seconds, either with a result or with a 504 timeout error, which is not charged.
These figures are measured response times for English text on the standard model. Other languages take longer. Detect requests typically complete in under one second.
- Set a client timeout of 180 seconds for humanize, 60 seconds for detect and 30 seconds for usage. With a shorter humanize timeout, a request that the client has abandoned can still complete and be charged. The SDK defaults are 120, 30 and 15 seconds and are configurable.
- Check platform limits. Serverless functions and proxies commonly terminate requests after 10 to 60 seconds. If your platform enforces such a limit, submit shorter text per request or make the call from a background job.
Retries#
The API indicates when a retry is safe. A 429, 503 or 504 response with a JSON code was not charged, and its Retry-After header gives the number of seconds to wait. All other failures require a change to the request or the account and should not be retried automatically. The SDKs implement this policy.
async function humanize(text, attempt = 0) { const resp = await fetch("https://api.writehuman.ai/v1/humanize", { method: "POST", headers: { "Authorization": `Bearer ${process.env.WRITEHUMAN_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify({ text }), signal: AbortSignal.timeout(180_000), }); const body = await resp.json().catch(() => null); if (resp.ok) return body; // Safe to retry: the API rejected the request, charged nothing, and specified a wait time. const wait = Number(resp.headers.get("Retry-After")); const retryable = [429, 503, 504].includes(resp.status) && body?.code; if (retryable && attempt < 2 && wait <= 60) { await new Promise((r) => setTimeout(r, wait * 1000)); return humanize(text, attempt + 1); } throw new Error(`${resp.status} ${body?.code ?? "no_code"}: ${body?.error ?? ""}`);}Do not automatically resend a humanize or detect request after a client timeout or a dropped connection. The API does not support idempotency keys, so if the first request completed, the resent request is charged again. To determine whether the first request was charged, call /v1/usage after approximately two minutes.
Long documents#
A request that exceeds the plan's words-per-request limit is rejected with 422 word_limit. The API does not split text automatically. Split long documents on the client side as follows:
- Split at paragraph boundaries, not within a sentence. Each segment is rewritten independently.
- Keep each segment below approximately 90% of the limit. The API word count can differ from a whitespace-based count, particularly for some languages.
- Send the segments sequentially, or in parallel within the requests-per-minute limit, and join the results using the original paragraph breaks.
// Groups paragraphs into segments of at most maxWords words.function splitText(text, maxWords = 1800) { const pieces = []; let current = []; let count = 0; for (const paragraph of text.split(/\n\s*\n/)) { const words = paragraph.trim().split(/\s+/).length; if (count + words > maxWords && current.length) { pieces.push(current.join("\n\n")); current = []; count = 0; } current.push(paragraph); count += words; } if (current.length) pieces.push(current.join("\n\n")); return pieces;}The example uses 1,800 words, which is appropriate for the Standard limit of 2,000. A single paragraph that exceeds the limit must be split further, at sentence boundaries.
Rate limits#
- The limit applies per account, not per key. It is shared by all keys and all teammates, and humanize and detect requests count toward the same limit.
- The limit resets at the start of each clock minute. A 429
rate_limitedresponse includes aRetry-Aftervalue of 1 to 60 seconds, which is the time remaining until the next minute. - Successful responses do not include rate-limit headers. To remain under the limit without relying on 429 responses, track the request count on the client side.
- There is no limit on concurrent requests. Under high load, a request can be queued briefly, and some requests return 429
queue_full, 429queue_timeoutor 503at_capacity. These responses are not charged and are safe to retry.
Balance monitoring#
- Every humanize response includes
words_remaining. Trackwords_remaining.totalin your monitoring. - A request that exceeds the balance is rejected with 402
insufficient_balance. The response is not charged and includes the current balance, which can be used to notify the user or to queue the request. - WriteHuman emails the account owner when 20% of the monthly words remain, and again when the balance is exhausted.
- On Premium plans, auto top-up can be enabled to purchase additional words automatically when the balance falls below a configured threshold.
Checklist#
- The API key is loaded from an environment variable or a secrets manager and is never exposed to a browser.
- Production and staging use separate keys, so each can be revoked independently.
- The humanize client timeout is 180 seconds, or the platform timeout is known and request size is limited accordingly.
- Only 429, 503 and 504 responses that include a
codeare retried, after theRetry-Afterinterval. - Text that exceeds the words-per-request limit is split at paragraph boundaries before submission.
- 402
insufficient_balanceresponses are handled, and the low-balance emails reach a monitored address. - Error handling is based on
code, not on the text oferror.