Cut Claude API Costs: Prompt Caching & Shorter Prompts
This guide shows you how to reduce your Claude API spending by combining two proven techniques: prompt caching (which discounts repeated input tokens) and writing leaner prompts (which reduces the number of tokens you pay for in the first place). You’ll learn the exact steps to enable caching, structure your prompts for maximum cache hits, and trim verbose instructions without losing output quality.
What You’ll Need
- An Anthropic API account with API access (console.anthropic.com)
- Your API key handy
- Basic familiarity with making API calls (curl or a programming language like Python)
- Access to your usage logs (Anthropic console or your own logging)
Step-by-Step
- Understand your current token usage. Before optimizing, check your usage dashboard or logs to see how many input tokens you’re sending per request and how often the same prefix repeats. If you’re sending the same system prompt or long context every time, caching will help.
- Enable prompt caching. Caching is not automatic. You must add the
cache_controlparameter to a content block in your request. For example, in a system prompt, set"cache_control": {"type": "ephemeral"}. This tells Claude to cache that block for 5 minutes (the default TTL). - Place the cache breakpoint correctly. The cache works on the prefix of your prompt. Anything before the breakpoint is cached. Put the breakpoint after the largest static portion—typically your system prompt or a long document—so that the dynamic part (user query) comes after.
- Structure your prompt for reuse. Separate static content (instructions, context, examples) from dynamic content (user input). Keep the static part identical across requests. Even a minor change (like a timestamp) invalidates the cache for that prefix.
- Shorten your prompt. Review every sentence. Remove greetings, filler phrases, and redundant instructions. Use bullet points or numbered lists instead of paragraphs. Aim for the minimum number of tokens that still gets the job done.
- Use concise system prompts. Instead of “You are a helpful assistant that always responds in a friendly tone and provides detailed explanations,” write “You are a helpful assistant. Be concise.” Every word costs tokens.
- Leverage examples judiciously. Few-shot examples help, but each one costs tokens. Keep one or two high-quality examples instead of five mediocre ones. If you need many, cache them.
- Trim the conversation history. If you’re sending a long chat history, consider truncating older messages that aren’t relevant. Keep only the last few turns or summarize the rest.
- Test and measure. After making changes, run a few test calls and compare the token usage in the response’s
usageobject. Look forcache_read_input_tokens(cached tokens) vscache_creation_input_tokens(new tokens). You want the read count to be high. - Monitor and iterate. Check your monthly bill. If costs are still high, revisit your prompt for more cuts, or increase the cache TTL if your use case allows (the default is 5 minutes; you can set it to 1 hour, but that costs more per cached token—only do this if you reuse the same prefix frequently).
Common Mistakes
- Changing the cached prefix. If you add a timestamp or dynamic variable before the cache breakpoint, the cache is useless. Keep the prefix absolutely static.
- Not setting cache_control. Without the parameter, no caching happens. You must explicitly opt in.
- Over-caching. Caching has a write cost (cache_creation_input_tokens). If you send a unique prompt each time, you’ll pay more, not less. Only cache content you reuse.
- Ignoring the cache TTL. The default is 5 minutes. If your requests are spaced more than 5 minutes apart, the cache expires and you pay for creation again. For longer gaps, consider a longer TTL (but weigh the higher per-token cost).
- Being too terse. Cutting essential instructions can degrade output quality, forcing you to retry and spend more. Balance brevity with clarity.
Tips
- Put the cache breakpoint on the system prompt or the first user message if that message is long and static.
- Use a tool like a tokenizer (Claude’s API has a token counting endpoint) to measure your prompt length before sending.
- If you have a large knowledge base, split it into smaller chunks and only include the relevant chunk per request, rather than sending everything.
- For chat applications, cache the system prompt and the entire conversation history up to the latest user message, but ensure the history is identical for the cache to hit.
- Automate prompt testing: write a script that sends the same request with and without caching to see the exact cost difference.
Troubleshooting
Cache isn’t hitting (cache_read_input_tokens is 0). Check that the prefix before the breakpoint is byte-for-byte identical between requests. Also verify that the cache TTL hasn’t expired. If you’re using the API SDK, ensure you’re passing the cache_control parameter correctly in the content block.
Costs went up after enabling caching. You’re likely paying cache_creation_input_tokens on prompts that aren’t reused. Only enable caching on content you know you’ll send repeatedly. Also check if you’re setting a long TTL (1 hour) on rarely repeated content—that’s expensive.
Prompt is too short but output quality dropped. Re-add the critical instructions one at a time, testing each. Sometimes you can replace verbose explanations with a single example that conveys the same rule.
API returns an error about cache_control. Ensure the parameter is placed inside a content block (e.g., in the system array or a message content object), not at the top level. Also check that your API version supports caching (it does as of late 2024).
Frequently Asked Questions
Q: How much does prompt caching save? A: Cached input tokens are about 90% cheaper than regular input tokens, and cache write tokens are about 25% more expensive. The net saving depends on how often you reuse the same prefix.
Q: What is the cache TTL? A: The default is 5 minutes. You can set it to 1 hour by specifying "type": "ephemeral" with a ttl of "1h" (in the API, you’d use "cache_control": {"type": "ephemeral", "ttl": "1h"}).
Q: Can I cache the entire conversation? A: Yes, but the cache must be a prefix. So you can cache the system prompt plus all previous messages, as long as you append the new user message after the cached block.
Q: Does caching affect output quality? A: No. Caching is purely a cost optimization; the model sees the same tokens and produces the same output as if caching were off.
Q: How do I see cache usage in the response? A: The response’s usage object includes cache_creation_input_tokens and cache_read_input_tokens. The read tokens are the ones you’re saving on.
Q: Is there a minimum prompt length for caching to be worthwhile? A: There’s no hard rule, but caching adds a small overhead. For prompts under a few hundred tokens, the savings may be negligible. Test with your own usage.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.