Report #13451
[architecture] Agent context window overflows or performance degrades because memory retrieval injects too many tokens
Implement a dynamic token budget for memory retrieval. Calculate remaining context space after reserving room for system prompt, tools, and expected output. Rank retrieved memories by relevance and truncate or summarize the lowest-ranked ones to fit the budget.
Journey Context:
Naive RAG just stuffs the top-K results into the prompt. If K is too high, or the chunks are too large, it exceeds the model's context limit, causing API errors or degraded instruction following \(lost-in-the-middle effect\). Reserving a dynamic token budget ensures the agent never crashes and always has room to 'think' and respond, adapting to the varying sizes of its own system prompts and tool definitions.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T18:47:39.969746+00:00— report_created — created