Skip to contentSkip to page list

Docs

Reduce Coding Agent Cost

Seven Claude Code settings and habits that cut cost and context, how to check each one on your own sessions in LangWatch, and the saving to expect.

On this page

Most Claude Code spend is cache reads and cache writes: across 105 organisations on LangWatch over 30 days, cache reads were 49% of API-equivalent spend and cache writes 32%. The settings below change how much context each call carries, how often it is written again, and which model reads it.

The skill on this page reads your sessions, runs one check per recommendation, and writes a report with the ones your data supports. Each recommendation states its evidence, the settings.json change and the expected saving.

How you pay changes what a saving means

On a Claude subscription a saving is more work before you reach the plan's usage limits. LangWatch shows those sessions in API-equivalent USD, the price the same tokens would have at Anthropic's list prices. On an API key, Amazon Bedrock, Google Cloud, Microsoft Foundry or usage credits, every token is billed, and a saving is cash.

Run the check

Where can my coding agents save tokens?

Install once, then run /coding-agent-cost in your coding agent.

View prompt

Where can my coding agents save tokens?

You are using LangWatch for your AI agent project. Follow these instructions.

Read the full prompt

The recommendations

Measured means a controlled A/B run on the same tasks, described in Run your own cost experiments. Estimated means the same tokens repriced or re-simulated from 30 days of real sessions. Ranges are per organisation where the check fires, as a share of its Claude Code spend.

RecommendationSetting in settings.jsonWho it applies toExpected saving
Use the current model version"model": "opus" (not a pinned older id)88% of organisationsEstimated median 24%, p90 42%. Measured 63% on our bug-fix runs
Compact the main conversation at 400k"autoCompactWindow": 40000079%Estimated median 7%, p90 23%; about 29% on threads past 800k. Measured 51% at 150k on a long reading session, with lost recall
Compact sub-agents past 600ksame autoCompactWindow, and narrower sub-agent tasks18% of those with sub-agentsEstimated 35% of those sub-agents' cost, median 2% of the total
Run built-in sub-agents on Sonnet"env": { "CLAUDE_CODE_SUBAGENT_MODEL": "sonnet" }, plus "CLAUDE_CODE_SUBAGENT_MODEL_FORCE": "1" to include Explore and Plan73%Estimated median 4%, p90 19%
API key: one-hour cache on the main conversation"promptCacheTtl": "1h"pay-per-token users whose cache is rebuilt after pausesEstimated median 11%, p90 25%
Subscription: keep the one-hour main cachedo not set promptCacheTtl to "5m"subscription usersEstimated: a five-minute main cache would cost more for 84 of 101 organisations
Split long sessions/compact before a break, /clear for a new task93%Estimated median 5%

The savings overlap. An old Opus running your sub-agents counts in the first and fourth rows, and a long session counts in the compaction and split rows, so the rows do not add up.

The prompt cache lifetime

Claude Code caches each request for five minutes or one hour. On a subscription within plan usage, the main conversation gets one hour and sub-agents five minutes. On an API key, a cloud provider or usage credits, everything gets five minutes. A one-hour write costs 2x the base input price and a five-minute write 1.25x.

With five minutes, a pause of 5 to 60 minutes expires the cache and the next call writes the whole context again. Applied to 101 organisations' real pause patterns, the one-hour main cache comes out cheaper for 84 of them. For sub-agents it came out cheaper for none of 95, because their calls run back to back. So pay-per-token users should set "promptCacheTtl": "1h" (Claude Code v2.1.242 or later) and leave subagentPromptCacheTtl alone.

To see which lifetime your main conversation uses, run claude -p "hello" --output-format json and read usage.cache_creation: one-hour writes are under ephemeral_1h_input_tokens, five-minute writes under ephemeral_5m_input_tokens.

Put settings in settings.json

A value in the env block of any settings.json wins over a shell export, so the export can be silently ignored. Put each setting in ~/.claude/settings.json, in the project's .claude/settings.json, or in managed settings for a team. Check every level for a conflicting value first, including ANTHROPIC_MODEL, CLAUDE_CODE_AUTO_COMPACT_WINDOW and CLAUDE_CODE_PROMPT_CACHE_TTL in an env block, because each environment variable overrides the setting of the same purpose. The sub-agent model has no setting of its own, so it goes in env.

~/.claude/settings.jsonJSON
{  "model": "opus",  "autoCompactWindow": 400000,  "env": {    "CLAUDE_CODE_SUBAGENT_MODEL": "sonnet"  }}

Pay-per-token users add "promptCacheTtl": "1h".

Check the effect

Run the same check on a window that starts after the change. The sessions list shows peak context, compactions and cost per session, and usage analytics shows the model mix and cache split over time.

Also check: Run your own cost experiments to measure a setting on your own work before rolling it out, and Find your context sweet spot to pick a compaction window from your own sessions.

Documentation