DEV Community

Cover image for Cutting Claude Code token costs: where the tokens actually go
AI Prime Tech
AI Prime Tech

Posted on

Cutting Claude Code token costs: where the tokens actually go

If you've run Claude Code for a full working day, you've seen the bill. Agentic loops are
token-hungry in a way normal chat never is — every file read, every tool result, every diff lands in
context and gets re-sent on the next turn. Here's where the tokens go and how to stop the leak.

Output tokens are the expensive ones

On every frontier model, output tokens cost 4–5× input. A verbose model that "thinks out loud" in the
final answer burns money faster than one that's concise. Cap max_tokens deliberately, and prefer a
model that answers tightly for routine work.

Quick way to see the shape of a task's cost before you run it — this reads a live model catalog and
prints per-model cost for the token counts you expect:

python3 cost_estimate.py --in 20000 --out 2000
# MODEL                CTX          COST (USD)
# Claude Haiku 4.5     200K            $0.0120
# Claude Sonnet 4.6    200K/1M         $0.0270
# Claude Opus 4.8      200K            $0.0900
Enter fullscreen mode Exit fullscreen mode

(The script is in an open repo: https://github.com/iskandaryv/anthropic-gateway-examples)

The four levers that actually move the bill

  1. Route by task. Classification, extraction and routing don't need Opus — Haiku does them at a fraction of the price. Reserve the flagship for reasoning-heavy work. Full comparison: all Claude models compared.
  2. Prompt caching. Repeated system prompts and stable prefixes are re-read from cache at ~10% of input price on supported endpoints. For agentic tools this is the single biggest lever.
  3. Trim context each turn. Don't resend the whole transcript; keep the last few turns plus a running summary. Unbounded context growth is what turns a $2 session into a $20 one.
  4. Cap the loop. Set a hard step limit on autonomous runs so a stuck agent can't spin forever.

The gotcha almost everyone hits

If ANTHROPIC_API_KEY is set in your shell, Claude Code bills that key at API rates and silently
ignores your Pro/Max subscription
. Unset it when you mean to use the subscription. (More setup
traps in the Claude Code API costs guide.)

Takeaways

  • Watch output tokens, not prompt length — that's where the money is.
  • Route cheap-first, escalate only when quality demands it.
  • Turn on prompt caching for anything with a stable prefix.
  • Estimate before you run; measure tokens in/out after.

Disclosure: I run ClaudeAPIKey.dev, an Anthropic-compatible API
gateway, so read the pricing notes as coming from an interested party. The token-accounting advice
above applies identically on the official API. "Claude" is a trademark of Anthropic; we are not
affiliated with Anthropic.

Top comments (0)