-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement context budget management in execution loops #416
Description
Summary
Add context window budget management to execution loops — soft budget indicators, compaction hooks at task boundaries, and compressed checkpoint recovery.
Motivation
Three independent research sources converge on the same need: agents must manage their context window actively during long-running tasks.
- Memex (arXiv:2603.04257): Soft budget indicators — append token usage status so agent self-manages compression timing
- LangChain Autonomous Context Compression: Agent-triggered compaction at clean task boundaries (conservative tuning)
- KV Cache Attention Matching (arXiv:2602.16284): Compress-on-fill pattern — 6 consecutive online compressions with no quality loss
Design
Soft Context Budget Indicators
Append [Context: 12,450/16,000 tokens | 3 archived blocks available] to agent context. Works with existing TurnRecord token counting. Agent self-manages compression timing at natural task boundaries.
Compaction Hook
Add a hook point in execution loops where the agent (or the loop itself) can trigger context summarization between turns. Detect context window fill level and optionally summarize older turns.
Compressed Checkpoint Recovery
When recovering from a checkpoint, inject compressed summary + index table from last compression point instead of replaying full conversation history (from Memex segmented trajectory pattern).
Affected Files
src/ai_company/engine/react_loop.pysrc/ai_company/engine/plan_execute_loop.pysrc/ai_company/engine/checkpoint/(recovery with compressed context)src/ai_company/engine/prompt.py(inject budget indicators)