Docs
Compression
TOKENWERK can shrink the context of long agent sessions before a request reaches the model. You choose the compression per API key: none (the default), lossless or lossy.
Purpose
A coding agent sends the whole conversation so far with every call, including every earlier tool output: files it read, command output, diffs. In long sessions much of it repeats, and old output the work has long moved past is sent again with every call.
Compression removes this redundant part before the call goes to the model. The model reads less input, and you pay for fewer input tokens.
- It is set per API key in the app, when you create or edit the key.
- Without compression your tool output stays as you send it.
What is removed
Only the texts of tool results change. The system prompt, your messages, the assistant's own text and tool calls, and images stay as the client sent them.
- Lossless replaces tool output the model has already seen exactly like that further
up with a short reference to the earlier copy, such as
[lines 1-80 same as in the earlier Read src/store.rs above, not repeated here]; when several calls share that name, the reference also names the call's id or its position (the 2nd-last, say). It also drops the agents' fixed framing text (headers, empty fields); exit codes and errors stay. The content is still in the conversation. - Lossy does the same and also shortens old tool output the work has moved past: long command output to its start, end, error lines and the lines the assistant referred to later; files read earlier to an outline or, when the file was changed afterwards, to a note to read it again. The newest tool results are never shortened; they only lose the agents' fixed framing text too.
- Both set references and shorten output only when the saving outweighs the prompt cache rebuild this causes. That is why they often leave early and small savings out. The agents' fixed framing text is always dropped.
What to look out for
- Only calls to
/v1/chat/completionsand/v1/messagesare compressed. Responses, text completions and embeddings are not compressed. - If the result can't be written back into your request without changing anything but tool texts, the call goes out uncompressed and shows as Failed on the usage page.
- A call also goes out uncompressed when its request is over the compression budget (more than 16 MiB or an unusually large number of JSON values) or no compression capacity was free within a short wait. It shows as Failed without context sizes (0 before and after).
- The usage your client sees in the response counts the removed input tokens by default. That way agents see the real size of their context and compact it at the same point as without compression. You are billed for the tokens the provider reports for the compressed call.
- The app's dashboard shows how much context was saved in the chosen period; the usage page shows per call how large the context was before and after.
- Prompt caching keeps working: your cache markers stay on routes that speak the API you called, and earlier parts of the conversation stay the same from call to call. An earlier part only changes when a repeat is folded or an old output shrunk, and only when the saving outweighs rebuilding the cache from there.
- When you ask an agent to show the full output of a command it already ran with the same result, it sometimes takes the reference to the earlier output for a truncation and runs the command once more. That costs one or two extra calls.
Risks
- With lossy compression the model no longer sees old output verbatim. It then reads a file again or runs a command once more, which costs extra calls. Rarely it bases an edit on text it no longer sees; the edit then fails and is retried.
- An agent's path can differ from runs without compression.
- On research tasks where an agent fetches many web pages, lossy compression can cost more than it saves: once a page fetched earlier is shortened, the agent fetches it again, and every fetch is one more call with new content. For such tasks lossless compression is the safer choice.
- Where old output must stay visible verbatim, for example for audits or verbatim transcripts, choose lossless compression or none.
- Compression takes a little time per call before the request goes to the model.