CH 53 / 86Chapter 7: The API and Building Your Own Agents

Optimizing API Costs

On this page9 items

In 10 seconds

Caching cuts costs to one-tenth, batching halves them, and model choice makes a 5-10x difference.
All three can be combined. But the most effective thing is to reduce how much you send.

The API is pay-as-you-go. When you run the same task 10,000 times, your choices show up directly on the bill.

A quick review of pricing#

As of August 2026, per million tokens (No.08):

ModelInputOutput
Claude Fable 5$10$50
Claude Opus 5$5$25
Claude Sonnet 5$2$10
Claude Haiku 4.5$1$5

Output costs exactly five times as much as input. And the gap to Haiku is 5x to 10x.

Three mechanisms#

They can be combined

Prompt caching. If you send the same preamble every time (a system prompt, a long document, the conversation history), reads from the second time on cost one-tenth of the input price.

Writes carry a multiplier.

OperationMultiplierLifetime
Writing a 5-minute cache1.25x input5 minutes
Writing a 1-hour cache2x input1 hour
Reading0.1x inputSame as the write

The break-even point is clear: the 5-minute cache pays for itself after one read, and the 1-hour cache after two reads.

Batch processing. If you submit large volumes of non-urgent work together, both input and output are half price. Overnight aggregation, bulk classification, batch translation. If you can wait, there’s no reason not to use it.

Model choice. This is where the price gap is biggest. First build something that works with a more capable model, then test whether a lighter model is enough. This order is the safe one. If you start from the other end, you can’t tell whether a failure comes from the model or from your design.

All three can be combined. Batch and cache discounts apply at the same time.

Even a million tokens isn’t more expensive#

This is stated clearly in the official documentation, and not knowing it can cost you.

Claude 4.6 and later models can use a 1-million-token context at standard pricing. A 900,000-token request and a 9,000-token request cost the same per token. Cache and batch discounts also apply as usual across the whole range.

Working with long documents does not make things pricier per token.

Other costs#

Things you pay for besides tokens:

  • Web search: $10 per 1,000 searches
  • Web fetch: no additional charge (you pay only for the tokens of the fetched content)
  • Code execution: free when used together with web search or web fetch. On its own, the first 1,550 hours per month are free, then $0.05 per hour per container
  • Fast mode: $10 input and $50 output on Opus 5 / 4.8
  • US-only inference: 1.1x the price

The most effective lever#

Before any of the three mechanisms, there is this: send less.

  • Pass only the relevant few pages of a PDF, not the whole thing (No.03)
  • Don’t hand over too many tools. Tool definitions themselves consume tokens (No.20)
  • Don’t let conversations pile up endlessly. The API sends the full text every time (No.51)
  • Lower the thinking depth for simple tasks

Halving what you send is faster than stacking discounts. It’s obvious, but easily forgotten.

How to estimate#

The order to follow when building something new:

  1. Actually run one item and measure the tokens (they appear in usage in the response)
  2. Multiply by the number of items
  3. Subtract the parts where caching and batching apply
  4. Allow about 2x headroom

Step 4 matters. Retries, failures, inputs longer than expected. If you estimate at twice the measured amount, you won’t get caught short later.

Try it: estimate the cost of your own workload (5 min)

Prompt
“`
Please estimate the cost of building the following workload with the Claude API.

  • Task: (describe it here)
  • Volume: (per day or per month)

Calculate in this order:
1. Estimated input and output tokens per item (with a breakdown)
2. Monthly total after multiplying by the volume
3. The parts where prompt caching applies, and the savings
4. Whether batch processing can be used, and the savings

I’ll specify the prices: Sonnet 5 at $2 input and $10 output (per million tokens)
“`

Expected result: A four-step calculation comes back. You’ll see that the part caching applies to is surprisingly large.

If it doesn’t work: Always supply the prices yourself. If Claude writes them from memory, you’ll get outdated numbers (No.18).

Common pitfalls#

Using caching for short preambles. Writes carry a multiplier, so short ones actually end up costing more. Caching pays off for long preambles.

Not using batching when nothing is urgent. It’s half price. If it can run overnight, run it overnight.

Starting with a light model. You lose the ability to isolate problems. Get it working with a capable model, then step down.

Writing prices from memory. They change. Always check them against the official table.

Summary#

  • Cache reads cost one-tenth of input. The 5-minute cache pays off after one read, the 1-hour cache after two
  • Batching halves both input and output. Use it if you can wait
  • Standard pricing even at a million tokens. Long documents don’t cost more per token
  • Reducing what you send is faster than discounts

Read next#

Building Your Own with the Agent SDK