---
title: "How to reduce Claude Code costs: what we measured | Anyray"
description: "Where the Claude Code bill comes from, and which changes lowered it in paired runs: a stable prompt cache, fewer turns, smaller tool schemas and cache-safe compression."
canonical: "https://anyray.ai/blog/reduce-claude-code-costs"
last_updated: "2026-11-25"
---

# How to reduce Claude Code costs: what we measured

Research 6 min read

The token count can go down while the bill goes up. Where the money goes in a Claude Code session, and the five changes that lower it.

[Yarden Shechter](https://www.linkedin.com/in/yarden-shechter)

November 25, 2026

**Claude Code cost follows two things: the state of the prompt cache and the number of turns.** About 80% of the cost of a session is cache traffic. So the changes that lower the bill keep the cache stable and remove turns. Removing tokens alone does not: the token count can go down while the bill goes up.

This post shows where the money goes, and five changes that lower the bill. Each number comes from paired runs of Claude Code on [Terminal-Bench 2 tasks](https://anyray.ai/blog/terminal-bench-2-benchmark), direct and through a tool.

## Where the bill comes from

A model API keeps no memory between calls. On each turn, Claude Code sends the full session again: the system prompt, the tool list, each earlier turn and each tool output. The provider caches the start of the request and bills it at a lower price when the next request starts with the same bytes.

Published measurements of Claude Code sessions show the split. About 80% of the cost is cache reads and cache writes. Model output is about 10%. Fresh input is about 1%.

_Figure: Chart. Share of the cost of a coding agent: cache traffic 80 percent, model output 10 percent, fresh input 1 percent, thinking and other 9 percent._

Two prices explain the rest. On Anthropic, a cache read costs 0.1x the input price and a cache write costs 1.25x. A turn that misses the cache pays more than ten times as much for the same text.

## 1\. Keep the prompt cache stable

The cache matches from the first byte. If one byte changes early in the request, the provider writes everything after it again at the higher price. Common causes of a changed prefix:

-   A tool list that changes during the session, for example an MCP server that connects late.
-   A [model switch](https://anyray.ai/blog/model-routing-cache) in the middle of a session. Each switch builds the cache again.
-   A compression tool that rewrites old history on each call.
-   Fast mode turned on or off in the middle of a task. Each speed has its own cache.
-   The `opusplan` setting. Each move into or out of plan mode is a model switch.

Some actions look risky and are free. An edit to a file in the repository, an edit to `CLAUDE.md`, a change of permission mode or output style, a skill, a command and `/recap` all keep the cached prefix.

The cache also expires. With an API key, a cloud provider or a gateway, the default lifetime is 5 minutes, so the first turn after a short break writes the full session again. The setting `promptCacheTtl` with the value `1h` keeps it for one hour. A one-hour write costs 2x the input price, so it pays when breaks are longer than 5 minutes. Behind a gateway, the one-hour request travels in the `anthropic-beta` header, and the gateway must forward that header unchanged.

On this path, a team can also write its own system prompt. A timestamp or another changing value in the system prompt breaks the cache on each request.

A cache bust is a turn that reads less than 90% of what the last turn left in the cache. Claude Code direct had 16 cache busts per 100 trials. Anyray had 8. RTK had 18 and Headroom had 24. [cacheLint](https://anyray.ai/blog/cache-lint) shows which segment changed.

_Figure: Chart. Cache busts per 100 trials: Anyray 8, Direct 16, RTK 18, Headroom 24._

## 2\. Remove turns, not only tokens

Each turn sends the full history again. So one turn less removes a full request from the bill, not a small part of one.

This is where token removal can backfire. When a tool deletes text that the agent still needs, the agent does not guess. It reads the file again, or runs the command again. That is a new turn, and it costs more than the tokens that were saved.

The agent needed 11.4 turns for a task direct and 8.7 through Anyray, which is 24% fewer.

Four habits remove turns and cache writes with no tool:

-   **Use `/rewind` to leave a wrong path.** It goes back to a prefix that is already cached. `/compact` builds a new one.
-   **Run `/compact` at a break between tasks,** while the cache is warm. It costs the most after a long break.
-   **Use `/clear` between unrelated tasks.** It costs nothing, and old context stops going out with each turn.
-   **Give noisy work to a subagent.** Test runs and log reads stay out of the main session, and the cache of the main session does not change.

_Figure: Chart. Turns per task: Anyray 8.7, RTK 10.5, Direct 11.4, Headroom 11.4._

## 3\. Fix the 200k limit behind a gateway

Behind a custom `ANTHROPIC_BASE_URL`, Claude Code budgets a flat 200,000 tokens and compacts at about 95% of that, also on a model with a 1M window. Early compaction rewrites the history, and the provider bills that as cache writes. The setting `_CLAUDE_CODE_ASSUME_FIRST_PARTY_BASE_URL` restores the real window. The post on [Claude Code behind a gateway](https://anyray.ai/blog/claude-code-gateway) has the details and the side effects.

## 4\. Size the tool schemas

Tool definitions go out with each request. Tool search loads them on demand, but it is not free. In paired runs it cost 33% more with a few MCP tools and saved 33% with many. So turn it on only when the tool schemas are large for the model's window. Where a CLI exists, such as `gh` or `aws`, it adds no tool list at all.

## 5\. Compress only with a tool that keeps the cache

Compression lowers the bill when it does three things. It gives the same bytes for the same input on each turn. It keeps code and errors exact. And it lets the agent get removed text back in one call, as [retrieval handles](https://anyray.ai/blog/removed-is-not-lost) do.

_Figure: Chart. Total cost against Claude Code direct: Anyray 32 percent lower, RTK 2 percent higher, Headroom 40 percent higher._

| Arm | Total cost against direct | Tasks cheaper than direct |
| --- | --- | --- |
| Anyray | 32% lower | 95% |
| RTK | 2% higher | 53% |
| Headroom | 40% higher | 11% |

Through Anyray, the agent also solved more tasks: 82% against 76% direct.

## What to measure

-   **Cache read share per session.** If it drops after a change, the change broke the cache. In Claude Code, `/usage` shows the share of input from the cache, the number of misses and the likely cause of the last miss.
-   **Turns per task.** More turns can mean that the agent reads again what a tool removed.
-   **Cost per solved task.** Divide the full cost, failed runs included, by the runs that passed. A cheap run that does not solve the task is not a saving. It was $0.228 through Anyray and $0.367 direct.

## Common questions

### What makes Claude Code expensive?

The session history. Each turn sends it again, and about 80% of the cost is cache reads and cache writes of that history.

### Do token-saving tools lower the Claude Code bill?

Only when they keep the prompt cache stable and do not add turns. In paired runs on Terminal-Bench 2, RTK raised the total cost by 2% and Headroom by 40%.

### What is a cache bust?

A turn that reads less than 90% of what the last turn left in the cache. The provider writes that part again, and a cache write costs more than ten times a cache read.

### How much can a team save on Claude Code?

On Terminal-Bench 2 with Claude Sonnet 5.5, total cost through Anyray was 32% lower than Claude Code direct, and the agent solved 82% of the tasks against 76%.

## Where to go next

-   [Anyray on Terminal-Bench 2](https://anyray.ai/blog/terminal-bench-2-benchmark) The setup, the tasks and all the numbers.
-   [Why context compression breaks prompt caching](https://anyray.ai/blog/prompt-cache-compression) The four rules that keep the cached prefix stable.
-   [Claude Code behind a gateway](https://anyray.ai/blog/claude-code-gateway) The 200k context trap and the env vars that fix it.
-   [The compression engine](https://anyray.ai/compression-engine) What Anyray removes from each agent request, and what stays exact.
-   [Claude Code gateway setup](https://docs.anyray.ai/tools/claude-code) The docs page for connecting Claude Code.

[← Back to blog](https://anyray.ai/blog)

## Ready to cut your LLM bill?
