Anthropic — context window exceeded error
Anthropic returns an error because the total input tokens plus requested max_tokens exceed the model context window, requiring truncation or a larger model.
Symptoms
- error: input length plus max_tokens exceeds model context window
- Long conversations fail but short ones succeed
- The error occurs after adding a new message to the history
Possible causes
- The conversation history is too long for the model context window
- max_tokens is set too high, leaving no room for the input
- The system prompt plus messages plus max_tokens exceed the limit
Diagnosis steps
Count the input tokens
echo $PROMPT | anthropic token-count --model claude-3-5-sonnetInterpretation
Compare the token count against the model context window (200K for Sonnet).
Check max_tokens setting
grep -n "max_tokens" src/llm.tsInterpretation
Ensure max_tokens plus input tokens does not exceed the window.
Root cause
The total of input tokens and max_tokens exceeds the model maximum context window, so the API rejects the request before processing.
Fix
Truncate or summarize older messages, reduce max_tokens, use a model with a larger context window, or implement sliding window conversation management.
Verify the fix
Retry the request with a shorter input or smaller max_tokens; confirm the model responds successfully.
Symptom walkthrough
Anthropic returns an error because the total input tokens plus requested max_tokens exceed the model context window, requiring truncation or a larger model. The error appears in production but may not surface in local development because the conditions that trigger it depend on the deployment environment or runtime configuration. Understanding the full symptom set helps distinguish this issue from similar errors.
Diagnosis in practice
Follow the diagnosis steps in the frontmatter to isolate the cause. Each step includes a command and an interpretation of the output. The goal is to narrow down whether the issue is in the configuration, the runtime, or the application code. Reproduce the error in a minimal setup before applying a fix.
Root cause
The total of input tokens and max_tokens exceeds the model maximum context window, so the API rejects the request before processing.
Fix
Truncate or summarize older messages, reduce max_tokens, use a model with a larger context window, or implement sliding window conversation management. After applying the fix, monitor the system to confirm the error does not recur under load or in different environments.
Verification
Retry the request with a shorter input or smaller max_tokens; confirm the model responds successfully. If the verification passes, the fix is confirmed; if not, re-examine the diagnosis steps for missed contributing factors.
Diagnostic commands
Run these commands to reproduce and diagnose the issue:
echo $PROMPT | anthropic token-count --model claude-3-5-sonnet
grep -n "max_tokens" src/llm.ts