Long turns, not token prices, drove my bill
If you run AI coding agents for hours at a time, or several at once, this is for you. I run several in parallel under Rigby, my own harness: a small supervisor that starts them, passes messages between them and merges their finished work.
One agent, the head (Claude Opus), plans the work and hands out units of it. The others, which I call lanes, do the units: Codex on one, a cheap model such as DeepSeek on another.
The most expensive lesson was about long turns, not token prices. A turn is one instruction and all the model calls an agent makes to finish it.
One of those turns read the same test file 404 times over three hours, and no change to the prompt wording fixed it.
Drew Breunig’s write-up of his AGNTCon talk, The State of Context Engineering, calls that context inertia. He argues that the work of assembling an agent’s context keeps moving out of model calls and into deterministic code.
Nearly every tactic he lists ended up in my own harness, usually after a measurement showed me that a model couldn’t be trusted to do it.
I’ve written about how work is handed over and how the agents clean up after themselves. The change that mattered most for context was how long a session is allowed to live.
Start a fresh session for each unit of work
The most useful rule I have is that a producer lane, one that writes the code, gets a fresh session for each unit of work. I didn’t start there.
Early on, a unit sent to a busy lane was pasted into its running turn. That turn never ended between units, so no reset was ever due.
Now Rigby decides when a lane starts over, from its own records rather than from the screen or the model’s judgement.
A unit delivered since the lane’s last reset stays in that session, and new units wait behind it. Once the lane reports the unit finished and its turn has ended, the next wake, a doorbell that tells the lane to check its mailbox, starts a fresh session.
Reviews go to the front of the queue, then corrections, then new work.
Before (simplified)
# a new unit of work arrives for a lane
paste_into_lane(unit) # lands in whatever turn is running, so the session never endsAfter (simplified)
# a new unit of work arrives for a lane
if lane.has_open_unit(): # sent since its last reset, not finished
hold(unit)
elif lane.finished_a_unit() and lane.turn_ended():
reset(lane) # /clear, /new or a fresh process
wake(lane) # a doorbell: "check your mailbox"The wake itself carries no content, so nothing stale is ever typed into a session that’s busy with something else.
This is Breunig’s quarantine, and close to Anthropic’s Memento pattern, applied where it costs least: the end of a unit, when everything worth keeping is already in a commit or a mailbox record.
So the rule I work to is one unit of work, one session. If your agents run for hours, end the session when a unit is committed and start the next from a short written brief.
Set a compaction limit well below the window
The head is the exception. It holds the plan, so it never gets reset, and it compacts instead, which means summarising its history to free up room.
Claude Code and Codex compact near the model’s window by default. On a 1M-token model, that lets a head carry close to a million tokens of history into every call.
Breunig starts watching his own sessions at around 200–300K tokens. The Gemini 2.5 report saw agents start repeating old actions past roughly 100K.
A freshly compacted head restarts at 130–170K tokens, which is why 200K had no room to work. A 300K limit should cut the head’s input by around a fifth.
Both runtimes have a documented setting
# Claude Code
CLAUDE_CODE_AUTO_COMPACT_WINDOW=300000 claude
# Codex
codex -c model_auto_compact_token_limit=300000If you run long agent sessions, set this yourself. The default lets the window fill almost to its limit before anything is summarised.
Check your agent’s fixed context first, since a freshly compacted session has to fit under the limit with room left to work.
Cap the model calls one turn can make
Compaction did nothing for the turns that cost the most. Context inertia, in Breunig’s words, is a model hyper-focusing on a hypothesis and pursuing it long after it should stop, and the 150-call chart near the top is what it cost me.
The worst turn I traced was a code review that spent nearly three hours reading one test file in the same ten slices.
Neither of my two loop guards noticed. Both look for the same call repeated back to back, and every call here asked for a different slice.
Two mechanisms handle it now, both outside the model. OpenCode has a per-agent step limit, so its lanes run with agent.build.steps set to 150.
Claude Code has no step limit in an interactive session. Its lanes get a PreToolUse hook that counts the current turn’s model calls from the session transcript.
The lane’s settings
{
"hooks": {
"PreToolUse": [{
"hooks": [{
"type": "command",
"command": "python3 turn_cap.py 150",
"timeout": 5
}]
}]
}
}At 150 the hook refuses further tools, with a reason telling the model to report what it finished and stop. Ten calls later it ends the turn outright.
What the hook returns at the limit
{
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "deny",
"permissionDecisionReason": "This turn has made 150 model calls;
the lane's limit is 150. Call no more tools. Reply with what
you finished, what is left ..."
}
}I tested it live with the limit set to three. The third call was refused, and the model listed what it had done and what was left, then ended the turn.
When a lane loops on a single command, Rigby resets it, and the fresh session gets one line about what happened:
your last session was stopped after repeating: <command> . It returned the same
output every time. That result IS the answer: do not run it again; act on it.The capitals are a small confession. Breunig is right that shouting in a prompt usually means you’re fighting what the model was trained to do.
The shouting isn’t what does the work here. The reset is, and if the same lane loops twice within an hour, the head is told to split or reassign the unit.
If your agent has no step limit, count a turn’s model calls in a hook and stop it at a number you choose. Mine is 150, and I look hardest at the turns that run long, because that’s where both the quality and the money go.
Trim what a cheap agent loads at startup
A cap limits how far a turn can run. It does nothing about where a fresh one starts, which turned out to be a problem of its own.
Breunig’s example of context clash is an enterprise that loaded 600 skills per engineer at startup. Mine was smaller, but the same shape.
A cheap lane’s settings, in OpenCode and in Claude Code
// OpenCode, cheap lane
{
"permission": { "skill": "deny" },
"mcp": { "every-server-but-the-supervisor": { "enabled": false } }
}
# Claude Code, cheap lane
claude --strict-mcp-config --mcp-config '{"mcpServers": {"supervisor": {...}}}'At cache-hit prices a 47K prefix is cheap per call, so the bill isn’t the main reason to do this. Every skill description is one more thing for the model to weigh on a task that will never need it.
Measure the prompt your agent starts a task with, then remove what a lane will never use.
Let code write the handoff, not a model
A lane’s first message matters as much as what it loads at startup. I covered this in my post on handing work to coding agents, so briefly.
It’s a short brief that Rigby builds from rules the project has committed, matched to the files the unit touches. The reviewer gets the same contract, rebuilt from what the commit actually changed.
The head decides the work, but it can’t leave the written rules out. Past review findings reach the brief through a lessons loop.
Breunig predicts that memory is where the problem moves next, because precise memories pile up and start to conflict. I built the loop with that in mind.
If you feed past mistakes back to your agents, make a finding repeat before it goes in, cap how many go in at once, and retire the ones whose code is gone.
How far to trust it
A cheap lane on a non-Anthropic model doesn’t need any of that. The next step is a configuration holding only the supervisor’s hooks and the project’s instructions, then measuring the starting prompt before and after.
Try it on one agent this week
Start with one agent, and measure before you change anything:
- Count the model calls in each turn from the session transcript, and read the longest turns first.
- Measure the prompt your agent starts a task with, then remove the skills and MCP servers a lane will never use.
- Set the compaction limit yourself, well below the window. Mine is 300K on a 1M window, and 200K was too low for my fixed context.
- Cap calls per turn outside the model, with a step limit or a hook. Mine is 150.
- End a session when its unit is committed, and start the next from a short brief built from your project’s written rules.
The settings for items 2 to 4 are in the sections above, in the lean loadout, the compaction limit and the turn cap.
How code decides what each session sees
I found most of these by measuring first: the share of spend by turn length, the size of a fresh turn’s prompt, and the point where compaction thrashed. Then I moved the decision into Rigby.
The rule behind all of it is that if a model has to remember to manage its own context, sooner or later it won’t.
Breunig’s point that better context is the cheap lever, compared with a bigger model or more thinking time, is what my bills show too. His full write-up is worth reading.