cache-diagnostics
build-with-claude/cache-diagnostics
History
build-with-claude/cache-diagnostics Changed · +1 / -1 lines
* **Beta:** Field names and semantics may change while this feature is in beta. * **Claude API only:** Not available on Amazon Bedrock or Google Cloud. * **Limited retention:** Fingerprints for `previous_message_id` lookup expire after a short period. Run diagnostic comparisons between closely spaced requests. -* **Same workspace:** The previous request must have been made with an API key from the same organization and workspace. To check, compare the `anthropic-workspace-id` [response header](https://platform.claude.com/docs/en/api/overview#response-headers) on the two responses. +* **Same workspace:** The previous request must have run in the same organization and workspace. To check, compare the `anthropic-workspace-id` [response header](https://platform.claude.com/docs/en/api/overview#response-headers) on the two responses. * **Comparison horizon:** For very long conversations where the only change is deep in the message list, the response may be `unavailable` rather than a precise location. * **Best-effort:** Diagnostics never blocks or fails your request. If diagnostic information is not available, the response returns `unavailable`, or `cache_miss_reason: null` when the comparison was still running.
build-with-claude/cache-diagnostics Changed · +16 / -16 lines
```bash cURL # Turn 1: establish the cache and opt in to diagnostics response=$(curl -sS --fail-with-body https://api.anthropic.com/v1/messages \ - --header "x-api-key: $ANTHROPIC_API_KEY" \ - --header "anthropic-version: 2023-06-01" \ - --header "anthropic-beta: cache-diagnosis-2026-04-07" \ - --header "content-type: application/json" \ - --data '{ + -H "x-api-key: $ANTHROPIC_API_KEY" \ + -H "anthropic-version: 2023-06-01" \ + -H "anthropic-beta: cache-diagnosis-2026-04-07" \ + -H "content-type: application/json" \ + -d '{ "model": "claude-opus-5", "max_tokens": 1024, "cache_control": {"type": "ephemeral"},
# Turn 2: reference the previous turn so the API can compare prefixes curl -sS --fail-with-body https://api.anthropic.com/v1/messages \ - --header "x-api-key: $ANTHROPIC_API_KEY" \ - --header "anthropic-version: 2023-06-01" \ - --header "anthropic-beta: cache-diagnosis-2026-04-07" \ - --header "content-type: application/json" \ - --data @- <<EOF | jq '{id, diagnostics}' # diagnostics: null means no divergence was found + -H "x-api-key: $ANTHROPIC_API_KEY" \ + -H "anthropic-version: 2023-06-01" \ + -H "anthropic-beta: cache-diagnosis-2026-04-07" \ + -H "content-type: application/json" \ + -d @- <<EOF | jq '{id, diagnostics}' # diagnostics: null means no divergence was found { "model": "claude-opus-5", "max_tokens": 1024,
# Turn 2: stream the response. diagnostics arrives on the message_start event; # a null value means no divergence was found. curl -sS --fail-with-body https://api.anthropic.com/v1/messages \ - --header "x-api-key: $ANTHROPIC_API_KEY" \ - --header "anthropic-version: 2023-06-01" \ - --header "anthropic-beta: cache-diagnosis-2026-04-07" \ - --header "content-type: application/json" \ - --data @- <<EOF | jq -R 'select(startswith("data: ")) | ltrimstr("data: ") | fromjson | select(.type == "message_start") | .message.diagnostics' + -H "x-api-key: $ANTHROPIC_API_KEY" \ + -H "anthropic-version: 2023-06-01" \ + -H "anthropic-beta: cache-diagnosis-2026-04-07" \ + -H "content-type: application/json" \ + -d @- <<EOF | jq -R 'select(startswith("data: ")) | ltrimstr("data: ") | fromjson | select(.type == "message_start") | .message.diagnostics' { "model": "claude-opus-5", "max_tokens": 1024,
## Limitations -* **Beta:** Field names and semantics may change before general availability. +* **Beta:** Field names and semantics may change while this feature is in beta. * **Claude API only:** Not available on Amazon Bedrock or Google Cloud. * **Limited retention:** Fingerprints for `previous_message_id` lookup expire after a short period. Run diagnostic comparisons between closely spaced requests. * **Same workspace:** The previous request must have been made with an API key from the same organization and workspace. To check, compare the `anthropic-workspace-id` [response header](https://platform.claude.com/docs/en/api/overview#response-headers) on the two responses.
build-with-claude/cache-diagnostics First recorded · 1100 lines, first recorded
## Compatibility ## How cache diagnostics works ## Basic usage ## Streaming ## Threading diagnostics through a conversation loop ## Response format ## Cache miss reason types ## Reading diagnostics alongside usage ## Limitations ## Data retention ## See also
The first capture of this source. The page was already there, and this is what it said.
---
title: Cache diagnostics
url: https://platform.claude.com/docs/en/build-with-claude/cache-diagnostics
description: Diagnose unexpected prompt cache misses by comparing consecutive requests and identifying exactly where the prompt prefix diverged.
---
## Compatibility
- Status: Beta
- [Beta header](https://platform.claude.com/docs/en/api/beta-headers): `cache-diagnosis-2026-04-07`
- [ZDR](https://platform.claude.com/docs/en/manage-claude/api-and-data-retention): eligible (excludes [Covered Models](https://platform.claude.com/docs/en/manage-claude/api-and-data-retention#model-specific-data-retention-requirements))
- Platforms: Claude API (beta); not available on Claude Platform on AWS, Amazon Bedrock, Google Cloud, Microsoft Foundry
[Prompt caching](https://platform.claude.com/docs/en/build-with-claude/prompt-caching) cuts latency and cost significantly, but only when the beginning of your prompt is byte-for-byte identical to a recent request. A reordered tool, a timestamp interpolated into your system prompt, or an edit to an earlier message can silently invalidate the cache. Without cache diagnostics, the only signal is `usage.cache_read_input_tokens` dropping to zero, with no indication of what changed.
Cache diagnostics closes that gap. Pass the `id` of your previous response, and the API compares the two requests and tells you where they diverged (the model, the system prompt, the tools, or the message history) so you can fix the root cause instead of guessing.
## How cache diagnostics works
When the beta header is present, the API stores a lightweight fingerprint of each request, keyed by the response `id`. On your next request, include that `id` as `diagnostics.previous_message_id`. The API rebuilds the fingerprint for the new request, compares it against the stored one, and attaches a `diagnostics` object to the response describing the first point of divergence.
The comparison is about request structure, independent of whether the cache actually hit. See [Reading diagnostics alongside usage](https://platform.claude.com/docs/en/build-with-claude/cache-diagnostics#reading-diagnostics-alongside-usage) for how to combine the `diagnostics` result with `usage.cache_read_input_tokens`.
Fingerprints contain only hashes and token-count estimates (never raw prompt content), are retained for a limited time, are scoped to your organization and workspace, and are not used for any other purpose.
## Basic usage
Send the beta header on every turn. On the first turn, pass `"previous_message_id": null` to opt in without a prior message to compare against. On subsequent turns, pass the `id` from the previous response.
<CodeGroup>
```bash cURL
# Turn 1: establish the cache and opt in to diagnostics
response=$(curl -sS --fail-with-body https://api.anthropic.com/v1/messages \
--header "x-api-key: $ANTHROPIC_API_KEY" \
--header "anthropic-version: 2023-06-01" \
--header "anthropic-beta: cache-diagnosis-2026-04-07" \
--header "content-type: application/json" \
--data '{
"model": "claude-opus-5",
"max_tokens": 1024,
"cache_control": {"type": "ephemeral"},
"system": "You are an AI assistant analyzing a large document. <document>...</document>",
"messages": [{"role": "user", "content": "Summarize section 1."}],
"diagnostics": {"previous_message_id": null}
}')
jq '{id, diagnostics}' <<< "$response"
message_id=$(jq -r '.id' <<< "$response")
# Turn 2: reference the previous turn so the API can compare prefixes
curl -sS --fail-with-body https://api.anthropic.com/v1/messages \
--header "x-api-key: $ANTHROPIC_API_KEY" \
--header "anthropic-version: 2023-06-01" \
--header "anthropic-beta: cache-diagnosis-2026-04-07" \
--header "content-type: application/json" \
--data @- <<EOF | jq '{id, diagnostics}' # diagnostics: null means no divergence was found
{
"model": "claude-opus-5",
"max_tokens": 1024,
"cache_control": {"type": "ephemeral"},
"system": "You are an AI assistant analyzing a large document. <document>...</document>",
"messages": [
{"role": "user", "content": "Summarize section 1."},
{"role": "assistant", "content": "Section 1 covers..."},
{"role": "user", "content": "Now summarize section 2."}
],
"diagnostics": {"previous_message_id": "$message_id"}
}
EOF
```
```bash CLI
# Turn 1
turn1=$(ant beta:messages create \
--beta cache-diagnosis-2026-04-07 \
--transform '{id,usage,diagnostics}' <<'YAML'
model: claude-opus-5
max_tokens: 1024
cache_control:
type: ephemeral
system: "You are an AI assistant analyzing a large document. <document>...</document>"
messages:
- role: user
content: Summarize section 1.
diagnostics:
previous_message_id: null
YAML
)
printf '%s\n' "$turn1"
# Turn 2: pass the id from turn 1 as previous_message_id
message_id=$(jq -r '.id' <<<"$turn1")
ant beta:messages create \
--beta cache-diagnosis-2026-04-07 \
--transform '{id,usage,diagnostics}' <<YAML
model: claude-opus-5
max_tokens: 1024
cache_control:
type: ephemeral
system: "You are an AI assistant analyzing a large document. <document>...</document>"
messages:
- role: user
content: Summarize section 1.
- role: assistant
content: Section 1 covers...
- role: user
content: Now summarize section 2.
diagnostics:
previous_message_id: $message_id
YAML
```
```python Python
client = anthropic.Anthropic()
SYSTEM = "You are an AI assistant analyzing a large document. <document>...</document>"
# Turn 1: opt in with previous_message_id=None
r1 = client.beta.messages.create(
model="claude-opus-5",
max_tokens=1024,
cache_control={"type": "ephemeral"},
system=SYSTEM,
messages=[{"role": "user", "content": "Summarize section 1."}],
diagnostics={"previous_message_id": None},
betas=["cache-diagnosis-2026-04-07"],
)
# Turn 2: reference the previous response id
r2 = client.beta.messages.create(
model="claude-opus-5",
max_tokens=1024,
cache_control={"type": "ephemeral"},
system=SYSTEM,
messages=[
{"role": "user", "content": "Summarize section 1."},
{"role": "assistant", "content": r1.content},
{"role": "user", "content": "Now summarize section 2."},
],
diagnostics={"previous_message_id": r1.id},
betas=["cache-diagnosis-2026-04-07"],
)
diagnostics = r2.diagnostics
if diagnostics is None:
print("No divergence detected.")
elif diagnostics.cache_miss_reason is None:
print("Comparison still pending.")
else:
print(f"cache_miss_reason: {diagnostics.cache_miss_reason.type}")
```
```typescript TypeScript
const client = new Anthropic();
const SYSTEM = "You are an AI assistant analyzing a large document. <document>...</document>";
// Turn 1: opt in with previous_message_id: null
const r1 = await client.beta.messages.create({
model: "claude-opus-5",
max_tokens: 1024,
cache_control: { type: "ephemeral" },
system: SYSTEM,
messages: [{ role: "user", content: "Summarize section 1." }],
diagnostics: { previous_message_id: null },
betas: ["cache-diagnosis-2026-04-07"]
});
// Turn 2: reference the previous response id
const r2 = await client.beta.messages.create({
model: "claude-opus-5",
max_tokens: 1024,
cache_control: { type: "ephemeral" },
system: SYSTEM,
messages: [
{ role: "user", content: "Summarize section 1." },
{ role: "assistant", content: r1.content },
{ role: "user", content: "Now summarize section 2." }
],
diagnostics: { previous_message_id: r1.id },
betas: ["cache-diagnosis-2026-04-07"]
});
if (r2.diagnostics === null) {
console.log("No divergence detected.");
} else if (r2.diagnostics.cache_miss_reason === null) {
console.log("Comparison still pending.");
} else {
console.log(`cache_miss_reason: ${r2.diagnostics.cache_miss_reason.type}`);
}
```
```csharp C#
AnthropicClient client = new();
var system = "You are an AI assistant analyzing a large document. <document>...</document>";
var r1 = await client.Beta.Messages.Create(
new()
{
Model = Messages::Model.ClaudeOpus5,
MaxTokens = 1024,
CacheControl = new(),
System = system,
Messages =
[
new() { Role = Role.User, Content = "Summarize section 1." },
],
Diagnostics = new() { PreviousMessageID = null },
Betas = [AnthropicBeta.CacheDiagnosis2026_04_07],
}
);
var r2 = await client.Beta.Messages.Create(
new()
{
Model = Messages::Model.ClaudeOpus5,
MaxTokens = 1024,
CacheControl = new(),
System = system,
Messages =
[
new() { Role = Role.User, Content = "Summarize section 1." },
new()
{
Role = Role.Assistant,
Content = r1.Content.Select(block => new BetaContentBlockParam(block.Json)).ToList(),
},
new() { Role = Role.User, Content = "Now summarize section 2." },
],
Diagnostics = new() { PreviousMessageID = r1.ID },
Betas = [AnthropicBeta.CacheDiagnosis2026_04_07],
}
);
Console.WriteLine(r2.Diagnostics switch
{
null => "No divergence detected.",
{ CacheMissReason: null } => "Comparison still pending.",
{ CacheMissReason.Type: var type } => $"cache_miss_reason: {type.GetString()}",
});
```
```go Go
client := anthropic.NewClient()
ctx := context.Background()
system := []anthropic.BetaTextBlockParam{
{Text: "You are an AI assistant analyzing a large document. <document>...</document>"},
}
r1, err := client.Beta.Messages.New(ctx, anthropic.BetaMessageNewParams{
Model: anthropic.ModelClaudeOpus5,
MaxTokens: 1024,
CacheControl: anthropic.BetaCacheControlEphemeralParam{},
System: system,
Messages: []anthropic.BetaMessageParam{
anthropic.NewBetaUserMessage(anthropic.NewBetaTextBlock("Summarize section 1.")),
},
Diagnostics: anthropic.BetaDiagnosticsParam{
PreviousMessageID: param.Null[string](),
},
Betas: []anthropic.AnthropicBeta{anthropic.AnthropicBetaCacheDiagnosis2026_04_07},
})
if err != nil {
panic(err)
}
r2, err := client.Beta.Messages.New(ctx, anthropic.BetaMessageNewParams{
Model: anthropic.ModelClaudeOpus5,
MaxTokens: 1024,
CacheControl: anthropic.BetaCacheControlEphemeralParam{},
System: system,
Messages: []anthropic.BetaMessageParam{
anthropic.NewBetaUserMessage(anthropic.NewBetaTextBlock("Summarize section 1.")),
r1.ToParam(),
anthropic.NewBetaUserMessage(anthropic.NewBetaTextBlock("Now summarize section 2.")),
},
Diagnostics: anthropic.BetaDiagnosticsParam{
PreviousMessageID: anthropic.String(r1.ID),
},
Betas: []anthropic.AnthropicBeta{anthropic.AnthropicBetaCacheDiagnosis2026_04_07},
})
if err != nil {
panic(err)
}
switch {
case !r2.JSON.Diagnostics.Valid():
fmt.Println("No divergence detected.")
case !r2.Diagnostics.JSON.CacheMissReason.Valid():
fmt.Println("Comparison still pending.")
default:
fmt.Printf("cache_miss_reason: %s\n", r2.Diagnostics.CacheMissReason.Type)
}
```
```java Java
var client = AnthropicOkHttpClient.fromEnv();
var system = "You are an AI assistant analyzing a large document. <document>...</document>";
Cut at 300 lines.