cocodot
← Back to guides
Local card declined, direct access hard? cocodot does both
TroubleshootingUpdated 2026-09

context_length_exceeded: Four Fixes, in the Order That Saves You Money

The context limit counts input plus the output you asked for. Work out which one blew the budget before you reach for a bigger model — getting the order wrong is expensive.

TL;DR: The error means **input tokens plus the output you requested** exceeded the model's context window — note that it is the sum. So a modest prompt with an oversized `max_tokens` triggers it just as easily as a huge prompt. Work through it in this order: **(1) measure** what you actually sent (read `usage.prompt_tokens`, do not estimate); **(2) trim** what should not have been sent — early conversation turns, repeated system prompts, whole documents where you needed one section; **(3) split** long work into segments and combine the results, which is usually cheaper than a bigger model; **(4) only then** move to a longer-context model. **The order matters: reaching for a bigger window first is the most expensive fix and often treats the symptom — context keeps growing and you hit the wall again next week.**

Measure before you change anything

Most people hitting this error do not know how many tokens they actually sent. Rough conversions (about 1.3 tokens per English word) are fine for order of magnitude, but to fix it read `usage.prompt_tokens` from the response, or run a tokenizer locally. **Without that number every subsequent step is guesswork**, and you will probably change the wrong thing.

Remember the output counts too

This is the most common misunderstanding. The window holds your input **plus** what the model is about to generate, so an oversized `max_tokens` eats the space directly. A request whose input is nowhere near the limit will still fail if `max_tokens` is set generously. Check that field first — sometimes dropping it from 4096 to 1024 fixes the error and lowers your bill at the same time.

Conversations: history cannot grow forever

Multi-turn chat sends the entire history by default, so turn fifty costs many times turn one and eventually hits the ceiling. Two options: a **sliding window** (keep the last N turns) is simplest; **summarising** older history preserves more at the cost of an extra call. Which to pick depends on how much early context matters to the current answer — for most support-style use cases a sliding window is enough.

Long documents: splitting almost always beats upgrading

Pushing a hundred-page document through in one call means paying for a great deal of text the answer never used, even when the model can technically hold it. Better to segment and combine, or retrieve the relevant passages and send only those. **Several small calls usually cost less than one enormous call**, and when something fails you re-run one segment rather than the lot.

When a longer window is genuinely the answer

After the first three, some cases remain — a model that must see an entire codebase to reason across it, where any split loses the connections that matter. That is real, and rarer than most people assume. The problem with upgrading first is that it raises your costs without changing the fact that context keeps growing, so you arrive back at the same wall with a larger bill.

Comparing windows across models quickly

If you need to compare how several models handle the same input, an OpenAI-compatible endpoint makes it a one-string change — no SDK swap, no second key. Context limits and per-token prices for each model are listed at cocodot.co/pricing#models, with a machine-readable version at `GET https://cocodot.co/api/ai/models`.

What each fix costs you

ApproachCost impactWhen to use it
Trim history and prompts**Lower**Always, and first
Split and combineSlightly higher (more calls)Long documents, batch work
Reduce max_tokensLowerWhen the output never needed to be that long
Move to a longer-context model**Significantly higher**Only after the first three

FAQ

Am I billed for a rejected request?

Generally not, since the model never processed it. But in a batch, the calls that already succeeded are billed normally — only the rejected one is free.

Is a bigger context window always better?

No. A larger window only raises the ceiling; you still pay per token actually sent, and very long contexts can make it harder for the model to focus on what matters. Big enough is the goal, not biggest.

About cocodot

cocodot is a payment and AI access service for developers and cross-border teams in mainland China. It provides US-BIN virtual cards issued by a licensed institution — used to pay for overseas subscriptions and ad accounts — and an OpenAI-compatible AI API gateway for calling Claude, GPT and Gemini from within mainland China. Both share one wallet, funded by Alipay and accounted in USD. Card: $9.9 to open, 3% to load, 0% on spend, $1 per active card per month.

Service scope, pricing and limits →
context_length_exceeded: Four Fixes, in the Order That Saves You Money · cocodot