Checking how many agents are working

How this works
Contact
← Blog
A cinematic photograph of a brass robot folding a long scroll of dense handwriting into a small neat square and filing the full scroll in a drawer behind it, in teal and steel with a warm amber light, the AMARBARO mark and wordmark centred over it.
✎post de blog

Folding old chat into summaries: how ctxd saves tokens

The short version

In ctxd's 80-turn gate session, requests totalled 2.55 MB without ctxd and 1.33 MB with it, about half, but no run of the fold policy on billed tokens is recorded yet.

What this is about

A long chat with an AI agent has a hidden cost. The model has no memory of its own, so the whole history is sent again with every turn. The longer the chat, the bigger every request.

One way out is to shrink the old part. ctxd, a small program we wrote, does it in a particular way: it folds old turns into short summaries and keeps the original text on disk. This article explains how, what it saves and what it risks.

Why it matters

When a chat grows past the model's window, one of three things happens. The request fails. Or the oldest part is cut off and forgotten. Or something squeezes it. Cutting loses things the agent will need later: the file it read, the error it hit, the decision it made.

Folding is an attempt at a fourth path. Keep what matters in a short form, and keep the full text where you can fetch it.

How it works

Think of a paper file on your desk that has grown too thick. You do not throw old pages away. You write a one-page summary of the first hundred, clip the originals into a folder, and write on the summary where the folder is. If you need a detail, you open the folder.

ctxd does the same, in steps.

  1. It sits in the middle. ctxd is a proxy. You point your agent at it instead of at the model service. It forwards each request unchanged, until the chat grows large.
  2. It estimates the size. It uses a simple rule: bytes divided by four. This is a rough guess at tokens.
  3. At 80 percent of the window, it folds. It takes the oldest block of messages, but never the last six. It cuts the block at a tool-call boundary, so a request and its answer stay together.
  4. A summariser writes the summary. The summariser is any model service that speaks the common chat format. Its prompt asks for exact file paths, names, numbers, decisions, errors and open tasks, with no preamble.
  5. The block is replaced by one message. It looks like [fold#12] summary text. Fold numbers are stable.
  6. The original goes to a local store. Each session gets its own folder with three flat files. Nothing is deleted.
  7. The agent can ask for the original back. ctxd offers three small tools to sessions that hold folds: ctx_expand returns the original messages behind a fold, byte for byte. ctx_search finds an exact piece of text in the whole session, folded parts included. compress folds older history now, before the window threshold.

If the fold fails for any reason, ctxd sends the original request unchanged and logs the reason. With no summariser configured, it does no folding and is a plain passthrough. Failing safe was a design goal.

What it saves

ctxd has gate scripts, small tests that run locally without a paid API. Here is what they measured.

The store. On 2026-11-16 a gate wrote 20,000 messages, 4.76 MB in total, and reopened them. Appending took 91.7 ms. Reopening used 5.1 MB more memory. Searching for a single hit at the last message took 0.095 ms.

The fold policy. In the 80-turn gate session, requests totalled 2.55 MB without ctxd and 1.33 MB with it, about half as many bytes. A 22 KB request got 210 microseconds of added delay at the median. Resident memory was 24.9 MB.

What it risks, and what we have not proved

We want to be careful here.

First, the savings above are bytes in a test session, not billed tokens in real work. The repository records no run of the fold policy on billed tokens. Its plan names that test, run on Claude Code and on a local model, as the acceptance test, and it is still to be run. So we do not say "ctxd saves half your bill." We say: in one gate session, the requests were about half as many bytes.

Second, a summary can lose a detail the agent needs. That is why the original is kept and why ctx_expand exists. But the agent has to realise it needs to look. Whether agents do that reliably is the open question, and our experience with memory hints in this series suggests it is not automatic.

Third, folding changes the start of the request, and the cache likes a stable start. ctxd keeps fold numbers stable and puts the cache marker back, so the cache can still work, but we have not measured the cache hit rate with it.

One more lesson from building it. Our first gate version used a made-up session that never put a fold boundary on a tool pair. A deliberate breakage of the cutting rule still passed. The gate was too gentle, so we made it harder. A check that cannot fail is not a check.

When to use it

Folding fits long sessions where you often return to older details. It does not fit short chats, where nothing grows. And it only helps if the summary is good, so the summariser you choose matters.

Glossary

  • Fold: replacing an old block of messages with one short summary message.
  • Proxy: a program between your agent and the model service.
  • Window: the most text a model can hold at once.
  • Summariser: the model that writes the summary.
  • Store: the local files where the original messages stay.
  • Gate: a test that must pass before a step counts as done.
  • Passthrough: forwarding a request unchanged.

Try it

  • Read the project. The ctxd page explains the design and lists the capabilities. The repository has the code and the gate scripts.
  • Run it locally. With Docker, the page gives two steps: build and run, then start your client with its API address set to the local ctxd port. With no summariser, it is a passthrough, so it is safe to try.
  • Do it by hand. On your next long chat, after twenty turns, ask the assistant for a summary of exact names, numbers and open tasks, and start a new chat with it. Did the new chat continue well? That is folding without the program.

Comentarii

Niciun comentariu încă.

Conectare pentru a comenta.