ERROR CODES / ENGLISH
context_length_exceeded: the request exceeds the model window
Four ways to fix an oversized context: trim history, summarize documents, split requests, or move to a model with a larger window.
Reviewed 2026-11-13 · ERROR CODES / 400 · Chinese detail: 完整中文页
What it means
The total token count of this request exceeded the model's window. Input and output both count, and you also need to reserve max_tokens for the output.
{'error': {'message': "This model's maximum context length is 128000 tokens. However, your messages resulted in 135000 tokens.", 'type': 'invalid_request_error', 'code': 'context_length_exceeded'}}
{"type":"error","error":{"type":"invalid_request_error","message":"prompt is too long: 210000 tokens > 200000 maximum"}}
Common causes
| Share | Cause |
|---|---|
| 45% | History is not trimmed; the conversation grows with every turn |
| 30% | A long document or a large block of code was inserted |
| 15% | max_tokens is too large and squeezes the input space |
| 10% | The model window itself is small (8K/16K) |
How to confirm
Check the token count reported in the error. If the message states how many tokens you sent and what the maximum is, the overshoot is confirmed. If the content looks short and the error persists, test the same request on another site; some relays reduce the window.
Fix
- Trim history. Keep the most recent N turns plus the first message (the first is usually the most important). Drop or summarize the middle.
def trim(messages, keep_first=1, keep_last=10):
if len(messages) <= keep_first + keep_last:
return messages
return messages[:keep_first] + messages[-keep_last:]
- Summarize long documents first. Do not insert an entire book. Summarize in chunks, then feed the summaries to the main model.
- Switch to a model with a larger window.
| Model tier | Typical window |
|---|---|
| Small models (mini/flash) | 128K |
| Mid-range | 200K |
| Long-context models | 1M |
The model library on this site can be filtered by context size.
If it still fails
| Symptom | Responsible party |
|---|---|
| The error states by how many tokens you exceeded | Your input |
| Content is clearly not long, but it still errors | The site reduced the window (some relays shrink it) |
| Only one site returns the error | That site has a stricter limit |
One commonly overlooked point: many relays advertise a 200K window as "1M". To test it, send an overlong prompt and see where it starts to fail.
General guidance, not a statement about any specific provider. For measured data, see the relay directory and method.