Claude returning prompt is too long: N tokens > M maximum? Make input + max_tokens fit the window:
- Count tokens before sending and trim the biggest input — usually conversation history or a large document. Use the token counter.
- Lower
max_tokens— Claude reserves it against the window, so an oversized value alone can overflow. - If the input is genuinely large, summarize, chunk (map-reduce), or pick a larger-context model.
Anthropic returns an invalid_request_error with a message like prompt is too long: 210000 tokens > 200000 maximum when your request exceeds the model’s context window. Like OpenAI’s context_length_exceeded, it arrives as an HTTP 400 — the request is too big, so retrying it unchanged always fails. You fix it by shrinking the request, not by backing off.
What this error means
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "prompt is too long: 210000 tokens > 200000 maximum"
}
} The crucial detail people miss: on the Messages API, max_tokens is required, and it’s reserved against the window before generation. The window holds your input plus the output you reserved. So input that fits on its own can still overflow once max_tokens is added.
| Context window | Input tokens | max_tokens reserved | Result |
|---|---|---|---|
| 200,000 | 198,000 | 4,096 | ❌ 202,096 > 200,000 |
| 200,000 | 198,000 | 1,024 | ✅ fits |
| 200,000 | 120,000 | 8,192 | ✅ fits |
Common causes
- Runaway conversation history. Appending every turn grows the prompt unbounded until it blows the window — the most common production cause.
- Large documents pasted whole. Summarizing or analyzing a long PDF, transcript, or codebase in one call.
- Oversized RAG context. Too many or too-large retrieved chunks stuffed into the prompt.
max_tokensset too high. A big output reservation eats window the input needed.- Big tool results. Returning large
tool_resultpayloads back into the conversation counts against the window too.
How to fix it
Count tokens before you send
Claude’s tokenizer differs from OpenAI’s, so don’t reuse exact GPT counts — but the discipline is identical: measure input and leave room for output. Anthropic’s SDK exposes a token-counting endpoint:
import anthropic
client = anthropic.Anthropic()
messages = [{"role": "user", "content": "..."}]
count = client.messages.count_tokens(
model="claude-sonnet-4-6",
messages=messages,
)
WINDOW = 200_000
RESERVE_FOR_OUTPUT = 4_096
if count.input_tokens > WINDOW - RESERVE_FOR_OUTPUT:
raise ValueError("Input too large — trim history or chunk the document.") Trim history with a sliding window
For chat apps, keep the system prompt plus the most recent turns that fit, and drop the oldest. Periodically replace dropped turns with a short model-generated summary so context stays bounded without losing the thread.
When the input is genuinely large
- Chunk + map-reduce. Split a long document, process each chunk, then combine the partial results in a final call — the right pattern for “analyze this whole book/repo.”
- Retrieve less. In RAG, lower top-k or shrink chunk size so only the most relevant context goes in.
- Use a larger-context model if you truly need more room. Confirm the exact window first with the context-window checker.
How to prevent it
- Budget the window as input + output, reserving space for
max_tokensbefore building the prompt. - Cap and prune history every turn rather than waiting for the overflow.
- Set
max_tokensto what you actually need, not a large default. - Pre-flight large requests through a token count and fail with a clear message instead of a raw 400.
A prompt is too long 400 is a size problem — retrying never helps. A 429 rate_limit_error is a rate problem — backoff does help. Branch on the error type; see the Anthropic 429 fix.
Related
- The OpenAI equivalent: context_length_exceeded.
- Size requests with the token counter and check model windows with the context-window checker.
If your workload keeps outgrowing one model’s window, routing to a larger-context model on demand is simplest behind a gateway. OpenRouter exposes many models and their context sizes through one API. (Affiliate link.)
What to do next
- Count input tokens and reserve room for
max_tokens. - Prune or summarize history so chat sessions stay under the window.
- Chunk large documents with map-reduce instead of one giant prompt.
- Verify the model’s real window and move up if you genuinely need more.