feat(metrics): per-call-site cache hit rate tracking#60
Merged
Conversation
Add CallSiteTracker to pkg/metrics. Records Anthropic API usage per call site and exposes HitRate, WriteEfficiency, RequestHitRate. - Record(callSite, UsageRecord): accumulates token counters and request counts; marks CacheHitRequests when cache_read_input_tokens > 0 - Stats(callSite): snapshot for a single call site - AllStats(): all call sites sorted by hit rate ascending (worst first) - Reset / ResetAll for test isolation - Summary(): human-readable table of call site, hit%, efficiency, reqs Thread-safe via sync.RWMutex. Co-authored-by: Ona <no-reply@ona.com>
Co-authored-by: Ona <no-reply@ona.com>
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 #47
What
Adds
CallSiteTrackertopkg/metrics. Records Anthropic API usage per call site and surfaces the worst performers first, making it easy to identify which agents or code paths are getting poor cache hit rates.Changes
pkg/metrics/callsite.go(new)CallSiteTracker: thread-safe tracker backed bysync.RWMutexRecord(callSite, UsageRecord): accumulatesCacheCreationTokens,CacheReadTokens,UncachedInputTokens,OutputTokens; incrementsCacheHitRequestswhencache_read_input_tokens > 0HitRate():cache_read / (cache_read + cache_creation + uncached_input)WriteEfficiency():cache_read / cache_creation— values < 1.0 mean writes that expire before being readRequestHitRate(): fraction of requests with at least one cache read tokenAllStats(): all call sites sorted by hit rate ascending (worst first)Summary(): human-readable table of call site, hit%, efficiency, request countReset/ResetAllfor test isolationpkg/metrics/callsite_test.go(new) — 7 testsUsage