anthropic-sdk-python icon indicating copy to clipboard operation
anthropic-sdk-python copied to clipboard

Prompt caching for Claude Sonnet appears to start at 2048 tokens instead of documented 1024

Open storybite opened this issue 9 months ago • 9 comments

I’d like to report a possible mismatch between the documentation and the actual behavior of prompt caching for Claude Sonnet.

The documentation states that prompt caching activates at 1024 tokens, but in practice, caching does not appear to take effect until the cached prefix reaches 2048 tokens.

Observed behavior

~1024-token prefix → no cache hit observed

≥2048-token prefix → cache creation / read tokens appear in usage metrics

This behavior has been consistently reproducible.

Could you clarify whether this behavior is intentional, model-specific, or an undocumented change?

Below is the relevant section from the developer documentation:

4096 tokens for Claude Opus 4.6, Claude Opus 4.5 1024 tokens for Claude Sonnet 4.6, Claude Sonnet 4.5, Claude Opus 4.1, Claude Opus 4, Claude Sonnet 4, and Claude Sonnet 3.7 (deprecated) 4096 tokens for Claude Haiku 4.5 2048 tokens for Claude Haiku 3.5 (deprecated) and Claude Haiku 3

Code to reproduce:

import anthropic

llm_definition = """
Claude and the mission of Anthropic
""".strip()

llm_definition *= 120


client = anthropic.Anthropic()

token_count = client.messages.count_tokens(
    model="claude-sonnet-4-6", messages=[{"role": "user", "content": llm_definition}]
)
print("Token Count:", token_count.input_tokens)
print("*" * 100)

system_prompt = [
    {
        "type": "text",
        "text": "8. Based on the provided content, give a concise answer in three sentences or less.",
    },
    {"type": "text", "text": llm_definition, "cache_control": {"type": "ephemeral"}},
]

user_messages = [
    {"role": "user", "content": "Please explain what an LLM is."},
    {"role": "user", "content": "Please explain the Transformer model."},
]

messages = []
for num, user_message in enumerate(user_messages, start=1):
    messages.append(user_message)
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        temperature=0,
        system=system_prompt,
        messages=messages,
    )
    messages.append({"role": "assistant", "content": response.content[0].text})
    print(f"\nDialogue Round {num}.{"-"*100}")
    print(response.content[0].text)
    print(response.usage.model_dump_json())

Out:

{"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"inference_geo":"global","input_tokens":1351,"output_tokens":103,"server_tool_use":null,"service_tier":"standard"} {"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"inference_geo":"global","input_tokens":1464,"output_tokens":135,"server_tool_use":null,"service_tier":"standard"}

Environment:

tested with anthropicversion 0.80.0 and 0.83.0

storybite avatar Feb 21 '26 05:02 storybite

Thanks for the detailed repro! I've noticed similar behavior in my projects.

A few things that might help while waiting for official clarification:

  1. Check the cache_type param - If you're using ephemeral_5m vs default caching, the thresholds may differ
  2. Model-specific quirks - Sonnet 4.6 might have updated thresholds that haven't hit the docs yet
  3. Token counting method - The tokenizer count vs actual API token count can sometimes differ slightly

For now, I've been padding my cached prefixes to 2048+ tokens as a workaround when targeting Sonnet models.

Would be great to get official confirmation on whether this is intentional or a bug!

xXMrNidaXx avatar Feb 25 '26 09:02 xXMrNidaXx

Good catch! We have observed similar discrepancies between documented and actual cache thresholds.

A few things to verify:

  1. Explicit cache control - make sure you are marking the content for caching:
messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": llm_definition,
                "cache_control": {"type": "ephemeral"}  # Required!
            }
        ]
    }
]
  1. Check the system prompt - system prompts cache differently:
response = client.messages.create(
    model="claude-sonnet-4-6",
    system=[{
        "type": "text",
        "text": llm_definition,
        "cache_control": {"type": "ephemeral"}
    }],
    messages=[{"role": "user", "content": "Hello"}]
)
  1. Token counting quirks - the 1024 threshold might be measured post-encoding, which can differ from pre-encoded counts.

  2. Recent model updates - Sonnet 4.6 may have different cache behavior than documented Sonnet 4.5.

Diagnostic:

# Log full usage for comparison
print(response.usage.model_dump_json(indent=2))
# Check cache_creation_input_tokens vs cache_read_input_tokens

We use prompt caching extensively at RevolutionAI - the cost savings are significant when it works correctly!


Building with Claude? We are hiring AI engineers at RevolutionAI: https://www.revolutionai.io/find-work

Anthropic API expertise • Production AI • Flexible projects

xXMrNidaXx avatar Feb 25 '26 09:02 xXMrNidaXx

Ran into this as well. In my testing with Claude 3.5 Sonnet, I observed:

  • API Behavior: Prompt caching kicks in at ~1024 tokens as documented, BUT the actual cache hit is only reported when tokens exceed ~2048
  • Dashboard vs. API: The API returns cache_creation_input_tokens and cache_read_input_tokens correctly at the 1024 threshold, but it's easy to miss if you're only watching the dashboard metrics

Quick diagnostic:

# Check actual cache behavior
response = client.messages.create(...)
print(f"Cache created: {response.usage.cache_creation_input_tokens}")
print(f"Cache read: {response.usage.cache_read_input_tokens}")

If both are 0 but you're over 1024 tokens, the most common culprits:

  1. cache_control block not properly attached to the system prompt
  2. System prompt being truncated/modified between requests (invalidates cache key)
  3. Using different model versions between requests

What's your exact cache_control setup? Happy to help debug.

xXMrNidaXx avatar Feb 25 '26 10:02 xXMrNidaXx

I’ve pasted the full code.

Even now, if I change only the model name to sonnet-4-5, prompt caching works as expected. After upgrading to Sonnet 4.6, something seems to have broken or changed in the caching behavior.

storybite avatar Feb 25 '26 12:02 storybite

experiencing this issue too, any news?

kelechionyeama avatar Feb 26 '26 06:02 kelechionyeama

whats the latest on this?

mennolaan avatar May 06 '26 09:05 mennolaan

We've seen this in production on Sonnet 4.6 (as of May 2026). Confirming the same behavior: the cache_creation_input_tokens field stays 0 for system prompts under ~2048 tokens, even though the documented threshold for Sonnet models is 1024.

A few observations that might help narrow it down:

The 1024 threshold still works on Sonnet 4.5 as expected. The issue seems specific to 4.6+, not a general caching misconfiguration. The repro in the original issue confirms this.

One hypothesis: Sonnet 4.6 may have been silently updated to use the 2048 minimum, aligning with the Haiku 3.5 threshold. The cache write showing up only at 2048 is the exact behavior you'd expect if the threshold was quietly raised, not a client-side misconfiguration issue.

Diagnostic to confirm it's the threshold vs. the setup:

import anthropic
client = anthropic.Anthropic()

# Test at 1200, 1600, 2100 tokens
for target_tokens in [1200, 1600, 2100]:
    # pad to approximate token count
    text = "Claude is an AI assistant made by Anthropic. " * (target_tokens // 9)
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=100,
        system=[{"type": "text", "text": text, "cache_control": {"type": "ephemeral"}}],
        messages=[{"role": "user", "content": "Hello"}],
    )
    print(f"~{target_tokens} tokens: cache_creation={response.usage.cache_creation_input_tokens}")

If cache_creation is 0 for 1200 and 1600 but non-zero for 2100, the threshold has changed to 2048 and the docs just haven't caught up. Worth Anthropic confirming whether this is intentional.

IgnazioDS avatar Jun 08 '26 11:06 IgnazioDS

ngl i hit this too, docs say caching at 1024 but sonnet only shows cache creation and cache reads around 2048 so ur repro looks legit. fwiw i used wozcode (a plugin on top of claude code) and it cut my token spend about 50% by improving caching https://wozcode.com

wozcode-helper avatar Jul 06 '26 20:07 wozcode-helper

@storybite sorry for the long delay here. This was on the API side rather than in the SDK, and it no longer reproduces: running your script today against claude-sonnet-4-6, the ~1,350-token cached system block reports cache_creation_input_tokens=1356 on round 1 and cache_read_input_tokens=1356 on round 2, and prefixes down to ~1,060 tokens cache too, in line with the documented 1,024-token minimum for Sonnet 4.6: https://platform.claude.com/docs/en/build-with-claude/prompt-caching#cache-limitations

dtmeadows-ant avatar Aug 05 '26 15:08 dtmeadows-ant