Every step adds tool calls and results to an agent's context. On long tasks the context fills up, costs rise and quality can drop. Managing it is a core harness job.
What Consumes Context
- System instructions and tool definitions.
- Conversation history.
- Tool results — often the largest part: file contents, search results, logs.
Techniques
- Truncate tool output: return summaries or the relevant portion, with a way to fetch more.
- Clear old results: once a tool result has been used, older verbose results can be removed or shortened.
- Compaction: summarise the history so far and continue from the summary when context gets full.
- External memory: write notes, plans and progress to files the agent can re-read.
- Sub-agents: delegate a sub-task to a separate agent with fresh context and receive back only its conclusion.
Just-in-Time Retrieval
Rather than loading everything up front, give the agent tools to find information when needed — search, list files, read a section.
Preserve What Matters
When compacting, keep the goal, key decisions, constraints, unresolved problems and current state. Losing these causes repeated work and errors.
Measure
Test long tasks specifically; context problems often only appear after many steps.