Skip to content

Agent context grows unbounded on long runs (no compaction) #161

Description

@lfnothias

Problem

On a multi-step reproduction run the execution agent's context grew to ~866k input tokens per step. The run crawled (1h15m+ without converging) and was expensive. There is currently no context management for the agent: smolagent_factory runs with max_steps=128 and no compaction or token budget, so a long run accumulates its full step history and re-sends it every step.

Root cause

The agent (smolagents) keeps every step in memory.steps and rebuilds the full message list on each step (standard ReAct). Over dozens of steps the live context grows roughly linearly, dominated by accumulated tool outputs and generated code. This is intra-run accumulation, distinct from the cross-run load_agent_memory replay (which only prepends one matched prior step).

Evidence

  • Haffner reproduction run: per-step input tokens climbed past 800k around step ~33; stopped at 1h15m without converging.

Why it matters

  • Cost: input tokens dominate the bill on long agentic runs.
  • Latency: every step carries the whole history.
  • Reliability: approaches model context limits.

Proposed direction (for discussion)

Add lightweight memory management / context compaction to the agent loop, inspired by coding agents (opencode / Claude Code): once the context crosses a threshold, summarize or drop the oldest steps while keeping recent steps verbatim plus a running summary, then continue. Start minimal (e.g. truncate old tool outputs first, then compact older steps), measure, iterate.

This is the alternative to the hard max_steps / context-window guard that was dropped in the #158 review: rather than capping or aborting, compact and continue.

Open questions

  • Hook point: a smolagents step callback vs a wrapper around memory.steps?
  • Strategy: deterministic truncation of old tool outputs first, then an LLM summary of older steps?
  • Threshold, and how many recent steps to keep verbatim?
  • Which model summarizes (cheap tier)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidThis doesn't seem right

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions