context_length_exceeded — the context window is full
The model refused because your request does not fit. What makes up the context window and what to cut first.
2 min · · errors · tokens · models
context_length_exceeded means your request plus the requested output does not fit in the model's context window. The request is rejected before generation — which is good news: a rejected request is not billed.
What counts toward the context
Everything that goes to the model in a single call.
The system prompt. In agents it often runs to thousands of tokens you do not think about, because you did not write it.
The entire conversation history. The model remembers nothing between calls — your client resends the history every time. That is why long conversations hit the limit abruptly: the last message was short, but the history pushed the total over the edge.
Tool definitions. JSON function schemas cost more than they look; a dozen tools easily reaches a couple of thousand tokens.
Attached files and tool results. In agent workloads this is usually the largest share.
And finally max_tokens — the space reserved for the answer. It is subtracted from the window up front. A 195,000-token request against a 200,000-token window will fail if you asked for max_tokens: 8000.
What to do, in order
Lower max_tokens. The cheapest move. It is often set generously to something like 64,000 when the answer needs two thousand. This frees the window instantly.
Trim the history. Keep the system prompt, the first few messages (they usually hold the task) and the last few. The middle of a conversation can usually go without loss.
Drop tool results. Command output an agent read five steps ago no longer needs to be in context. Many frameworks can clear old results automatically — look for that setting.
Summarise the history. Replace the first twenty messages with a paragraph of conclusions. More expensive than the options above — you pay for the summary too — but it works when the conversation is genuinely long.
Move to a model with a larger window. A last step, not a first one: if the context grows without bound, a bigger window only postpones the problem while raising the bill, since you pay for every input token.
Avoiding the error altogether
Count tokens before sending rather than after a refusal. A working rule of thumb: one token is roughly four Latin characters. Exact numbers come from a tokeniser, but approximations are enough to answer "will this fit".
And watch the shape of the growth. If your logs show the request size climbing at every agent step, you will meet the limit — the only question is which step.
zukko
Pay 25×
less
for Claude Code & ChatGPT
One fetch_* key and base URL api.zukko.pro — for Cursor, Claude Code, Codex, and any SDK.
Related articles
- OpenAI API Keys in the Open: Why You Should Not Use ThemWhere free keys on GitHub come from, why using them is dangerous for you and for the owner, and how to protect your own key from the same leak.
- GLM API Errors: Why the Request Fails and How to Fix ItA wrong model id, an overflowing 128K context, the key, the balance, limits: the typical causes of GLM API errors and a quick way to find yours.
- xAI Grok API: Connecting Grok 4.7, Price and the 256K ContextHow to call grok-4.7 through an OpenAI-compatible API, what to do about the 256K context limit and what typical requests cost. Plus a comparison with Grok 4.6.
- GPT-6 Astra or Claude Fable 5.1: Two Top Models by Price and IndexTwo premium models with the same intelligence index and different Zukko prices. We price a single request and an agent session with cache, and explain how to choose on your own data.