Skip to content

Track cache token metrics for Anthropic and OpenAI#144

Open
leameow wants to merge 1 commit intomainfrom
lea/labels
Open

Track cache token metrics for Anthropic and OpenAI#144
leameow wants to merge 1 commit intomainfrom
lea/labels

Conversation

@leameow
Copy link
Copy Markdown
Member

@leameow leameow commented Apr 23, 2026

ref https://linear.app/flared/issue/AGENTIC-50/track-cached-token-usage

Summary

  • Add Prometheus counters lacuna_provider_tokens_cache_creation_total (with cache_duration label: 5m/1h/unknown) and lacuna_provider_tokens_cache_read_total for cache token tracking
  • Extract cache TTL from Anthropic request cache_control blocks; resolve flat streaming totals into duration-labeled buckets via RequestInspectionMetadata passed to response_inspector
  • Extract cached_tokens from OpenAI prompt_tokens_details (Chat) and input_tokens_details (Responses)

Test plan

  • Anthropic JSON with nested cache_creation breakdown -> "5m" and "1h" keys
  • Anthropic SSE with flat total + TTL hint 300 -> "5m", 3600 -> "1h", None -> "unknown"
  • Request inspection: uniform TTL, mixed TTLs, default TTL, no cache_control
  • OpenAI chat/responses with and without cached_tokens
  • All 134 tests pass, clippy clean, build clean

🤖 Generated with Claude Code

Add Prometheus counters for cache creation tokens (with duration label)
and cache read tokens. Anthropic responses include per-duration breakdowns
(5m/1h) for non-streaming, and flat totals for streaming that are resolved
using the request's cache_control TTL hint. OpenAI cached read tokens are
extracted from prompt_tokens_details/input_tokens_details.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@leameow leameow force-pushed the lea/labels branch 2 times, most recently from fc69c18 to 8c74735 Compare April 23, 2026 18:37
@leameow leameow requested a review from aviau April 23, 2026 18:38
Comment thread src/api_type/anthropic.rs
assert_eq!(metadata.output_tokens, Some(150));
}

// --- Cache token tests ---
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move to another module?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants