ClaudeHack
0

Enable automatic prompt caching on multi-turn API chats

Usage & Limits·12 hours ago·verified 12 hours ago·intermediate·~15 min setup·verified

Add a top-level cache_control ephemeral flag so growing conversation prefixes reuse cached tokens and cut API cost and latency.

The problem

Long chatbots and agent loops re-send the same system prompt and history on every turn, paying full input cost repeatedly.

The hack

Set `cache_control: { type: "ephemeral" }` at the request top level (automatic caching). Keep a stable prefix so subsequent turns hit the cache.

Why it works

Automatic caching applies the breakpoint to the last cacheable block and advances as the conversation grows. Cache reads are far cheaper than reprocessing the full prefix.

Setup

1. Identify a multi-turn Claude API flow with a large stable system/tools prefix. 2. Add top-level `cache_control: { "type": "ephemeral" }` to each request. 3. Keep tools + system + earlier messages byte-stable so the prefix matches. 4. Confirm `cache_read_input_tokens` rises on turn 2+; consider 1-hour TTL only if 5 minutes is too short.

Code

{
  "model": "claude-sonnet-4-5",
  "max_tokens": 1024,
  "cache_control": { "type": "ephemeral" },
  "system": "You are a helpful assistant that remembers our conversation.",
  "messages": [{ "role": "user", "content": "Hello" }]
}

Additional details

Prompt caching is an API feature. Claude Code already manages its own context; use this when you build apps on the Messages API.

Source

anthropic·View original →·by Anthropic Docs

Does this still work?

0 said it works · 0 said it's broken

Discussion

No comments yet. Be the first to say whether this worked for you.