API referenceError code index400context_length_exceeded

context_length_exceeded

The message text is shown in the example below.

HTTP status400
typeinvalid_request_error
Retry?No, not unchanged
StatusIn the public catalog (error-envelope.ts)
Extra fieldsnone

When it happens

The input tokens plus max_tokens exceed the model’s context window of 262,144 tokens.

Example

{
  "error": {
    "type": "invalid_request_error",
    "code": "context_length_exceeded",
    "message": "The request exceeds the model's maximum context length. Shorten the messages or lower max_tokens.",
    "param": null,
    "request_id": "req_6f1c2a9e0b7d4c3a8e5f1b2d"
  }
}

How to fix it

  • Count before you send with token counting and trim when you are close.
  • Lower max_tokens to what you need; the reservation counts against the window.
  • Summarise or drop old turns, or move large documents into files and quote only the relevant parts.
  • For agents, turn on context management.

Detect it in code

err = resp.json().get("error", {})
if resp.status_code == 400 and err.get("code") == "context_length_exceeded":
    log.warning("%s (%s)", err["message"], err["request_id"])  # fix the request; do not retry unchanged
Was this page helpful?