Skip to main content
Version: 1.15.0

Compress long agent context to cut input cost

Agents resend their whole history on every turn: old messages, long tool output, file listings. Most of that text is paid for again on every call. From 1.14.0, DVARA can send the older parts of a long request to a compression engine you run, and send the shorter request to the provider. Every compressed and skipped call is recorded on its cost record, so you can see what compression saved.

Enterprise only

Not included in DVARA Open Source. It runs with or without a licence.

Compression is off by default for every workspace. It changes what the model reads, so a workspace admin turns it on for their own workspace. It also needs an engine: DVARA does not ship one. The operator runs a service that speaks the engine contract and points the gateways at it.

Turn compression on for a workspace​

  1. The operator sets the engine address on every gateway, for example DVARA_LLM_GATEWAY_COMPRESSION_ENGINE_URL=https://compressor.internal.example.com/v1/compress. See Engine settings for the other settings.
  2. The workspace admin opens Cost → Cost Advisor in Flightdeck and finds Shorten long requests before they are sent.
  3. Turn on Compress long requests, check the two numbers below and click Save.
SettingDefaultAllowedWhat it does
Compress requests over (input tokens)80001000 to 2000000Only a request with more input tokens than this, by the gateway's own count, is compressed. A request exactly at the threshold is not.
Share of the older text to keep (0.1 to 0.9)0.50.1 to 0.9Sent to the engine as the target. 0.5 asks it to keep about half of each part it is given.

A value out of range is refused and nothing is saved. The change reaches the gateways with the workspace's other settings and writes WORKSPACE_ADVISOR_CONFIG_UPDATED to the audit trail, with area: compression and the three compression settings before and after.

Only the workspace admin can change these settings. A developer or viewer cannot open the page. The owner and billing-admin see each workspace's saving on the platform Cost Advisor page but cannot turn compression on or off.

See that a call was compressed​

A compressed call's response carries a header with the gateway's input token count before and after:

curl -s -D - https://<gateway-host>/v1/chat/completions \
-H "Authorization: Bearer <your-api-key>" \
-H "Content-Type: application/json" \
-d @long-agent-session.json -o /dev/null | grep -i x-gateway-context-compressed
X-Gateway-Context-Compressed: 12450 -> 6120

A call that was over the threshold but sent as it came has no header. Its cost record says why (see What each call records).

What is compressed, and what never is​

Only text that the model has already seen and is least likely to need word for word is sent to the engine:

  • user and assistant messages before the latest user message;
  • tool results anywhere except the last message;
  • and of those, only text parts of at least 400 characters.

These are never sent or changed:

  • the system prompt and developer messages;
  • the latest user message, the last message, and assistant messages after the latest user message (the current step);
  • any message that holds thinking, and images;
  • tool calls and tool-call ids. No message is added, removed or moved, so every tool call stays paired with its result.

A shorter text that is not shorter than the original is not used.

Which APIs. Chat Completions requests and Anthropic Messages requests on /v1/messages are compressed. A Gemini generateContent body or a Responses API body that the gateway sends on as the caller wrote it is not compressed in this version; it is recorded as skipped with reason native_body.

Claude Code and other Anthropic Messages clients. On an Anthropic route the gateway sends the caller's own /v1/messages body on. Compression then changes only the text slots it shortened, and every other byte of the body goes as sent. Extra rules apply on this path:

  • nothing from the body's last message, or from its last user message (the latest turn with the tool results it answers);
  • no text that carries a cache_control breakpoint, so each breakpoint stays on the text it was put on;
  • no thinking, redacted thinking or signatures, no tool-use input or ids, no tool-result ids, no images and no documents.

Text before a breakpoint can still be shortened, which changes the prompt-cache prefix. An engine that gives the same answer for the same input keeps that prefix stable from turn to turn. If the gateway cannot read the body the same way twice, it plans nothing and records native_body_unread. Token-count requests (/v1/messages/count_tokens) are not compressed.

PII placeholders. When PII tokenization has replaced a value with a placeholder such as {{PII_EMAIL_…}}, the gateway lists every placeholder in the request it sends to the engine. If a shorter text drops, cuts or invents one, the whole answer is refused and the original request is sent, so every placeholder is still restored in the reply.

Where it runs. After policy, PII handling, guardrails, priority and model downgrade, and before the context-window check. So the policy and guardrails judge the request your application sent, and the context-window check measures the request the provider receives. Streamed requests are compressed the same way. Calls the gateway makes for itself, such as an evaluation judge, are never compressed.

Compression never fails a call​

Any problem sends the original request. The engine is an optimisation, not a control:

Reason on the cost recordWhen
no_engineThe workspace turned compression on, but no engine address is set on the gateway
timeoutThe engine did not answer within timeout-ms (2,000 ms by default)
engine_errorThe engine answered with a status other than 200, or the call failed
bad_replyThe engine's answer is outside the contract
egress_refusedThe engine address is a loopback, link-local, cloud metadata or (when not allowed) private-network address
pii_tokens_changedA shorter text dropped, cut or invented a PII placeholder
native_bodyA Gemini or Responses API body sent on as the caller wrote it
native_body_unreadAn Anthropic Messages body the gateway could not read the same way twice
nothing_to_compressNo part of the request qualifies
too_largeThe text to compress is longer than max-characters (2,000,000 by default)
no_gainThe shorter request is not smaller by the gateway's count

A call under the threshold, or in a workspace with compression off, is not recorded at all, and the engine is not called.

What each call records​

Every call over the threshold in a workspace that turned compression on is tagged, so you can compare compressed and uncompressed calls:

Cost record tagValue
dvara.tokenomics.compressioncompressed or skipped
dvara.tokenomics.compression_reasonThe reason from the table above, on skipped calls only
dvara.tokenomics.input_beforeThe gateway's input token count before compression
dvara.tokenomics.input_afterThe count after compression

A caller cannot set these tags: a value sent in the request under the same names is ignored.

The gateways also count every such call in the Prometheus counter dvara_context_compression_total{outcome, reason}. outcome is compressed or skipped; reason is none for a compressed call, else one of the reasons above. A rising share of timeout or engine_error means the engine is adding latency and saving nothing.

See what compression saved​

Both Cost Advisor pages show Saved by context compression for the last 30 days: dollars saved, input tokens not sent, calls compressed, and calls sent as they came, with a row per model (per workspace and model on the platform page). Tokens not sent are priced at the model's input price, from the pricing list or else the model catalogue. A workspace admin sees their own workspace on Cost → Cost Advisor; the owner and billing-admin see every workspace that uses compression on the platform Cost Advisor.

Response cache. The response cache keys on the request as sent, after compression. An engine that always gives the same answer for the same input gets the same cache key and the same hits. One that answers differently each time misses the cache; it never serves a wrong answer. The engine is called before the cache is asked, so a cache hit still costs one engine call.

Write an engine that speaks the contract​

The engine is any HTTP service that answers contract version 1. You can wrap a prompt compressor, or a small model that summarises, behind it.

The gateway sends a POST with a JSON body:

{
"version": 1,
"workspace_id": "ws-payments",
"model": "gpt-4o",
"target_ratio": 0.5,
"segments": [
{"id": "m3b0", "role": "tool", "text": "…4,800 characters of a directory listing…"},
{"id": "m5b0", "role": "assistant", "text": "…an earlier plan the agent wrote…"}
],
"preserve": ["{{PII_EMAIL_9f2a…}}"]
}
FieldMeaning
versionThe contract version, 1
workspace_id, modelThe workspace and the requested model, for your own logs or per-model tuning
target_ratioThe workspace's share to keep
segmentsThe parts to shorten, each with an id, the message's role and the text
preserveStrings that must come back unchanged in every segment that holds them, such as PII placeholders

When dvara.llm-gateway.compression.secret is set, the request carries X-Gateway-Signature: sha256=<hex HMAC-SHA256 of the body>, as guardrail plugins are signed. Check it before you trust the body.

Answer with status 200 and the shorter texts by id:

{
"segments": [
{"id": "m3b0", "text": "…the same listing, shortened to 2,300 characters…"}
]
}

A segment you leave out stays as it was. Any of the following is bad_reply, and the original request goes: an unknown or repeated id, a text that is not a non-blank string, or any other shape. Any status other than 200 is engine_error.

Engine settings​

Set these on every gateway. The engine address is the operator's: a workspace cannot point the gateway at an address. See the configuration reference for the full list.

PropertyEnvironment variableDefault
dvara.llm-gateway.compression.engine-urlDVARA_LLM_GATEWAY_COMPRESSION_ENGINE_URLblank: no engine
dvara.llm-gateway.compression.timeout-msDVARA_LLM_GATEWAY_COMPRESSION_TIMEOUT_MS2000
dvara.llm-gateway.compression.secretDVARA_LLM_GATEWAY_COMPRESSION_SECRETblank: not signed
dvara.llm-gateway.compression.allow-private-networksDVARA_LLM_GATEWAY_COMPRESSION_ALLOW_PRIVATE_NETWORKStrue
dvara.llm-gateway.compression.max-charactersDVARA_LLM_GATEWAY_COMPRESSION_MAX_CHARACTERS2000000

timeout-ms bounds the whole engine call, so it is the most latency compression can add to a request. Keep it low and watch the timeout share of the counter.