In this categoryTroubleshooting · 27
- How to Fix an OpenAI 401 'Invalid API Key' ErrorStart
- How to Fix OpenAI 429 Rate Limit Errors With Backoff
- How to Fix Anthropic Claude API 401 Authentication Errors
- How to Fix Google Gemini 'API Key Not Valid' Errors
- How to Fix an API Key That Loads as Undefined
- How to Handle Anthropic 529 'Overloaded' Errors
- How to Fix Rate Limit Errors from an AI API
- How to Fix CORS Errors When Calling an AI API From the Browser
- How to Fix 'Model Not Found' and Deprecated Model Errors
- How to Rotate a Leaked API Key Without Downtime
- How to Fix SSL Certificate Errors When Calling AI APIs
- How to count tokens before sending a prompt to Claude
- How to Fix 'Context Length Exceeded' Token Limit Errors
- How to fix a context length exceeded error in the Claude API
- How to fix a Claude response that gets cut off mid-sentence
- How to reduce Claude hallucinations by grounding answers in your documents
- How to keep a long Claude conversation under the context limit
- How to stop Claude from calling tools when it should not
- How to handle a Claude Fable 5 refusal with a fallback model
- How to choose the right Claude model for cost and quality
- How to cut Claude API costs with prompt caching
- How to halve Claude costs for bulk jobs with the Batch API
- How to cap spend on a Claude agent with a task budget
- How to Debug an MCP Server That Will Not Connect
- How to Ask an Agent to Explain a Bug Before Fixing It
- How to Roll Back a Bad Deploy Quickly
- How to Fix Cursor Not Indexing Your Codebase
How to cut Claude API costs with prompt caching
Cache a large stable prefix so repeated requests pay roughly a tenth of the input price for the cached part.
If many of your requests share a large fixed chunk, such as a long system prompt, a document, or a set of examples, you are paying full input price for the same tokens over and over. Prompt caching stores that prefix so later requests read it at roughly a tenth of the cost. This guide shows how to add caching correctly and verify it is actually hitting.
- The Anthropic SDK and an API key
- A request with a large prefix that repeats across calls
- A prefix above the cacheable minimum (4096 tokens on Opus, 2048 on Sonnet and Fable)
Step 1: Understand the one rule
Caching is a prefix match. Any byte change anywhere before a breakpoint invalidates the cache from that point on. Render order is tools, then system, then messages. So put stable content first and volatile content, like timestamps or the user's question, last.
Step 2: Add a cache breakpoint
Mark the last block of the stable section with cache_control. Here the large system prompt is cached, and the per-request question stays uncached at the end.
from anthropic import Anthropic
client = Anthropic()
BIG_CONTEXT = open("handbook.txt").read() # the same every request
resp = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
system=[{
"type": "text",
"text": BIG_CONTEXT,
"cache_control": {"type": "ephemeral"},
}],
messages=[{"role": "user", "content": "What is the PTO policy?"}],
)Step 3: Verify the cache is working
Read the usage object. On the first call you pay a write, shown as cache_creation_input_tokens. On the second identical call you should see cache_read_input_tokens populated and input_tokens drop to just the uncached question.
print("created:", resp.usage.cache_creation_input_tokens)
print("read: ", resp.usage.cache_read_input_tokens)
print("uncached:", resp.usage.input_tokens)Step 4: Know the break-even
A cache write costs about 1.25 times normal input for the default five minute window, and a read costs about a tenth. With the five minute window you break even at two requests. The one hour window costs about two times to write, so it needs at least three reads to pay off, but it survives longer gaps in traffic.
Result: the 12,044 token handbook moved from full price on every call to a cache read on every call after the first. Across a day of repeated policy questions the input bill dropped by roughly 85 percent on the cached portion.
Watch related tutorials
1:42:18
28:14
41:09
9:47
8:23
52:31New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.