Anthropic 'prompt is too long' Error: Fix the Context Window Limit

Quick Fix

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

Anthropic context-window 400
{
"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.

The window holds input + reserved output together (illustrative, 200K model).
Context windowInput tokensmax_tokens reservedResult
200,000 198,0004,096❌ 202,096 > 200,000
200,000 198,0001,024✅ fits
200,000 120,0008,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_tokens set too high. A big output reservation eats window the input needed.
  • Big tool results. Returning large tool_result payloads 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:

Python — count tokens with the Anthropic API, then guard
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_tokens before building the prompt.
  • Cap and prune history every turn rather than waiting for the overflow.
  • Set max_tokens to 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.
Don't confuse this with a 429

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.

Need a bigger window without re-coding?

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

  1. Count input tokens and reserve room for max_tokens.
  2. Prune or summarize history so chat sessions stay under the window.
  3. Chunk large documents with map-reduce instead of one giant prompt.
  4. Verify the model’s real window and move up if you genuinely need more.

Frequently asked questions

Why does Claude say my prompt is too long when it seems to fit?
Because max_tokens is reserved against the window too. On a 200K model with 198K input, asking for 4,096 output tokens totals 202,096 and overflows. Lower max_tokens or trim the input.
Is 'prompt is too long' retryable?
No — it's an HTTP 400, a size error. Retrying the same request fails identically. Make it smaller (trim, summarize, chunk) or use a larger-context model.
How do I count tokens for Claude?
Use Anthropic's count_tokens endpoint (exposed in the SDK) for an exact input-token count, since Claude's tokenizer differs from GPT models. Compare it against the model's window minus your max_tokens reservation before sending.
Does max_tokens really count against the context window?
Yes. On the Messages API max_tokens is required and reserved up front, so the window must hold input plus that reservation. An inflated max_tokens wastes window you could have used for input.
How do I handle documents larger than Claude's window?
Chunk and map-reduce: split into pieces that fit, process each, then combine the partial outputs in a final call. This scales to inputs far larger than any single model's context window.