What this error means
A 400 context_length_exceeded fires when input tokens plus max_tokens exceed the model's context window.
How to fix it
- Trim or summarize history before sending
- Lower max_tokens to leave room for the prompt
- Move to a longer-context model for big inputs
Example error message
{
"error": {
"message": "This model's maximum context length is 128000 tokens.",
"type": "invalid_request_error",
"code": "context_length_exceeded"
}
}Frequently asked
Does max_tokens count toward the limit?
Yes. The window must fit prompt tokens plus the completion you reserve with max_tokens.