Context manager
The agent sends its whole history to the model on every call, so a long run sends more with each step: every tool call adds its result: a file it read, a page it fetched or a command’s output. The context manager keeps it down in two ways: it replaces old tool results with a placeholder once the model’s input grows past a limit, and it replaces old screenshots once the history holds too many.
Turning it on
Every setting has a default, and an agent without a context manager behaves as if it had one with every default: the image cap is on and the token limit is off.
| Setting | Default | What it is |
|---|---|---|
max_input_tokens (maxInputTokens) |
0 | The input the model reports, in tokens, past which old tool results are replaced. 0 turns truncation off. |
preserve_recent_turns (preserveRecentTurns) |
3 | How many of the latest user turns are left as they are. |
max_images (maxImages) |
30 | Images in tool results the history may hold at the start of a turn. 0 turns the cap off. |
preserve_recent_images (preserveRecentImages) |
10 | Images in tool results left once the cap is passed. |
agent = await ( AgentBuilder("anthropic/claude-haiku-5-5") .sandbox(sandbox) .context_manager(max_input_tokens=50_000, preserve_recent_turns=2) .build())const agent = await new AgentBuilder('anthropic/claude-haiku-5-5') .sandbox(sandbox) .contextManager({ maxInputTokens: 50000, preserveRecentTurns: 2 }) .build()use ailoy::agent::ContextManager;
let agent = AgentBuilder::new("anthropic/claude-haiku-5-5") .sandbox(sandbox) .context_manager(ContextManager { max_input_tokens: 50_000, preserve_recent_turns: 2, ..Default::default() }) .build() .await?;Tool results past the token limit
As each turn begins, once the user’s message is in the history, the agent looks at the last input token count the model reported. When that is more than max_input_tokens, it truncates the history:
- A turn is a user message and everything after it up to the next user message. The last
preserve_recent_turnsturns, including the one just begun, are left as they are. If the history has fewer turns than that, nothing changes.1and0both leave only the turn just begun. - In the turns before those, each tool result’s contents become the text
[context truncated]. The result keeps the id of the call it answers, since APIs such as Anthropic’s refuse a tool call that has no result. - The system message, the user’s messages and the model’s own messages, tool calls included, stay whole. No message is removed.
The placeholders replace the results in the agent’s history, so agent.history shows them too, and they stay in it.
The count comes from a call already made, and the history is truncated only as a turn begins. A turn that goes past the limit runs to its end with the history unchanged, and the next turn begins truncated. The requests of a turn send back the thinking the model has produced in it. From Claude 5.1 on, the API can refuse a thinking block once anything before it has changed (Keeping the prefix unchanged), so the history stays as it is until the turn ends. A reply without usage leaves the last count in place, so a model that never reports usage, such as a model of your own whose replies leave it out, never triggers truncation. And since only tool results are replaced, a history whose messages alone are past the limit keeps growing.
Images past the cap
At the start of each turn, when the tool results hold more than max_images images, the oldest become the text [image from an earlier turn removed] until preserve_recent_images remain.
- Only images in tool results count and only they are replaced: a screenshot, an image read with
imgread, an image an MCP tool returned. Images you attach to a message are never replaced. - Within a turn nothing is replaced, so a turn of many screenshots sends them all until the next turn starts.
- The cap counts across every turn, the last
preserve_recent_turnsincluded.
As with the token limit, the placeholders stay in agent.history.
Apart from the cap, and for every model, an embedded image past 2576 pixels on its longer edge, attached or returned by a tool, is shrunk to that edge before it enters the history. The shrunk image is turned upright by its EXIF orientation and keeps its color profile, and comes back as:
| Image | Comes back as |
|---|---|
| Any but a WebP | Its own format |
| A WebP with transparent pixels | PNG |
| An opaque lossless WebP | PNG, or JPEG if that PNG would pass 5 MB in base64 |
| Any other WebP | JPEG |
| One that would still come out past 5 MB in base64, an animated one, or a GIF of any size | Left as it is |
Next
- Model providers: naming a model and choosing how it runs.
- Tools: built-in tools, your own functions, MCP and the browser.