Cut API Costs with Prompt Caching
Harry
· 15 Oct 2026
· 3 views
Log in to track your progress and mark lessons complete.
Sponsored
Why Caching Matters
Repeated system prompts, long documents and few-shot examples are re-processed on every call. Prompt caching stores them once and reuses them, cutting latency and up to 90% of input cost on repeats.
Practical Setup
response = client.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=1024,
system=[{
"text": LONG_STABLE_PROMPT,
"cache_control": {"type": "ephemeral"}
}],
messages=[{"role": "user", "content": question}]
)Rules
Put stable content first, changing content last: cache breaks on the first difference. Cache system prompts, docs and examples; never cache the user question. Watch the cache-hit rate in the response usage fields.