Checking how many agents are working

How this works
Contact
← Blog
A long dark steel scroll partly unrolled across a bench, its far end folded into neat olive-lit layers, with the AMARBARO mark centered above it.
✎blog post

The context window: how much a model can keep in mind

The short version

A context window is a fixed budget of tokens, and a long chat has to be trimmed or folded; folding keeps the original so details can be recovered.

What this is about

A model can only look at a fixed amount of text at one time. That amount is its context window, and it is counted in tokens. Your question, the model's earlier answers, any files you pasted and the instructions all share the same window.

When a chat grows past the window, something has to go. This article explains what happens, and how a small tool we wrote deals with it.

Why it matters

Picture a desk of fixed size. You can lay out only so many pages. A model does not "remember" a conversation the way you do. On every turn, the whole chat so far is sent again and read again. If it does not fit, the oldest pages fall off the desk.

This matters in three ways.

  • Forgetting. Something you said early may be gone, and the model will not know it is gone.
  • Cost and speed. Each turn re-reads everything, so a long chat gets slower and, on a hosted service, more expensive.
  • Failure. Past a hard limit, a request can simply be refused.

How it works

The window has a size, such as 128,000 tokens. Each turn, a chat program builds one big text: the instructions, then every earlier message, then your new one. The model reads all of it and writes the next piece.

There are three common ways to cope when the text grows too long.

  1. Cut the oldest messages. Simple, but you lose things you may need later, such as a file the model read or an error it hit.
  2. Summarize the old part. Replace many old messages with a short summary. This saves room but the summary may leave out a detail.
  3. Summarize and keep the original. Replace the old messages with a summary, but store the full text somewhere, so that it can be fetched back.

The third is what our tool, ctxd, does.

How ctxd folds a chat

ctxd is a small proxy. You point your chat program at it, and it passes each request on, unchanged, until the chat grows large. Then it steps in.

Here is the rule. It estimates the size of each request as the number of bytes divided by four. If the estimate passes 80 percent of the window, it takes the oldest block of messages outside the last six, cuts the block at a tool-call boundary, and sends it to a summarizer. It then replaces the block with one message of the form [fold#<id>] <summary>. The summary asks for exact file paths, names, numbers, decisions, errors and open tasks, with no filler. The original messages go into a local store under your home directory.

If the model later needs the detail, it can ask. A tool called ctx_expand returns the original messages behind a fold, byte for byte. ctx_search looks for an exact piece of text across the whole chat, folded parts included. There is also compress, to fold now instead of waiting for the threshold.

If folding fails for any reason, ctxd sends the original request on unchanged and logs why. Without a summarizer configured, it does nothing and is a plain pass-through. It listens only on your own machine.

In the example settings, the window is 200,000 tokens for one route and 128,000 for the other. These are settings, not facts about every model.

What we measured, and what we did not

The store is the part we have tested. In one run it wrote 20,000 messages, 4.76 MB in total, about 1.19 million tokens by the bytes-over-four guess, in 91.7 milliseconds. Reopening them added 5.1 MB to memory use. Each message cost 23.6 bytes of overhead beyond its own text. Searching for one hit at the end took 0.095 milliseconds.

What we have not done is measure whether folding saves billed tokens in real use. The plan names that A/B test as the acceptance test, and it is still to be run. We also do not measure how faithful the summaries are, because they come from a model. And the search is exact text, not meaning. So treat ctxd as a careful idea with a tested store, not a proven saving.

The cheapest context is the one that never arrives

Folding is a repair. It is better to stop sending things that should not be sent. We found one block of path hints, 964 bytes long, that a hook sent 50 times in one session. That is about 48 KB of text, read over and over. Removing it at the source was better than compressing it. We later retired the filter and cut the sender instead.

So before you reach for clever folding, look at what your chat program sends each turn. Often a large part is repeated instructions or leftover tool output.

Long contexts at home

For a local model, a long window also costs graphics memory, because the model keeps notes on every token it has read. Our engine can read a prompt in chunks. Reading a 16,384-token prompt took 4.54 seconds on one card against 5.66 for llama.cpp, on the same model file. That was one measurement on one card.

Words you may not know

  • Context window: the most tokens a model can read at once.
  • Token: a small piece of text, covered in an earlier article.
  • Proxy: a program that sits between you and a service and passes messages along.
  • Fold: replacing old messages with a summary while keeping the originals.
  • Summarizer: a model asked to write a short version of text.
  • Tool call: a request from the model to run a tool, such as reading a file.
  • Store: the local files where ctxd keeps the original messages.

Try it

  • Read how ctxd works, with its measured store numbers and its limits, on the ctxd project page.
  • The code is public: ctxd on GitHub. The README shows a Docker start that runs it on port 8787 on your own machine.
  • A home experiment: in any chat, give an early fact, chat about other things for a long while, then ask about the fact. Notice when the model loses it.

Comments

No comments yet.

Log in to comment.