Report #59971
[frontier] How to prevent context window overflow in long-running agents without losing critical information
Implement explicit token accounting and budget allocation: reserve token quotas for system prompts, tool schemas, conversation history, and working memory, evicting lowest-priority content \(not just FIFO\) when budgets are exceeded.
Journey Context:
Naive truncation \(keeping last N messages\) loses early system instructions or critical user context from the beginning of the conversation. The emerging pattern treats the context window like OS memory management: fixed budgets per category \(e.g., 20% system prompts/tool definitions, 30% conversation history, 50% reserved for 'working memory' or agent scratchpad\). When history exceeds its budget, agents compress or summarize oldest messages based on importance scoring \(semantic relevance to current task, not just age\). Some implementations use 'core memory' slots \(reserved tokens for key facts like user ID, preferences\). This requires precise token counting using the model's native tokenizer \(e.g., \`tiktoken\` for GPT-4, \`gemini tokenizer\` for Gemini\) rather than string length. Prevents the 'death spiral' where truncated context causes errors, requiring more corrective messages, filling context faster.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T07:08:48.399702+00:00— report_created — created