Headroom prompt compression
Headroom is a prompt compression service. The gateway's Headroom integration runs as a guardrail that sends the prompt to Headroom before the model call and forwards the compressed version, which cuts input tokens and cost on long conversations and agent loops with large tool results. Compressed content can be recovered by the model through a retrieval tool, so details that were compressed away are still available when the model asks for them. Applying compression to every request is part of the free gateway, and turning it on per key or team needs an enterprise license.
A real-world scenario
A coding agent runs thirty tool calls per task and each one returns a few thousand tokens of file contents, most of which the model never needs again. The admin adds a Headroom compression guardrail that applies to all requests. The agent's prompts shrink by a large share of their tokens, the model fetches the odd compressed file through the retrieval tool when it really needs it, and the Compression savings tile and the Savings chart on the Overall tab of the Cost Optimization page show the tokens removed and the money saved, so the team can judge the trade before widening the rollout.
Step by step
In the Admin UI
- Open
/ui/cost-optimizationas a proxy admin and pick the Prompt Compression tab. - In Add Headroom compression guardrail type a Name, for example
headroom-compression, and the Headroom API base of your compression service. The gateway calls its/v1/compressendpoint. - Leave Apply to all requests on to compress every request, or turn it off to create the guardrail as opt-in for keys, teams or single requests.
- Leave Refuse requests when Headroom is down off, its default, so requests go to the model uncompressed while Headroom is unreachable and only the savings stop. Turn it on only when an uncompressed prompt would break a context window or a cost limit, because every request on the gateway is then refused with a 502 for as long as Headroom is down.
- Click Add guardrail. A toast reads Compression guardrail created and the guardrail appears in the list above with an Always on or Opt-in badge.
- Set the
HEADROOM_API_KEYenvironment variable on the gateway, or edit the guardrail under/ui/guardrailsto pointapi_keyat a secret. - Return to the Overall tab after traffic flows to read Compression savings and the number of tokens compressed.
With curl
The same guardrail in config.yaml.
guardrails:
- guardrail_name: headroom-compress
spanway_params:
guardrail: headroom
mode: pre_call
api_base: os.environ/HEADROOM_API_BASE
api_key: os.environ/HEADROOM_API_KEY
unreachable_fallback: fail_open
default_on: true
Create it over the API, then send a request that opts in when default_on is false.
curl -X POST http://localhost:4000/guardrails \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{"guardrail": {"guardrail_name": "headroom-compress", "spanway_params": {"guardrail": "headroom", "mode": "pre_call", "api_base": "https://headroom.internal", "api_key": "os.environ/HEADROOM_API_KEY", "default_on": false}}}'
curl -i -X POST http://localhost:4000/v1/chat/completions \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "claude-sonnet-5-5", "messages": [{"role": "user", "content": "Here is the full log ..."}], "guardrails": ["headroom-compress"]}'
Expected result
The request is answered as usual, with the model seeing the compressed prompt. The spend log row records the input tokens actually billed and the tokens compression removed, and the Compression savings tile and the Savings chart on the Overall tab add them up at the model's input rate. The Prompt Compression tab lists the compression guardrails and their badges. When the model calls the Headroom retrieval tool, the gateway answers the tool call with the original content and the conversation continues without the client noticing.
Settings
| Setting | Default | Meaning |
|---|---|---|
api_base | HEADROOM_API_BASE | URL of the Headroom compression service. |
api_key | HEADROOM_API_KEY | API key for the service. |
model | none | Model name forwarded to the compression endpoint. |
unreachable_fallback | fail_closed in config.yaml and over the API, fail_open from the Admin UI form | What to do when Headroom is unreachable or errors. fail_closed refuses the request with a 502, fail_open logs a critical line and forwards the request uncompressed. |
timeout | 60 | Seconds the gateway waits for one call to Headroom's compress or retrieve endpoint. Zero, negative and non numeric values are ignored with a warning and 60 is used. |
default_on | false | true compresses every request on the gateway. false compresses only requests whose key, team or body names the guardrail. |
ccr_retrieval | true | Add the Headroom retrieval tool so the model can fetch compressed content by its hash. |
mode | pre_call | Use pre_call. post_call is accepted, but answers are never compressed, so a post_call guardrail does nothing. |
With default_on: true the guardrail runs on every request. Otherwise attach it to specific keys or teams with their guardrails field, or let clients request it per call with "guardrails": ["headroom-compress"] in the body.
The guardrail never compresses system messages, the latest user message, earlier assistant replies, or tool results the model already fetched back through the retrieval tool, so instructions and the current question are never altered. Older user messages and tool results are what gets compressed.
Two kinds of request are always sent uncompressed. A client can skip compression for one call, even on an always on guardrail, by sending the header x-headroom-bypass: true, and background requests ("background": true on the Responses API) are never compressed. So savings can be lower than expected when clients send the header.
Built-in compression
The gateway also has its own compression pass, with no outside service, for Anthropic Messages requests (/v1/messages, which Claude Code uses). When a request is longer than compression_trigger tokens, it shortens older content down to about compression_target tokens, keeps the originals for the conversation, and gives the model a spanway_content_retrieve tool to fetch any part it needs. Its savings are added to the same Compression savings tile.
spanway_settings:
callbacks: ["compression_interception"]
compression_interception_params:
enabled: true
compression_trigger: 200000
| Setting | Default | Meaning |
|---|---|---|
enabled | true | Off leaves every request as it is. |
compression_trigger | 200000 | Requests below this many input tokens are never touched. |
compression_target | 70 percent of the trigger | The size the pass aims for. |
embedding_model, embedding_model_params | unset | An embedding model that helps pick what to keep. Without one the pass ranks content by keyword match only. |
Errors
With unreachable_fallback: fail_closed a Headroom problem refuses the request with a 502 whose body is {"detail": {"error": "<message>", ...}}. The extra fields are status_code and the first 500 characters of Headroom's answer in body when Headroom answered, hint for a 404, detail for a connection error, and sent and returned when the message count changed. With fail_open none of these reach the caller, and the log has a CRITICAL line instead.
| What you see | Why | Fix |
|---|---|---|
502 Headroom compression service unreachable | The gateway could not connect to Headroom, or Headroom did not answer within timeout. | Check api_base and the network path from the gateway, raise timeout, or set fail_open while you investigate. |
502 Headroom compression service returned an error with status_code 401 | HEADROOM_API_KEY or the guardrail's api_key is missing or wrong. | Set the variable or the guardrail's api_key. |
502 Headroom compression service returned an error with status_code 404 and a hint | The compression endpoint was not found. Self hosted Headroom may need remote compression turned on, for example HEADROOM_COMPRESS_ALLOW_REMOTE=1. | Check api_base and the Headroom deployment. |
502 Headroom compression service returned an error with another status_code | Headroom failed. | Read body and Headroom's own logs. |
502 Headroom compression service returned non-JSON response, ... returned unexpected response shape, ... response missing 'messages' or ... returned empty message list | Headroom answered with something other than a list of messages, often because api_base points at a proxy or a web page. | Point api_base straight at Headroom and check its version. |
502 Headroom compression service changed the message count | Headroom returned a different number of messages than it was sent, so the gateway cannot put the protected messages back in place. | Upgrade Headroom. |
| A critical log line saying Headroom failed and the request was forwarded uncompressed | fail_open did its job. | Fix the Headroom service. Requests keep working at full cost meanwhile. |
400 Guardrail configuration error: Headroom guardrail requires an API base URL. ... from POST /guardrails | No api_base was sent and HEADROOM_API_BASE is not set. | Send api_base, or set the variable. |
| API base is required or Name is required on the form | A required field is empty. | Fill it in. |
| Compression savings stay at zero | The guardrail is opt-in and nothing opts in, prompts are too short to compress, or clients send x-headroom-bypass: true. | Turn on Apply to all requests, add the guardrail to the keys and teams that send long prompts, or ask clients to drop the header. |
| Per key or per team compression is refused | That needs an enterprise license. | Apply the guardrail to all requests, or add the license. |
See the error reference for the same messages next to every other gateway error.
Logs
| Level | Line | Meaning | Action |
|---|---|---|---|
| CRITICAL | Headroom: <error>; fail_open configured, forwarding request uncompressed. status_code=<n> | Headroom failed and fail_open sent the request through at full size. | Fix Headroom. |
| WARNING | Headroom: ignoring unusable timeout <value>, using 60.0 seconds | timeout is not a positive number. | Fix timeout. |
| WARNING | Headroom: retrieve failed for hash=<hash>: <error> or Headroom: retrieve returned <status> for hash=<hash> | The model asked for compressed content and Headroom could not return it. | Check Headroom when it repeats. |
| WARNING | Headroom CCR: rejecting hash=<hash> not produced by current request compression | The model asked for content this conversation never compressed, which the gateway refuses. | None. |
The log reference lists the same lines.
Related
- Guardrails explains how guardrails attach to keys, teams and requests.
- Cost optimisation and the Cost Optimization page show the savings.
- Prompt caching is the other way to cut the cost of long prompts.
- Enterprise lists the licensed features.