AI / Claude Models Basics Interview Questions
What is prompt caching and how does it reduce costs when using Claude?
Prompt caching allows Anthropic to store a copy of a prompt prefix (such as a long system prompt, documentation, or conversation history) so that subsequent requests reusing that prefix are billed at a much lower rate than re-sending it fresh each time.
| Token type | Cost vs standard input |
|---|---|
| Cache write | ~25% more expensive (one-time cost to store the prefix) |
| Cache read (hit) | ~90% cheaper than standard input tokens |
| Standard input (miss) | Full input price |
# Example: using prompt caching with a long system prompt
message = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
system=[
{
"type": "text",
"text": "You are an expert assistant...[5000 token system prompt]...",
"cache_control": {"type": "ephemeral"} # mark this prefix for caching
}
],
messages=[{"role": "user", "content": "What is the main point of section 3?"}]
)When to use prompt caching:
- Long system prompts reused across many requests
- Large reference documents (codebases, manuals, books) that are constant across a session
- Long conversation histories in multi-turn applications
- Few-shot example sets provided in every request
Cache entries expire after a period of inactivity (5 minutes by default; a 1-hour TTL beta is available). The cache is per-organisation, not per-user.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
