Tools
Claude API, Claude Code, RTK, Headroom
Price
Free settings, open-source tools
Time
30 min
Level
Intermediate

You have connected an AI agent to your shop or your customer service. It answers well, but the API bill climbs every month while the number of customers does not. Look closely and most tokens (the chunks of text the model bills for) are not spent on thinking: they are the same instructions sent with every exchange, logs, and order lists in JSON format. Here, in order, is what I adjust before touching anything else.

1. Measure before cutting

In Claude Code, /usage shows the session's consumption and, in recent versions, the share of tokens served from the cache. /context shows what fills the agent's working memory. On the API, each response reports cache_read_input_tokens and cache_creation_input_tokens: if both stay at zero, nothing is cached.

Prompt to copy
Here is the output of /usage and /context from my session. Rank what uses the most tokens, largest first. Suggest three concrete changes to reduce usage without lowering answer quality, and say for each one what I risk losing.

2. Prompt caching (official setting)

Caching keeps the fixed start of your requests (instructions, catalogue, documentation) on Anthropic's side so it is not billed at full price again. According to Anthropic's documentation, a cache read costs 0.1 times the normal input token price (0.05 times on Claude Opus 5.5 and Sonnet 5.5). Writing costs a little more: 1.25 times for a 5-minute cache, 2 times for one hour. Caching is turned on with a single field in the request:

API request (JSON)
{
  "model": "claude-sonnet-5-5",
  "max_tokens": 1024,
  "cache_control": {"type": "ephemeral"},
  "system": "You are the customer service assistant for the shop…",
  "messages": [{"role": "user", "content": "Where is my order?"}]
}

Two conditions: the fixed part must come first and stay identical from one call to the next (a date or an ID slipped into the instructions breaks the cache), and it must pass a minimum size, 512 tokens on recent models. Claude Code uses caching automatically.

3. The right model for each task (official setting)

Public prices per million tokens, as of 17/10/2026, on Anthropic's pricing page:

  • Claude Opus 5.5: $4 input, $20 output.
  • Claude Sonnet 5.5: $2 input, $10 output.
  • Claude Haiku 5.5: $0.10 input, $0.50 output, for requests up to 100,000 tokens.

Sorting emails or pulling an amount from an invoice does not need the biggest model. The Claude Code documentation recommends Sonnet for most tasks, Opus for complex decisions, and model: haiku for simple subagents. For jobs that can wait (classifying 2,000 customer reviews overnight), the Batch API charges 50% less on both input and output.

4. RTK: compress command output

RTK is a small program that sits between your coding agent and the terminal: it shortens what git status, tests or ls return before the agent reads it. According to the project, command output gets up to 90% smaller. The README itself points out that the bill does not drop by as much, since that output is only part of the tokens.

  1. Install and checkAnother project has the same name: rtk gain must show a savings dashboard.
    Terminal
    brew install rtk
    rtk gain
  2. Hook it into Claude CodeThen restart Claude Code. RTK only acts on terminal commands, not on the built-in read tools.
    Terminal
    rtk init -g

Licence: the full edition combines Apache-2.0 and BUSL-1.1, the rtk-oss edition is Apache-2.0 only. The project states that production use is free, but reselling RTK as a service is not.

5. Headroom: compress JSON, logs and files

Headroom (Apache-2.0 licence) compresses on your machine everything the agent reads: tool results, logs, files. According to the project, about 20% fewer tokens for a coding agent, and 60 to 95% for highly repetitive JSON, such as an order list returned by an online shop.

Terminal
uv tool install --python 3.13 "headroom-ai[all]"
headroom wrap claude
headroom doctor

headroom wrap claude starts a local proxy (a relay your requests go through) and also installs the Serena tool for that project. headroom unwrap claude undoes it all.

Limits

  • Both tools' percentages come from the projects, measured on their own examples. Compare your /usage before and after a week.
  • Compression that is too aggressive can hide the error line the agent needed. Headroom keeps the original reachable for the agent, but check the first results either way.
  • Habits matter just as much: /clear between unrelated tasks, a CLAUDE.md under 200 lines, precise instructions rather than "improve the code".

Security. A proxy sees all your requests, and a hook rewrites the agent's commands. Install these tools from their official repositories, read what they change in your configuration, and test them on a project without customer data before rolling them out.

Sources

Is your agent costing more than planned? I can audit and tune it as part of my custom AI agents work.