← Journal
Tool output costs more than you expect. Cut tokens with headroom

Tool output costs more than you expect. Cut tokens with headroom

I tested headroom with the network blocked and compressed agent tool output before sending it to an LLM. Plain text produced no savings. JSON produced a 52.9% reduction.

Repository covered in this article

headroomlabs-ai/headroom

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

59.4k Python Apache License 2.0

A coding agent that reads search results or build logs incurs a cost from the text itself. File lists, repetitive logs, and JSON responses with similar structures all add to that cost. The agent passes the full output to the LLM. The extra tool output can cost more than the listed price. headroom compresses that output before the LLM receives it. I installed the library and measured the reduction.

headroom is a Python library that compresses tool output, logs, and RAG chunks before an LLM receives them, reducing token use. The project publishes its source under the Apache 2.0 License (*1).

1. Install it

FROM python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'headroom-ai==0.31.0'
ENV TIKTOKEN_CACHE_DIR=/opt/tiktoken-cache
RUN python -c "import tiktoken; tiktoken.encoding_for_model('gpt-4o')"
WORKDIR /work
podman build -t headroom-sandbox .

The tokenizer used by the dependency (tiktoken) fetches an encoding definition for each model name from the network at first use. During the build, with the network available, I made one call and set TIKTOKEN_CACHE_DIR to a fixed path. Without that cache, an offline run cannot fetch the definition, so the compression process cannot run.

2. A roadblock: plain text gets no compression

I started with repetitive plain text.

from headroom import compress

noisy = "\n".join(
    f"[INFO] step {i}: processing item_{i} status=ok elapsed_ms={100+i}"
    for i in range(400)
)
result = compress([{"role": "user", "content": noisy}], model="gpt-4o")
print(f"tokens_saved={result.tokens_saved}")
print(f"compression_ratio={result.compression_ratio:.3f}")
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
  -e HEADROOM_OFFLINE=1 -e HEADROOM_TELEMETRY=off -e HEADROOM_UPDATE_CHECK=off \
  -v "$(pwd)":/work -w /work headroom-sandbox python demo.py

The -v option lets the container read and write the specified directory. Run this in an empty directory reserved for the experiment. HEADROOM_OFFLINE=1 tells headroom to disable all of its background network traffic, including update checks. Use it with --network none as a second safeguard.

The command produced:

tokens_saved=0
compression_ratio=0.000

The result showed no token savings. The code worked as written. The input shape caused the result. headroom's documentation describes the core compressor, SmartCrusher, as targeting JSON arrays of dictionaries. It does not detect repetition in arbitrary plain text. I needed to test the format that agent tool output uses in practice: JSON arrays from search results and API responses.

3. Try compression on a JSON array

I switched to a JSON array that mimicked the repeated structure of tool output.

import json
from headroom import compress

records = [
    {"path": f"src/module_{i}/handler.py", "line": 42, "status": "ok", "size_bytes": 1024 + i}
    for i in range(300)
]
tool_output = json.dumps(records)

result = compress([{"role": "tool", "content": tool_output}], model="gpt-4o")
print(f"original_chars={len(tool_output)}")
print(f"tokens_saved={result.tokens_saved}")
print(f"compression_ratio={result.compression_ratio:.3f}")

I ran the same podman run command with network access blocked. The command produced:

original_chars=25990
tokens_saved=5086
compression_ratio=0.529

The 300 records shared a near-identical structure, and the result showed a 52.9% reduction in token count. I left --network none in place, and the test made no additional LLM API calls. The compressor used local structural analysis alone.

Beyond the core compressor, headroom includes a code-specific compressor that supports multiple languages through AST analysis, image compression, and context sharing among multiple agents. This trial measured the core compression effect.

Trim tool output before you pass it on

I look at an agent's input shape as well as its contents before deciding what it should read. The same information can consume different numbers of tokens depending on whether you pass it to an LLM as plain text or prepare it for compression first. You can add a step that trims tool output before you pass it to the LLM.

Sources

*1 headroom (GitHub), https://github.com/headroomlabs-ai/headroom, Apache 2.0 License