Cut API Costs with Prompt Caching

Harry · 15 Oct 2026 · 3 views
Log in to track your progress and mark lessons complete.

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.

Share this post:
AI caption:

Comments (0)

Please login or register to comment.
FREE FOREVER • NO CREDIT CARD

Learn faster with your free account

Join GroovyGrails and unlock everything a guest can't do.

🎓
Earn certificates
Pass quizzes and download verified PDF certificates.
📊
Track your progress
Resume tutorials where you left off, from your dashboard.
🔖
Bookmark & comment
Save tutorials for later and join the discussion.
🚀
Training & updates
Request live training and get new-lesson alerts.
📚 100+ tutorials📝 1000+ lessons💚 100% free
or continue with your account below

🙋 Register Free

Takes 30 seconds. You've enjoyed 3 free tutorials!

Already have an account?