
Keeping a long agent run’s context in check
Five ways to keep a long run affordable and on task, and what each did on a 99-report batch.
On the agent compass Stage 1 of 4 · Build the requestA model has no memory between requests, so the harness sends the entire history each turn: the task, every crash report opened so far, every file read, every test output. Over a long run that history gets big. Money is the first thing it hurts; the model’s ability to pay attention to all of it comes later. Deciding what goes into each request is the first stage of the agent loop, and the design below turns that decision into Go code you can swap and test.
The problem, measured
One task in the crash catalog throws everything at the agent together: 99 reports from 23 different causes, the kind of pile a crash feed produces after a release goes badly. The default model ran it with no context policy at all. It needed 35 turns and sent 3.1 million input tokens across them, roughly 89,000 per request on average, for a total of $1.33.
Measured result
$0.23 down from $1.33, for the same work
Two cache markers per request (hints that tell the provider to cache the start of the request, explained under prompt caching below), and no other change, moved 95 percent of the input to the cache price. Part of the drop comes from the price tier described below, which both runs crossed; at the base price alone, the chapter puts the saving at about four and a half times.
- Task
- the crash catalog’s batch task, 99 crash reports from 23 causes, triaged by one agent with the prompt from the
ch08companion code - Models
- anthropic/claude-haiku-5.5, the book’s default model, through OpenRouter
- Runs
- one run with no context policy and one with cache markers; the provider’s billed cost
- Date
- October 2026
Measured with the companion code
Pricing explains part of that bill. For requests above 100,000 tokens, the default model’s price jumps to five times its normal rate, and close to half the requests in this run crossed that threshold. The context window was never the constraint: at a million tokens, it held far more than any one request in the run. So for an agent the order of trouble is money first; on longer runs the window size and how well the model attends to a very long request come next.
Where the tokens go
A request is made of five parts, and each grows at its own pace. The system prompt and the tool definitions are fixed. The model’s own messages and tool calls add a few hundred tokens per turn. Tool results arrive in big steps whenever the agent reads something. And many reasoning models return their reasoning with each tool call, often encrypted, which has to be sent back and grows as well.
By turn 10 of the batch run, tool results made up 71 percent of the request. By turn 35 the reasoning had caught up: it took about as many bytes as every tool result in the request combined. Since it is encrypted, the harness has no way to condense it, so all the policies below treat each turn’s reasoning as part of that turn and keep or drop them together.
Policies, not rewrites
How to build a request is a value you pass to the loop:
// A ContextPolicy builds the messages one request sends from the full
// conversation. The loop keeps the full conversation in
// Result.Messages and never rewrites it; a policy only decides what the
// model sees. Its state, such as how much it has trimmed, lives in a
// ContextState that the loop holds and passes back each turn, so a
// policy keeps nothing between calls and the same conversation and
// state always build the same request.
type ContextPolicy interface {
Build(ctx context.Context, msgs []Message,
st ContextState) (Window, error)
}
// A Window is what a policy built: the request's messages, the state
// to pass back next turn, and what building it cost, such as a summary
// call.
type Window struct {
Messages []Message
State ContextState
Usage Usage
}
// ContextState is a context policy's memory between turns. Each field
// is an index into the full conversation or text derived from it.
type ContextState struct {
Cut int // Trim: messages before this are left out
Cleared int // ClearResults: tool results before this are cleared
Summary string // Compact: the latest summary of earlier turns
Covered int // Compact: the summary covers messages before this
}
A context policy takes the full conversation and returns the messages for one request.
It leaves the conversation alone; Result.Messages still holds every message.
You want that complete history when you debug a bad run, when an eval grades one, and when a stopped run resumes.
Policies are also stateless: whatever a policy has decided earlier is handed back to it in a ContextState.
Given the same conversation and state, a policy always produces the same request, so you can unit-test it and resume a run without surprises.
Whatever a policy removes, it removes in whole turns. The API requires every tool call in an assistant message to be followed by its result, and every result to follow its call; break that pairing and the request is refused.
Five tools, cheapest first
Prompt caching.
Consecutive requests share almost everything up front, and providers charge roughly a tenth of the normal input price for a prefix they have already seen.
Some models cache on their own; the default model wants explicit markers.
CacheMarks sets two of them, one after the system prompt and task and one at the end of the request.
That single change took the batch from $1.33 to $0.23, and 95 percent of the input tokens were read from the cache.
The cache only works if the beginning of the request stays identical, so anything that varies there breaks it: a clock reading in the system prompt, say, or tool definitions emitted in map iteration order (Go deliberately randomizes the order of a map, so the same tools can come out in a different order on each request).
Clearing old results.
In an old turn, the tool result is usually the bulk of the tokens, and if the model needs it again it can repeat the call.
ClearResults swaps each large, old result for a short note and keeps all the messages in place.
An early version started clearing on turn one.
The agent then kept re-reading what had vanished: it repeated a read it had already made, with the same arguments, 47 times across 16 files and 9 crash reports, and the run went 132 turns against roughly 35 for the other policies.
The version that ships waits until the request crosses a size limit, and then advances its boundary several turns at a time, so consecutive requests keep the same prefix and the cache keeps working.
Trimming.
Once a request is too long, Trim drops the oldest complete turns.
It always keeps the system prompt and the task, and it adds a note saying turns were removed, so the model knows to re-read a file instead of concluding it never looked.
The batch cost $0.09 this way.
Trimming suits this agent because it handles crash groups one after another; turns from a group it finished long ago are almost never needed again.
Compaction. Sometimes the useful findings are scattered over dozens of turns. Then the harness can ask a model to summarize the older turns and send the summary in their place; this is compaction. The summary is written from the complete conversation, before any results were cleared, and it goes into the request marked as the harness’s own record. Summaries tend to drop exact values such as crash IDs, file paths, and issue numbers, and losing an issue number can lead the agent to file the same crash twice. So code compares the summary against every identifier the agent has used and appends any that are missing. The batch runs produced one summary; it kept 48 of 54 identifiers, and the check added back the remaining six.
Notes the agent keeps. Two tools let the agent save and fetch notes for each crash group. Since they are stored apart from the conversation, policies never touch them. The idea is that the model, better than any harness rule, can tell which findings are worth keeping. On this batch it wrote as many as 11 notes in a run and read none of them, since each group was finished before its turns were trimmed.
The default chain
The default policy of the crash-triage agent, which reads crash reports from the remind practice service and files issues in a tracker, chains these together, starting with the one that loses least:
// contextPolicy returns the context policy for a -context setting. The
// default compacts once a request would pass Limit tokens, clearing old
// tool results from what it sends but not from what it summarizes, and
// marks cache points on what is left, so a long batch stays well
// inside the window and the repeated start of each request comes from
// the cache.
func contextPolicy(name string,
m agent.Model) (agent.ContextPolicy, error) {
const limit = 60_000 // tokens: far below a 1M window, and cheap
switch name {
case "none":
return nil, nil
case "cache":
return agent.CacheMarks{}, nil
case "clear":
return agent.Chain(agent.ClearResults{Limit: limit},
agent.CacheMarks{}), nil
case "trim":
return agent.Chain(agent.Trim{Limit: limit},
agent.CacheMarks{}), nil
case "compact", "default":
return agent.Chain(agent.Compact{Model: m, Limit: limit,
Sent: agent.ClearResults{Limit: limit}},
agent.CacheMarks{}), nil
}
return nil, fmt.Errorf("no context policy %q; try none, "+
"cache, clear, trim, or compact", name)
}
Once a request passes 60,000 tokens, estimated from its byte count, it clears old results; it summarizes only if clearing falls short; and last it places cache markers on whatever remains. The estimate assumes four bytes per token. On the recorded run it averaged 0.57 of the token count the provider reported, and never went above 0.70. Code and crash reports take fewer bytes per token than English prose, so four bytes per token undercounts them, and the estimate also leaves out the tool definitions, about 4,000 tokens on every request. Dividing 60,000 by 0.57 gives about 105,000, so 60,000 estimated tokens are near 100,000 real ones. If the limit exists to keep requests under the price tier, set it lower, or calibrate it from the token usage the last reply reported.
What the runs showed
| Policy | Turns | Cost | From the cache | Checks passed (of 94) |
|---|---|---|---|---|
| None | 35 | $1.33 | 0% | 86 |
| Cache markers | 33 | $0.23 | 95% | 86 |
| Trim, then cache markers | 35 | $0.09 | 88% | 85 |
| Clear from the start, then cache markers | 132 | $0.68 | 76% | 81 |
| Clear past 60,000 tokens, then cache markers (two runs) | 62 and 30 | $0.42 and $0.06 | 82% and 90% | 87 and 82 |
Every row is one batch run on the default model, except the last, which is two, and the checks are the catalog’s 94 for this task. Read that last row before the others. Nothing changed between its two runs except the model’s choices, and one run took 62 turns for $0.42 while the other took 30 turns for $0.06, a sevenfold difference in cost. So a gap between two single-run rows means something only when it is large and has a cause you can point to. Two of the gaps can be read that way: caching, where 95 percent of the input came from the cache against none, and the 132 turns from clearing too early, which line up with the 47 repeated reads. Compaction and notes have no row of their own; what they did is reported above. The checks-passed column, which ranges from 81 to 87, is within the spread you could get by running any row again.
What to take from it
- Mark the cache first, and keep the beginning of every request identical from one turn to the next.
- Keep the full conversation, and derive each request from it through a policy you can unit-test.
- Clear before you summarize. A cleared result costs a line and can be fetched again; a summary is another model’s lossy rewrite.
- Act only past a limit, and in chunks, so the request’s prefix stays stable and keeps hitting the cache.
- Check summaries in code for the identifiers the agent relies on.
- Measure on your own task, and run each policy more than once.
When to use it
Reach for it
- Requests grow toward a price tier. Past 100,000 tokens, the default model bills every token of the request at five times the base rate, and every single-agent batch run came within 4 percent of that line, one crossing it.
- The provider caches only what you mark. Cache markers cut one batch run on the default model from $1.33 to $0.23 for the same work.
Leave it out
- Clearing old results early in a run. Cleared from the start, the agent reread the same things 47 times and took 132 turns where the others took about 35.
- Runs that stay short. On the longest single task recorded, no request passed 38,000 tokens, well under where the default policy starts clearing.
Try it
From the code directory of a clone of the companion repository, with no key set:
$ go run ./ch08 -task batch
This replays the recorded model replies under the default policy (30 turns), while every tool runs against fresh copies of the remind service, and the outcome is graded against the catalog.
Because it runs the service’s tests many times, it takes several minutes.
The output lists each turn’s tool calls and ends with the model’s final submission, then two lines like these:
stopped: the model answered
1728605 tokens in (1560770 from the cache), 55749 out, $0.0645
The policies are in ch08/agent/trim.go and ch08/agent/compact.go.
type ContextPolicy interface {
Build(ctx context.Context, msgs []Message,
st ContextState) (Window, error)
}
A ContextPolicy builds the messages one request sends from the full conversation.
A ContextPolicy builds the messages one request sends from the full conversation. type ContextPolicy interface { Build(ctx context.Context, msgs []Message, st ContextState) (Window, error) }. In ch08/agent/policy.go, line 18.
func (ContextPolicy) Build(ctx context.Context, msgs []Message,
st ContextState) (Window, error)
func (ContextPolicy) Build(ctx context.Context, msgs []Message, st ContextState) (Window, error). In ch08/agent/policy.go, line 19.
var ctx context.Context
var ctx context.Context. In ch08/agent/policy.go, line 19.
package context ("context")
Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes.
Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes. package context ("context"). Documented at pkg.go.dev, context.
type Context interface {
// Deadline returns the time when work done on behalf of this context
// should be canceled. Deadline returns ok==false when no deadline is
// set. Successive calls to Deadline return the same results.
Deadline() (deadline time.Time, ok bool)
// Done returns a channel that's closed when work done on behalf of this
// context should be canceled. Done may return nil if this context can
// never be canceled. Successive calls to Done return the same value.
// The close of the Done channel may happen asynchronously,
// ...
}
A Context carries a deadline, a cancellation signal, and other values across API boundaries.
A Context carries a deadline, a cancellation signal, and other values across API boundaries. type Context interface { // Deadline returns the time when work done on behalf of this context // should be canceled. Deadline returns ok==false when no deadline is // set. Successive calls to Deadline return the same results. Deadline() (deadline time.Time, ok bool) // Done returns a channel that's closed when work done on behalf of this // context should be canceled. Done may return nil if this context can // never be canceled. Successive calls to Done return the same value. // The close of the Done channel may happen asynchronously, // ... }. Documented at pkg.go.dev, context.Context.
type Message struct {
Role string // "system", "user", "assistant", or "tool"
Content string // empty when the message has tool calls
ToolCalls []ToolCall // set on assistant messages that call tools
ToolCallID string // on tool messages: the call this answers
IsError bool // set on tool messages that report a failure
// ReasoningDetails is a reasoning model's record of its thinking,
// sent back unchanged with the tool calls it led to (chapter 4).
ReasoningDetails jsontext.Value
// ...
}
Message is one entry in the conversation.
Message is one entry in the conversation. type Message struct { Role string // "system", "user", "assistant", or "tool" Content string // empty when the message has tool calls ToolCalls []ToolCall // set on assistant messages that call tools ToolCallID string // on tool messages: the call this answers IsError bool // set on tool messages that report a failure // ReasoningDetails is a reasoning model's record of its thinking, // sent back unchanged with the tool calls it led to (chapter 4). ReasoningDetails jsontext.Value // ... }. In ch08/agent/types.go, line 38.
type ContextState struct {
Cut int // Trim: messages before this are left out
Cleared int // ClearResults: tool results before this are cleared
Summary string // Compact: the latest summary of earlier turns
Covered int // Compact: the summary covers messages before this
}
ContextState is a context policy's memory between turns.
ContextState is a context policy's memory between turns. type ContextState struct { Cut int // Trim: messages before this are left out Cleared int // ClearResults: tool results before this are cleared Summary string // Compact: the latest summary of earlier turns Covered int // Compact: the summary covers messages before this }. In ch08/agent/policy.go, line 34.
type Window struct {
Messages []Message
State ContextState
Usage Usage
}
A Window is what a policy built: the request's messages, the state to pass back next turn, and what building it cost, such as a summary call.
A Window is what a policy built: the request's messages, the state to pass back next turn, and what building it cost, such as a summary call. type Window struct { Messages []Message State ContextState Usage Usage }. In ch08/agent/policy.go, line 26.
type error interface {
Error() string
}
The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error.
The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error. type error interface { Error() string }. Documented at pkg.go.dev, builtin.error.
field Messages []Message
field Messages []Message. In ch08/agent/policy.go, line 27.
field State ContextState
field State ContextState. In ch08/agent/policy.go, line 28.
type Usage struct {
PromptTokens int // everything sent, counted in tokens
CompletionTokens int // everything the model wrote
CachedTokens int // prompt tokens served from the cache
ReasoningTokens int // completion tokens spent on reasoning
Cost float64 // in dollars, as the provider reported it
// CacheWriteTokens are prompt tokens written to the cache, which
// some providers bill above the usual input price (chapter 8).
CacheWriteTokens int
}
Usage is what one model call consumed.
Usage is what one model call consumed. type Usage struct { PromptTokens int // everything sent, counted in tokens CompletionTokens int // everything the model wrote CachedTokens int // prompt tokens served from the cache ReasoningTokens int // completion tokens spent on reasoning Cost float64 // in dollars, as the provider reported it // CacheWriteTokens are prompt tokens written to the cache, which // some providers bill above the usual input price (chapter 8). CacheWriteTokens int }. In ch08/agent/types.go, line 89.
field Cut int
Trim: messages before this are left out
Trim: messages before this are left out field Cut int. In ch08/agent/policy.go, line 35.
type int int
int is a signed integer type that is at least 32 bits in size.
int is a signed integer type that is at least 32 bits in size. type int int. Documented at pkg.go.dev, builtin.int.
field Cleared int
ClearResults: tool results before this are cleared
ClearResults: tool results before this are cleared field Cleared int. In ch08/agent/policy.go, line 36.
field Summary string
Compact: the latest summary of earlier turns
Compact: the latest summary of earlier turns field Summary string. In ch08/agent/policy.go, line 37.
type string string
string is the set of all strings of 8-bit bytes, conventionally but not necessarily representing UTF-8-encoded text.
string is the set of all strings of 8-bit bytes, conventionally but not necessarily representing UTF-8-encoded text. type string string. Documented at pkg.go.dev, builtin.string.
field Covered int
Compact: the summary covers messages before this
Compact: the summary covers messages before this field Covered int. In ch08/agent/policy.go, line 38.
func contextPolicy(name string,
m agent.Model) (agent.ContextPolicy, error)
contextPolicy returns the context policy for a -context setting.
contextPolicy returns the context policy for a -context setting. func contextPolicy(name string, m agent.Model) (agent.ContextPolicy, error). In ch08/task.go, line 21.
package agent ("github.com/blackwell-systems/go-agents-book/code/ch08/agent")
Package agent is the agent loop and the types it works with.
Package agent is the agent loop and the types it works with. package agent ("github.com/blackwell-systems/go-agents-book/code/ch08/agent"). In ch08/agent.
type Model interface {
Generate(ctx context.Context, req Request) (Reply, error)
}
Model is anything that can produce the next reply in a conversation: a scripted fake, or a real model such as the OpenRouter client.
Model is anything that can produce the next reply in a conversation: a scripted fake, or a real model such as the OpenRouter client. type Model interface { Generate(ctx context.Context, req Request) (Reply, error) }. In ch08/agent/types.go, line 13.
var nil Type
nil is a predeclared identifier representing the zero value for a pointer, channel, func, interface, map, or slice type.
nil is a predeclared identifier representing the zero value for a pointer, channel, func, interface, map, or slice type. var nil Type. Documented at pkg.go.dev, builtin.nil.
type CacheMarks struct{}
CacheMarks marks where a provider that needs explicit markers may cache the request: the end of the head, which never changes, and the last message, so the next request, which starts with all of this one, can read it back from the cache.
CacheMarks marks where a provider that needs explicit markers may cache the request: the end of the head, which never changes, and the last message, so the next request, which starts with all of this one, can read it back from the cache. type CacheMarks struct{}. In ch08/agent/trim.go, line 115.
func Chain(ps ...ContextPolicy) ContextPolicy
Chain applies policies in order, each to the messages the one before it built.
Chain applies policies in order, each to the messages the one before it built. func Chain(ps ...ContextPolicy) ContextPolicy. In ch08/agent/policy.go, line 51.
type ClearResults struct {
Limit int // tokens; clear only past this, 0 means at once
Keep int // recent turns whose results stay; 0 means 3
Min int // results of this many bytes or fewer stay; 0 means 500
Chunk int // turns cleared at a time; 0 means 4
}
ClearResults replaces the content of old, long tool results with a note saying how to get them again, keeping every message, so no call loses its answer.
ClearResults replaces the content of old, long tool results with a note saying how to get them again, keeping every message, so no call loses its answer. type ClearResults struct { Limit int // tokens; clear only past this, 0 means at once Keep int // recent turns whose results stay; 0 means 3 Min int // results of this many bytes or fewer stay; 0 means 500 Chunk int // turns cleared at a time; 0 means 4 }. In ch08/agent/trim.go, line 66.
field Limit int
tokens; clear only past this, 0 means at once
tokens; clear only past this, 0 means at once field Limit int. In ch08/agent/trim.go, line 67.
type Trim struct {
Limit int // tokens
Chunk int // turns cut at a time; 0 means 4
}
Trim leaves the oldest turns out of the request once it is estimated to be over Limit tokens, keeping the head and the latest turn.
Trim leaves the oldest turns out of the request once it is estimated to be over Limit tokens, keeping the head and the latest turn. type Trim struct { Limit int // tokens Chunk int // turns cut at a time; 0 means 4 }. In ch08/agent/trim.go, line 18.
type Compact struct {
Model Model
Limit int // tokens
Keep int // recent turns kept word for word; 0 means 4
// Sent is applied to the messages the request sends, never to
// what the summary reads, such as ClearResults; it must keep the
// conversation's length.
Sent ContextPolicy
// ...
}
Compact summarizes older turns once the request is estimated to be over Limit tokens: the head stays, older turns are replaced by one summary, and the latest turns stay word for word.
Compact summarizes older turns once the request is estimated to be over Limit tokens: the head stays, older turns are replaced by one summary, and the latest turns stay word for word. type Compact struct { Model Model Limit int // tokens Keep int // recent turns kept word for word; 0 means 4 // Sent is applied to the messages the request sends, never to // what the summary reads, such as ClearResults; it must keep the // conversation's length. Sent ContextPolicy // ... }. In ch08/agent/compact.go, line 28.
field Limit int
tokens
tokens field Limit int. In ch08/agent/compact.go, line 30.
field Sent ContextPolicy
Sent is applied to the messages the request sends, never to what the summary reads, such as ClearResults; it must keep the conversation's length.
Sent is applied to the messages the request sends, never to what the summary reads, such as ClearResults; it must keep the conversation's length. field Sent ContextPolicy. In ch08/agent/compact.go, line 36.
package fmt ("fmt")
Package fmt implements formatted I/O with functions analogous to C's printf and scanf.
Package fmt implements formatted I/O with functions analogous to C's printf and scanf. package fmt ("fmt"). Documented at pkg.go.dev, fmt.
func Errorf(format string, a ...any) (err error)
Errorf formats according to a format specifier and returns the string as a value that satisfies error.
Errorf formats according to a format specifier and returns the string as a value that satisfies error. func Errorf(format string, a ...any) (err error). Documented at pkg.go.dev, fmt.Errorf.

