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.

Get API keyDocumentation

api.zukko.pro · fetch_*

context_length_exceeded: how to fit your request · Zukko