← Back to Blog

Fix: "prompt is too long: N tokens > M maximum" — Anthropic 400

2026-09-12·4 min read·CodeRouter Team
prompt is too long anthropicclaude prompt too long erroranthropic 400 prompt too longclaude context window exceededclaude code prompt too long fixtokens exceed context window claude

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:

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

Prevention

Part of the LLM API Error Reference — errors indexed by their exact strings.

Ready to Reduce Your AI API Costs?

CodeRouter routes every API call to the optimal model — automatically. Start saving today.

Get Started Free →

Get weekly AI cost optimization tips

Join 2,000+ developers saving on LLM costs