Search⌘ K
AI Features

Keep the Useful Context

Explore how to keep useful context for AI coding agents by implementing compaction contracts that measure context size, preserve essential messages, and manage working memory limits. Learn to prevent context overflow by dropping or summarizing older exchanges without losing important evidence, ensuring reliable agent behavior across sessions and tasks.

On CLIN-547, Wren spent twelve turns finding the booking rule, reading its tests, previewing a small edit, and explaining why the old check was wrong. Then it received a fresh user request: “Run the focused test and tell me whether the replacement is safe.”

The provider rejected the next request. The conversation had crossed its context limit.

The easy repair is to drop old messages until the call fits. That repair can make a worse mistake. If Wren drops the preview receipt, it can claim an edit was reviewed when it was not. If it drops a tool result while keeping the tool call, it invents an unresolved action. If it converts every old message into a cheerful paragraph, the paragraph can acquire facts the trace never contained.

This is a Carry failure. Wren has the evidence, but it has no rule for deciding what the next model call needs. Context compaction gives the active run a smaller working set. The append-only journal keeps the original evidence.

What are we building in this lesson?

Imagine a desk beside a filing cabinet:

  • The desk holds the papers needed for the current task. Space is limited.

  • The filing cabinet keeps the full record. It can grow without covering the desk.

  • A receipt on the desk says which older papers were filed away.

In Wren, the provider context is the desk, and the JSONL journal is the filing cabinet. Compaction clears older material from the desk. It does not destroy the filed record.

Follow one request through the design:

full journal -> measure working messages -> keep current exchange
-> replace older messages with a labelled receipt
-> send the smaller context to the model
Where compaction sits between journal and provider

The learner’s goal is not to write a brilliant summary. It is to know which record is exact, which record is shortened, and when shortening must stop.

Why count context before the provider fails?

A provider counts tokens using its own rules. It also adds tool schemas and reserves space for the answer. A small local harness cannot know that exact final number without copying provider-specific behavior.

It can still account for what it owns. Wren counts characters in each message, call payload, and tool identifier before each provider call. That number is a stable early-warning meter, not a billing statement. The policy leaves headroom below the provider limit so the harness can compact before a request is rejected.

The Compaction Contract answers four questions:

...