Checking how many agents are working

How this works
Contatti
← Blog
A long dark scroll folded neatly into a small olive packet, with the full scroll kept in a teal drawer below and the AMARBARO mark centered.
✎post del blog

ctxd: keeping long AI sessions short

The short version

ctxd folds the oldest block of a conversation into a summary once a request passes 80% of its window, and ctx_expand returns the folded messages byte for byte from the local store.

Part 7 of 20 in Tools we built and why. Previous: mojo-port: checking that a rewrite does the same thing · Next: mojo-baro: our chat engine for one AMD card

What this is about

An AI chat has a limit on how much it can hold in mind at once. That limit is called the context window. When a long conversation, such as a coding session, grows past it, the program has to throw something away or the request fails. Each turn also resends the whole history, so every turn costs more than the last.

ctxd is a small program that sits between your AI client and the API it talks to. It passes requests through unchanged until a conversation grows large. Then it folds the oldest part into a short summary and stores the original text on your own disk. If the model later needs a folded detail, it can ask for the original and get it back exactly.

Why it matters

The simple fix for a long chat is to cut off the old part. That loses things you will need: the file the model read, the error it hit, the decision it made. A summary keeps the gist, but a summary can drop the one number that matters.

ctxd does both. The summary carries an id. The original messages stay in a store. A model that reads the summary can see that detail has been folded, and can ask for the full text.

How it works

Here is what happens on each request, in plain steps.

  1. Listen. ctxd listens on your own computer only, at 127.0.0.1. It handles two routes: the Anthropic message route that Claude Code uses, and the OpenAI-style chat route. Each has its own upstream address and window size.
  2. Measure. It estimates the size of the request as bytes divided by four. If the estimate passes 80% of the window, it acts. If not, it forwards the request unchanged.
  3. Choose. It takes the oldest block of messages outside the last six, and cuts it at a tool-call boundary so a call is never split from its result.
  4. Summarize. It sends that block to a summarizer, any OpenAI-compatible endpoint you configure. The prompt asks for exact file paths, names, numbers, decisions, errors and open tasks, with no preamble.
  5. Replace. The block becomes one message that starts with a fold id, such as [fold#3], followed by the summary. ctxd puts the cache marker back so the prompt cache can still be used.
  6. Keep. The original messages are written to a store under ~/.cache/ctxd, in three flat files: the raw messages, an index of where each starts, and a record of which messages each fold covers.

If folding fails for any reason, ctxd sends the original request unchanged and logs why. If you give it no summarizer, it never folds, and acts as a plain pass-through.

The model gets small ctx_ tools only when a session holds 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. A compress action folds old history now instead of waiting for the threshold. ctxd runs those calls itself and gives the client only the final answer.

How to do it, step by step

The project page shows a Docker path.

  1. Build and run it with docker/build.sh && docker/run.sh. The container binds 127.0.0.1:8787 and keeps the store in ~/.cache/ctxd.
  2. Point your client at it. For Claude Code, that means starting it with ANTHROPIC_BASE_URL=http://127.0.0.1:8787 claude.
  3. Give ctxd a summarizer. Put its address and model name in ctxd.toml, and the key in a separate environment file.
  4. Work as usual. When a request passes the threshold, ctxd folds the oldest block. You will see [fold#...] messages in the history.
  5. To go back, ask for a fold with ctx_expand.

A systemd user service is available too. service/install.sh builds ctxd into ~/.local/opt/ctxd and enables it.

What we measured, and what we did not

The store has a test, tests/gate-s1.sh. On 2026-11-16 it wrote 20,000 messages, 4.76 MB in all, and reopened them. Appending took 91.7 ms, about 218,000 messages per second. Reopening raised resident memory by 5.1 MB. A search with one hit at the last message took 0.095 ms. Messages of 1.5 MB and 3 MB came back byte for byte after a reopen.

What we have not measured matters just as much. The repository has no recorded run of the folding policy on billed tokens. We do not measure how faithful the summaries are, because they come from a model. And release 0.1.0 shipped without the Docker gate, tests/gate-s4.sh.

Other limits

  • It needs a mojo-bento checkout beside it. There is no package yet.
  • It listens on localhost only and is not built to be a network service.
  • ctx_search looks for exact text. It is not a search by meaning.
  • A store's main file is limited to 4 GiB, because offsets are 32-bit. Past that, the store raises an error.

Words you may not know

  • Context window: how much text a model can take in one request.
  • Proxy: a program that sits in the middle and passes traffic along.
  • Fold: replacing old messages with a short summary.
  • Prompt cache: a feature that makes repeated beginnings of a request cheaper.
  • Summarizer: a model that writes the short version.
  • Localhost: your own computer, at the address 127.0.0.1.
  • Byte for byte: exactly the same, with no change at all.

Try it

  • Read the ctxd project page.
  • Get the code at github.com/amarbarosys/ctxd. It is Apache-2.0 and release 0.1.0 is dated 2026-11-11.
  • Run it with no summarizer first. Check that it passes your requests through unchanged. Then add a summarizer.
  • After a fold, call ctx_expand on its id and compare with what you remember.

Commenti

Ancora nessun commento.

Accedi per commentare.