Cost Optimization · Claude Code

How to Reduce Claude Code Costs

Claude Code spends most of its tokens on context: instructions, history, files and tool output that it re-sends every turn.

These 12 tips, drawn mainly from Anthropic's own cost guidance, cut that context down, match the model to the work and put limits in place. The last section covers routing Claude Code through an LLM router, and what Anthropic supports.

Updated By AirLLM6 min read

Short answer

Send less context and use less expensive settings for routine work. Run /clear between unrelated tasks and /compact with a focus in long sessions. Keep CLAUDE.md short and turn off unused MCP servers. Use Sonnet by default and Opus only for hard reasoning, and avoid switching models mid-session so the prompt cache keeps working. Check /cost regularly and cap headless runs with --max-budget-usd.

Where Claude Code costs come from

Claude Code is billed either as part of a Claude subscription's usage limits or per token through an API key or a cloud provider. Either way, the cost of a session comes down to the same thing: how many tokens each turn sends and receives, and which model processes them.

Most of those tokens are context, not your prompt. Every turn re-sends the system prompt, tool definitions, CLAUDE.md, the conversation history, file contents and tool output. For a full breakdown, see how coding agents use tokens. Claude Code already helps on its own: Anthropic's documentation says it automatically optimizes costs through prompt caching and auto-compaction.

The tips below come mainly from Anthropic's own cost guidance for Claude Code. Each one either sends less context, uses a less expensive setting for the step, or stops work that isn't going anywhere.

Keep the context small

1. Clear the context between tasks

When you finish one task and start an unrelated one, run /clear. Anthropic's guide points out that clearing costs nothing, while carrying an old conversation forward means paying to re-send it on every new turn.

2. Compact with a focus

/compact summarizes the conversation so far. It accepts instructions, so you can tell it what to keep:

Claude Code
/compact Keep the auth refactor plan and the two failing tests; drop the earlier exploration

Claude Code also compacts automatically. The CLAUDE_AUTOCOMPACT_PCT_OVERRIDE environment variable makes it compact earlier if you want shorter contexts by default.

3. Keep CLAUDE.md short and move procedures into skills

CLAUDE.md loads into every session and stays in context. Anthropic recommends keeping it under 200 lines and moving workflow instructions into skills, which load on demand instead of on every turn.

4. Prefer CLI tools and disable unused MCP servers

Every connected MCP server adds tool definitions to the context. Anthropic's guide suggests preferring command-line tools the agent can already call over MCP servers where they do the same job, and disabling servers you aren't using in the current project.

5. Pre-filter tool output with hooks

A full test log or build output can be larger than the code under change. Claude Code hooks run your own commands at set points, so you can use them to trim output, for example to pass on only the failing tests, before it reaches the model.

Match the model and effort to the task

6. Use Sonnet for most work and Opus for hard reasoning

Anthropic's guidance is to use Sonnet for most coding work and Opus for complex reasoning, switching with /model or --model. The opusplan alias does this split for you: it uses Opus during plan mode, then switches to Sonnet for execution. For simple subagents, setting model: haiku in the subagent definition keeps routine work on a smaller model.

7. Lower the effort level when the task is simple

Higher effort means more thinking tokens. You can lower it with /effort or the CLAUDE_CODE_EFFORT_LEVEL variable, or limit thinking with MAX_THINKING_TOKENS. Save higher settings for tasks that need them.

8. Send verbose work to subagents, and use teams sparingly

Each subagent runs in its own context window, so a noisy job such as searching a large codebase or reading logs doesn't fill up your main conversation. The subagent's tokens still count, though, and Anthropic's guide says agent teams use approximately 7x more tokens than a standard session. Use them when parallel work is worth the cost.

Work in shorter, clearer loops

9. Write specific prompts and plan first

Vague prompts lead to exploration, and exploration means file reads and tool output. Naming the files, the function and the expected outcome cuts that down. For larger changes, plan mode lets Claude Code research and propose a plan before it edits anything, which is a cheaper place to correct course.

10. Back out early with /rewind

Claude Code creates a checkpoint with each prompt. If a change heads the wrong way, /rewind (or pressing Esc twice) restores the code and/or the conversation, instead of spending more turns undoing it. Edits made through Bash commands aren't tracked, and checkpoints don't replace version control.

Keep the prompt cache working

11. Avoid changes that reset the cache

Prompt caching lets the repeated start of each request be billed as cached input instead of full-price input. According to Anthropic, the cache lasts five minutes by default on API keys and cloud providers and one hour on a subscription within plan usage; setting CLAUDE_CODE_PROMPT_CACHE_TTL=1h lengthens it. Switching models, changing the effort level on most models, or connecting an MCP server mid-session forces a fresh cache, so settle those choices early in a session.

Measure and cap spend

12. Watch /cost and set limits

Run /cost, an alias for /usage, to see where a session stands. For API users it estimates the session's cost at list price and shows prompt-cache statistics; subscribers see plan usage bars. For scripts and CI:

  • --max-budget-usd caps what a headless run can spend.
  • claude -p --output-format json returns total_cost_usd, so you can log cost per job.
  • In the Claude Console you can set workspace spend limits, and Team and Enterprise admins can set spend limits for the organization, groups or members.

Routing Claude Code through an LLM router

The tips above all work inside Claude Code. An LLM router for coding agents works one level down, on the requests Claude Code sends. Routing Claude Code through Air Router means pointing ANTHROPIC_BASE_URL at the router and using your AirLLM key as ANTHROPIC_AUTH_TOKEN:

~/.zshrc
export ANTHROPIC_BASE_URL="https://api.airllm.ai/"
export ANTHROPIC_AUTH_TOKEN="your_airllm_api_key"
export ANTHROPIC_MODEL="airllm-anthropic"   # or airllm-auto for cross-provider routing

The Claude Code setup guide has the full configuration, including the model aliases, timeout and non-essential traffic settings. Before you set it up, know where Anthropic stands:

  • Gateways are supported, but not endorsed. Claude Code works with any gateway that exposes a supported API format, but Anthropic doesn't endorse, maintain or audit third-party gateways.
  • Claude models only, officially. Anthropic doesn't support routing Claude Code to non-Claude models through any gateway. airllm-anthropic keeps each request within Anthropic's Claude family while still choosing a suitable Claude model per request. airllm-auto routes across providers, which is outside Anthropic's support.
  • Use the router's key, not your Claude login. With a gateway credential set, Claude Code bills per token to whoever owns that credential, and your subscription's usage limits don't apply. Never route a claude.ai subscription login through a gateway.

What a router adds is automatic: a suitable model for each request, less repeated context before it reaches the model, prompt caches preserved, and protection against unproductive loops. See Air Router for details. Actual results depend on your workload.

A quick checklist

  • /clear between unrelated tasks; /compact with a focus when a session runs long.
  • CLAUDE.md under 200 lines, procedures in skills, unused MCP servers off.
  • Sonnet by default, Opus or opusplan for hard problems, small models for simple subagents.
  • Specific prompts, plan mode for big changes, /rewind when a change goes wrong.
  • No model or effort switches mid-session if you can avoid them.
  • /cost to check, --max-budget-usd and spend limits to cap.

Frequently asked questions

What is the fastest way to cut Claude Code token usage?

Clear the context between unrelated tasks with /clear, and compact long sessions with /compact plus a short note on what to keep. Both stop you paying to re-send history the next task doesn't need.

Does /clear cost anything?

No. Anthropic's cost guide notes that clearing the conversation costs nothing; it just starts the next turn with a fresh context.

Should I use Opus or Sonnet in Claude Code?

Anthropic recommends Sonnet for most coding work and Opus for complex reasoning. The opusplan alias combines them, using Opus in plan mode and Sonnet for execution.

Can I set a hard budget in Claude Code?

For headless runs, --max-budget-usd caps spend. In the Claude Console you can set workspace spend limits, and Team and Enterprise plans support spend limits for the organization, groups or members.

Does Anthropic support using Claude Code with an LLM gateway?

Claude Code works with any gateway that exposes a supported API format, but Anthropic doesn't endorse or audit third-party gateways and doesn't support routing Claude Code to non-Claude models through one. With Air Router, airllm-anthropic keeps requests on Claude models.

Will an LLM router break Claude Code's prompt caching?

It can if the router strips cache fields: Claude Code's docs say a gateway must pass cache_control through, or every turn bills as uncached input. Air Router is designed to preserve prompt caches.

Sources

Route Claude Code through Air Router

Keep Claude Code and your workflow. Change the base URL, key and model, and Air Router picks a suitable model for each request.