Context Overflow Recovery
When the API returns a “context too long” error, ChatCLI applies up to 3 recovery levels before giving up:- Level 1: Aggressive Budget
- Level 2: Emergency Truncation
- Level 3: Nuclear Truncation
First attempt: halves the budget limits and cleans up misalignments.Actions:
- Repairs tool result pairing (removes orphans, injects synthetics)
- Reduces
DefaultTurnBudgetCharsandDefaultPerResultMaxCharsto 50% of their original values - Applies budget enforcement with reduced limits
- Truncates long assistant messages to 5,000 chars
The original limits are restored after application. Only the current history is affected by the reduction.
Error Detection
The system recognizes multiple forms of overflow errors:| Error Message | Provider |
|---|---|
context length exceeded | Anthropic |
prompt is too long | OpenAI |
request too large | Various |
max_tokens exceed | Various |
input too long | |
token limit | Generic |
Max Output Token Escalation
When the model stops generating because it hit themax_tokens limit, ChatCLI can automatically escalate:
| Attempt | Action |
|---|---|
| 1st | Doubles the current max_tokens (up to the provider’s cap) |
| 2nd | Doubles again (up to the provider’s cap) |
| 3rd+ | Stops escalating, returns partial content |
Continuation Message
When the model is interrupted by a token limit, ChatCLI injects a continuation message:Configuration
| Environment Variable | Description | Default |
|---|---|---|
CHATCLI_MAX_RECOVERY_ATTEMPTS | Maximum context recovery attempts | 3 |
CHATCLI_MAX_TOKEN_ESCALATIONS | Maximum max_tokens escalations | 2 |
CHATCLI_EMERGENCY_KEEP_MESSAGES | Messages kept in emergency truncation | 10 |
Aggressive Budget Ratio
At level 1, the tool result budget limits are multiplied by0.5 (50%). This means:
| Parameter | Normal | Level 1 Recovery |
|---|---|---|
| Budget per turn | 200,000 chars | 100,000 chars |
| Max per result | 20,000 chars | 10,000 chars |
Recovery Flow
Interaction with Other Systems
Context recovery works in conjunction with:Tool Result Budget
The result budget is the first line of defense. Recovery activates when the budget was not sufficient.
Microcompaction
Progressive compaction reduces context growth over time.
Conversation Control
The
/compact command is the proactive way to prevent overflow.Cost Tracking
Monitor context usage to anticipate when /compact will be needed.