Context compression
Context compression makes long conversations cheaper. Before a request reaches the provider, Peris.ai Router removes tokens the model does not need: terminal noise in command output, repeated JSON keys, and text you already sent earlier in the same conversation. You pay for fewer input tokens and the answer does not change, because every engine is lossless. None of them rewrites what your text says.
It is off by default and set per API key. It pays off most on coding agents and other tools that resend the whole conversation, with file contents and tool output, on every turn. On short prompts it saves close to nothing.
The three engines
| Engine | What it removes | Typical saving |
|---|---|---|
rtk_filter |
Color codes, progress bars redrawn in place, runs of identical log lines and extra blank lines in command and tool output | Large on build, test and install logs |
headroom |
Whitespace in JSON tool output, and repeated keys in arrays of same-shaped objects, which become one list of column names and rows of values | Often 40 to 60 percent on JSON API results |
session_dedup |
An earlier copy of text that is repeated word for word later in the conversation, such as a file read twice. The later copy is kept | Largest on long agent sessions |
You can turn on any combination. They always run in the order above.
What is never changed
- The last message, which is the one being answered.
- System and developer messages.
- A message that calls tools, and every field other than text:
tool_call_id, names, images. - Anything inside a fenced code block.
If anything goes wrong inside an engine, Peris.ai Router sends your request exactly as you sent it. A compressed request is never larger than the original.
Turning it on
In the console, open API keys, pick a key, and tick the engines under Context compression. The key's owner can do this as well as an Admin.
From an MCP client, call compression_settings with the engines you want:
{ "engines": ["rtk_filter", "headroom", "session_dedup"] }
An empty list turns it off. It takes effect from the next request.
Seeing what it does
- Every response from a key with compression on carries
x-perisai-router-compression: the engines that changed the request, ornone. - Logs show, for each request, which engines ran and about how many input tokens and rupiah they saved. When the key logs bodies, the request shown is the one you sent, before compression.
- The usage CSV export has
tokens_savedandsaved_idrcolumns. - The MCP tool
optimize_contextshows what the engines would do to a conversation you give it, without sending or billing anything. - The MCP tool
compression_reporttotals the savings for the key.
Tokens saved are an estimate: Peris.ai Router counts the bytes it removed and converts them at the rate the provider counted for that same request.
Turning it off for one request
Send the header x-perisai-router-compression: off. That request goes through as sent,
and the key's setting stays as it is. Useful when you want to rule compression
out while debugging an odd answer.