The gateway trims a request's conversation history before it reaches the provider, removing tool results a fast classifier judges no longer necessary. It never summarises: user and assistant text comes out byte-identical. It ships off.
On the way out, before the request reaches the provider, the gateway reads the conversation history and pairs each tool call with its result. For every pair a classifier answers two questions: does this call still need to be here, and does its full result still need to be here. What the classifier marks as no longer necessary is removed. Everything else goes through untouched.
Client (Agent, CLI or application) -> Gateway (Pairs calls and results, consults the engine, rewrites) -> Provider (Receives a shorter history, with the same prose). Compaction happens on the outbound path of a single request. Nothing is stored, nothing is rewritten on the client side.
The guarantee: If you compare the request that left the gateway with the one that came in, the difference is always a subtraction. Nothing in the output was written by the compaction engine.
Who decides can be a model the customer runs on their own machine (the local engine) or a hosted decision service (the remote engine). Both speak the same wire protocol, so the only difference in configuration is the endpoint. Nothing else in the block changes.
Compaction is off in a fresh install. With no configuration block and no environment variable turning it on, the gateway never calls any engine and the history goes to the provider exactly as it arrived. Turning it on is a deliberate act.
Once on, every failure path ends in the same place: the ORIGINAL history is sent.
The rule: No request is ever broken to save tokens. Compaction is an optimisation, so any doubt resolves in favour of sending everything.
The configuration field for the engine credential holds the NAME of an environment variable, never a pasted value. The gateway reads the variable at startup. A configuration file that carries the secret itself is refused, with a message naming the field, so a credential never reaches a backup, a repository or a support ticket by accident.
"api_key_env": "ATLASBERG_COMPACTACAO_API_KEY" ok: the name of the variable
"api_key_env": "sk-live-9f2c..." refused: a value in the fileThis is the part that decides whether compaction is worth anything at all, and it is not intuitive. In a long agent session, most input tokens are not read at full price: they are cache reads, billed at 0.1x. The prefix of the conversation stays stable turn after turn, so it stays in the cache and costs almost nothing to send again.
Compaction rewrites that prefix. A new prefix is a cache miss, and entering the cache costs 1.25x. So every compaction starts in the negative: it pays 1.25x once, on a smaller history, to then read at 0.1x for as long as the new prefix survives. It only turns a profit if enough turns happen after it.
Before turning compaction on, the gateway can measure what compaction WOULD save on the customer's own traffic without compacting anything. It runs the engine alongside real requests, records what it would have removed and what that would have been worth, and sends every request untouched. No behaviour changes while it is measuring.
Start here. Traffic profiles differ enormously: a workload of short conversations with few tool calls has almost nothing to remove, and a measurement week costs a few cents to find that out. The numbers below came out of exactly this mode.
The variable works with the feature still off. What it produces are counts, never text: how many calls would have become questions, how many tokens would have left the history and what that would be worth. No prompt content reaches the metric or the trail.
ATLASBERG_COMPACTACAO_MEDIR=1From 30 real agent sessions, 6,892 turns and 250 real consultations to the engine:
The two numbers at the bottom belong together. 40.6% of the removals deleted material the conversation later mentioned again, which sounds alarming until you look at what was removed: 97.6% of everything removed came from deterministic tools, a file still on disk, a command that runs again with the same output. The material is recoverable by repeating the call. That is why the maximum cost of a classifier error is one repeated tool call, and why the worst case above is US$ 4.32 and not a broken session.
Two limits, stated plainly: The cost model covers only the input side of the conversation. And task-completion quality with and without compaction was NOT measured: nothing here says the agent finished the same work equally well.
Compaction only ever addresses part of the bill, and it is worth knowing which part before expecting a number from it.
Recommended order: measure first on your own traffic, read the reduction against the fixed prefix you cannot touch, and only then set enabled to true, starting with the local engine.
"compactacao": {
"enabled": true,
"engine": "local",
"endpoint": "http://127.0.0.1:8080",
"model": "<decision model>",
"api_key_env": "ATLASBERG_COMPACTACAO_API_KEY",
"limiar_tokens": 40000,
"piso_bytes": 2048,
"preservar_recentes": 6,
"limiar_decisao": 0.7,
"truncar_cabeca_chars": 800,
"timeout_ms": 1500,
"min_reducao": 0.1,
"consentimento_exportar_prompt": false
}Atlasberg Platform is the control layer between a company and every AI model: each request is authenticated with a virtual key, filtered by policy and DLP, routed to the right provider and written to a hash-chained audit trail. It exposes an OpenAI-compatible API, so applications only swap the base URL.
The same artifact runs in Atlasberg Cloud, in your VPC, on-premises or fully air-gapped, and is priced by capacity and modules, never per seat.
This page is part of the official documentation. To talk to the engineering team, write to [email protected] or use https://atlasberg.com/contato. Answers come within one business day.
Agents: this page is also available as Markdown at /docs/console/compactacao-de-contexto.md, or by requesting this URL with the header Accept: text/markdown. Index of everything: /llms.txt.