Skip to content

feat: implement context budget management in execution loops #416

@Aureliolo

Description

@Aureliolo

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.

  1. Memex (arXiv:2603.04257): Soft budget indicators — append token usage status so agent self-manages compression timing
  2. LangChain Autonomous Context Compression: Agent-triggered compaction at clean task boundaries (conservative tuning)
  3. 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.py
  • src/ai_company/engine/plan_execute_loop.py
  • src/ai_company/engine/checkpoint/ (recovery with compressed context)
  • src/ai_company/engine/prompt.py (inject budget indicators)

Research

Metadata

Metadata

Assignees

No one assigned

    Labels

    prio:mediumShould do, but not blockingscope:medium1-3 days of workspec:agent-systemDESIGN_SPEC Section 3 - Agent Systemspec:memoryDESIGN_SPEC Section 7 - Memory & Persistencespec:task-workflowDESIGN_SPEC Section 6 - Task & Workflow Enginetype:featureNew feature implementation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions