AI

Stop babysitting your coding agent’s context window

On a 150-task benchmark, early compaction cut Codex token use by 62% and Claude Code cost by 32%. I built better-auto-compact to handle the handoff while the agent keeps working.

Sasa Fajkovic5 min readAI
Stop babysitting your coding agent’s context window

The same check cost me 6 cents in Claude Code with about 120,000 tokens in the session and 21 cents with about 880,000. It passed all 16 tests both times. The difference was how much old output Claude had to read to do the next piece of work. Context-fill experiment

I could keep the session small myself. Watch the context meter. Find a stopping point. Ask the agent to write down what it tried and what still fails. Compact the conversation and check that the agent knows where to resume. That works while I’m at the keyboard. It also gives a long-running job a dependency on me.

I built better-auto-compact so Claude Code and Codex CLI can do that handoff on their own. I can leave a batch running and come back to review the result. Plugin on GitHub

Why hand off early?

A test log has two very different uses. While the agent is debugging, it may need the whole trace. Once it has isolated the failure, a single line can name the failing test, the error code and the command that reproduces it. A handoff keeps those details available after the client summarises the conversation. The next turn can start with a short record of the work. Handoff skill

Matt Pocock’s /handoff skill lets you tell the agent what the next session is for. It writes a short file for that session and links to plans, commits and diffs you already have. Boris Cherny described a similar decision on the Claude Code team. They found that giving Claude a fresh context window after a user approved a plan helped it follow that plan for longer. Pocock’s handoff skill · Cherny on starting from an approved plan

When should you do it? Pablo González writes a one-page handoff at about 30% of the context window used, then clears the session. Dex Horthy at HumanLayer aims for 40–60% utilisation depending on the work. In Nathan Onn’s original Claude Code workflow, you finish the task, run /handoff-doc, then run /compact. Someone still has to choose the moment. González’s workflow · Horthy on context engineering · Onn’s handoff skill

What I wanted to automate was the timing. Before the client reaches its compaction point, my plugin asks the agent to save the decisions, exact values and next step while the original output is still in view. After the client compacts, the plugin tells the agent where to read that handoff. There’s no command for me to issue between batches. How the plugin works

What 150 tasks cost

I gave a lead agent 150 tasks to delegate in batches of five. Eighteen failed once and needed a retry. At the end, I scored every result ID and retry code against the expected value. I ran the workload at four compaction settings in both clients, twice per setting. That’s 2,400 task executions. Eight more runs with the handoff skill loading correctly brought the total to 3,600 task executions on this benchmark. Workload and test runs

These are the eight reruns. The plugin was active at both settings. I changed when the client compacted and kept the tasks and seeds the same. Paired results

Per runClient’s default thresholdCompact at 30%Saving
Claude Code cost$12.12$8.2132%
Codex CLI tokens166 million64 million62%

Codex returned all 150 result IDs and all 18 retry codes in every rerun. Claude returned all 150 IDs at its default threshold. At 30%, it returned 150 in one run and 148 in the other. It got every retry code right at both settings. On this workload I’d take that trade for a job where I can check the two missing IDs. Accuracy by run

I tested the handoff itself separately. I put five values in tool output, forced at least three compactions and asked for the values without letting the agent look them up. With the handoff, both clients recovered all five in every run. Built-in compaction averaged 2.5 in Claude Code and 1.3 in Codex. Here, getting the details right cost Codex 29% more tokens. Shortening the history saved tokens in the batch job. Writing the handoff earned its keep when the exact values mattered. Recall test and costs

The settings I ship

A fresh install uses 30% for Claude Code and 40% for Codex CLI unless you already manage those settings. The token counts depend on the window. Claude Sonnet 5.5 had a 1-million-token window in my tests. After reserving 20,000 tokens, Claude Code compacted at about 294,000 tokens. On a 200,000-token Claude window, the same 30% kicks in at about 54,000. In Codex, GPT-6.1 Sol and Terra had 475,000 usable tokens out of 500,000, so 40% meant 190,000 tokens. Measured compaction points · How the installer uses your window

I chose 40% for Codex after a separate 90-trial comparison. Both models gave exact answers in all nine runs each at that setting. I also timed coding tasks in a persistent workspace. Both setups passed all the coding tasks, and built-in compaction finished faster in all eight matched pairs. Follow-up measurements

To try it on macOS or Linux with Python 3.8 or later, preview the install before applying it. Installation

git clone https://github.com/sasa-fajkovic/better-auto-compact.git
cd better-auto-compact
python3 install.py --dry-run
python3 install.py

Add --no-threshold to both installer commands if you want to try just the handoff with your current settings. In Codex, approve the hooks in /hooks. Installer options

If we meet in Frankfurt in December, I’d like to hear where you still have to interrupt an agent to keep a job on track. That’s the part of a workflow I’d measure next.

Back to Blog