TL;DR — prompt is too long: N tokens > M maximum is Anthropic's context-window rejection, and it counts everything: system prompt, full message history, tool definitions, tool results, and cached blocks. It is deterministic, so retrying the same request fails identically. In coding agents it arrives mid-session because tool results accumulate on every step. Fix by compacting history before you hit the ceiling, trimming tool output at the source, or moving the session to a larger-window model — not by retrying.
The error
HTTP 400
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "prompt is too long: 213418 tokens > 200000 maximum"
}
}
The two numbers make this the friendliest context error of the major providers — you can see exactly how far over you are, which tells you how much you need to cut.
What actually counts toward the number
People underestimate the total because they only think about the visible conversation. The prompt budget includes:
- the system prompt (often large in agent frameworks, and re-sent every request)
- every message in the replayed history, user and assistant
- tool/function definitions — a big toolset can be thousands of tokens on every call
- tool results — the real culprit in agents: file reads, command output, search results
- images, which cost tokens proportional to their dimensions
- cached blocks still count toward the window even when cache reads are cheaper
And separately, your requested max_tokens for the reply must fit alongside all of that.
Why coding agents hit it suddenly
An agent replays the whole conversation on every step, and each step adds to it — a file read here, a 400-line diff there. Context grows monotonically until it crosses the line, which is why the failure lands deep into a productive session rather than at the start. The same dynamic in OpenAI's dialect is context_length_exceeded.
Five fixes, ranked
1. Compact before you hit the wall. Summarize or drop old tool results — a file you read 30 steps ago and already edited rarely needs to stay verbatim. Do this proactively at roughly 80% of the window; in Claude Code, /compact does it on demand and auto-compaction handles it when configured.
2. Trim tool output at the source. This is the highest-leverage fix and the most overlooked. Cap what tools return: read line ranges instead of whole files, head long command output, paginate search results. A tool that can dump 50k tokens will eventually dump 50k tokens.
3. Slim the system prompt and tool definitions. These are re-sent on every request, so bloat here is paid repeatedly. Trim verbose tool descriptions and drop tools the session doesn't need.
4. Lower max_tokens. A large completion budget reserves window you rarely use. Size it to the longest reply you actually need.
5. Move to a larger-window model when the task genuinely needs it. Windows differ across models and change between releases — check current figures rather than folklore. Routing only the long-context steps to a big-window model is cheaper than running everything there: phase-aware routing explained.
What not to do
- Don't retry unchanged. Same input, same rejection — a backoff loop just burns time.
- Don't cut arbitrary messages from the middle. Removing an assistant message with a
tool_useblock while leaving itstool_result(or vice versa) produces a different 400 about mismatched tool blocks. Compact in whole turns. - Don't assume caching saves you. Prompt caching reduces cost and latency; cached content still occupies the window.
Prevention
- Log
usage.input_tokensfrom every response and alert at a threshold — overflow is visible many steps before it happens. - Set the agent's auto-compact threshold below the window, leaving room for
max_tokens. - Treat this as terminal in retry logic: compact or fail loudly; never spin.
Part of the LLM API Error Reference — errors indexed by their exact strings.