Skip to content

Anthropic: capture prompt cache diagnostics (cache_miss_reason, cache_missed_input_tokens) #861

Description

anthropic 1.9.0 made cache diagnostics GA: a diagnostics= request parameter and Message.diagnostics.cache_miss_reason, a union of reason types (model, system, tools or messages changed; previous message not found; unavailable), each carrying cache_missed_input_tokens. Beta BetaMessage.diagnostics already existed in 1.8.0 and isn't captured either.

Proposal

In py/src/braintrust/integrations/anthropic/tracing.py:

  • Add "diagnostics" to METADATA_PARAMS so the request option is logged.
  • In _log_message_to_span / _message_output, log message.diagnostics.cache_miss_reason.type as metadata.
  • Record cache_missed_input_tokens as a numeric metric (e.g. prompt_cache_missed_tokens).
  • Read the fields with getattr so older SDK versions (0.48.0) need no gating. Streaming should get this from the message_start snapshot.

Tests

New cassette-backed test: two calls, the second passing diagnostics={"previous_message_id": ...}.

Parent: #858 (found while reviewing the 2026-10-05 weekly dependency update, #849).

🤖 Generated with Claude Code

Metadata

Metadata

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions