feat(metrics): cache write cost accounting in /metrics#57
Merged
Conversation
Add Prometheus metrics for Anthropic prompt cache token usage and
cache boundary state. Callers record API response usage via
RecordCacheUsage; boundary state is recorded via RecordCacheBoundary.
New metrics:
distill_cache_creation_tokens_total{session_id} - tokens written (1.25x)
distill_cache_read_tokens_total{session_id} - tokens read (0.10x)
distill_uncached_input_tokens_total{session_id} - uncached tokens (1.00x)
distill_cache_hit_rate - rolling hit rate gauge
distill_cache_write_efficiency - reads/writes ratio
distill_cache_boundary_position_tokens{session_id} - current boundary
distill_cache_boundary_advances_total{session_id} - boundary advances
distill_cache_boundary_retreats_total{session_id} - boundary retreats
distill_cache_estimated_savings_tokens_total - estimated savings
New helpers:
RecordCacheUsage(UsageRecord) - records Anthropic usage block
RecordCacheBoundary(sid, tokens, adv, ret) - records boundary evaluation
Co-authored-by: Ona <[email protected]>
Co-authored-by: Ona <[email protected]>
9b66a9c to
39a2be7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #52
What
Adds 9 new Prometheus metrics to
/metricscovering Anthropic prompt cache token usage and cache boundary state. Two new helper methods let callers record data without touching Prometheus directly.Changes
pkg/metrics/metrics.goNew metrics:
distill_cache_creation_tokens_total{session_id}distill_cache_read_tokens_total{session_id}distill_uncached_input_tokens_total{session_id}distill_cache_hit_ratecache_read / (cache_read + cache_creation + input)distill_cache_write_efficiencyreads / writesratio — values < 1.0 mean writes that expire before being readdistill_cache_boundary_position_tokens{session_id}distill_cache_boundary_advances_total{session_id}distill_cache_boundary_retreats_total{session_id}distill_cache_estimated_savings_tokens_total{session_id}New helpers:
RecordCacheUsage(UsageRecord)— pass theusageblock from any Anthropic API response; updates counters and derived gaugesRecordCacheBoundary(sessionID, tokens, advanced, retreated)— called by the session boundary manager after each evaluationpkg/metrics/metrics_test.goTestRecordCacheUsage: verifies counter increments and hit rate gaugeTestRecordCacheUsage_DefaultSessionID: empty session ID falls back to"default"TestRecordCacheBoundary: advances/retreats counters and position gaugeTestHandler_CacheMetrics: all new metric names appear in/metricsoutputWhy
Distill's existing
/metricsonly covers the dedup pipeline. Without visibility intocache_creation_input_tokensandcache_read_input_tokens, a 40% token reduction looks like a 40% cost reduction — but if cache hit rate is low, the actual saving is much smaller. These metrics close that gap and also validate that the boundary manager (PR #56) is working correctly in production.