detached d2ceb48

fix(provider): report Anthropic cache hit ratio from measured usage

commit
d2ceb4836c0827bb92c8fa55ba1fd21a55afc9c0
author
brice · · 2026-10-20 04:34 UTC
parent
a9d674e
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
2 files · +83 -16
M packages/provider/anthropic.go +34 -16
@@ -666,8 +666,12 @@ func (c *anthropicClient) runStream(ctx context.Context, resp *http.Response, re
666666 blockOrder []int // insertion order of indexes
667667 activeIdx = -1
668668 usage Usage
669- stop = StopEnd
670- finalErr error
669+ // cacheReported is set once the stream carries explicit cache
670+ // usage fields, so the cache hit ratio is only reported when the
671+ // endpoint measured it (some Anthropic-compatible APIs omit them).
672+ cacheReported bool
673+ stop = StopEnd
674+ finalErr error
671675 )
672676 _ = activeIdx // read-only indicator used for legacy parity
673677
@@ -700,6 +704,10 @@ func (c *anthropicClient) runStream(ctx context.Context, resp *http.Response, re
700704 }
701705
702706 sendDone := func() {
707+ if cacheReported {
708+ usage.CacheMeasuredPromptTokens = usage.PromptTokens()
709+ usage.CacheMeasuredReadTokens = usage.CacheReadTokens
710+ }
703711 usage.CostUSD = ComputeCost(model, usage)
704712 out <- EventUsage{Usage: usage}
705713 out <- EventDone{Stop: stop, Err: finalErr, Message: assembleMsg()}
@@ -800,10 +808,10 @@ func (c *anthropicClient) runStream(ctx context.Context, resp *http.Response, re
800808 var m struct {
801809 Message struct {
802810 Usage struct {
803- InputTokens int `json:"input_tokens"`
804- OutputTokens int `json:"output_tokens"`
805- CacheReadInputTokens int `json:"cache_read_input_tokens"`
806- CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
811+ InputTokens int `json:"input_tokens"`
812+ OutputTokens int `json:"output_tokens"`
813+ CacheReadInputTokens *int `json:"cache_read_input_tokens"`
814+ CacheCreationInputTokens *int `json:"cache_creation_input_tokens"`
807815 } `json:"usage"`
808816 } `json:"message"`
809817 }
@@ -814,18 +822,26 @@ func (c *anthropicClient) runStream(ctx context.Context, resp *http.Response, re
814822 // which can be 50-70% of cost.
815823 usage.InputTokens = m.Message.Usage.InputTokens
816824 usage.OutputTokens = m.Message.Usage.OutputTokens
817- usage.CacheReadTokens = m.Message.Usage.CacheReadInputTokens
818- usage.CacheWriteTokens = m.Message.Usage.CacheCreationInputTokens
825+ usage.CacheReadTokens = 0
826+ usage.CacheWriteTokens = 0
827+ if r := m.Message.Usage.CacheReadInputTokens; r != nil {
828+ usage.CacheReadTokens = *r
829+ cacheReported = true
830+ }
831+ if w := m.Message.Usage.CacheCreationInputTokens; w != nil {
832+ usage.CacheWriteTokens = *w
833+ cacheReported = true
834+ }
819835 case "message_delta":
820836 var m struct {
821837 Delta struct {
822838 StopReason string `json:"stop_reason"`
823839 } `json:"delta"`
824840 Usage struct {
825- InputTokens int `json:"input_tokens"`
826- OutputTokens int `json:"output_tokens"`
827- CacheReadInputTokens int `json:"cache_read_input_tokens"`
828- CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
841+ InputTokens int `json:"input_tokens"`
842+ OutputTokens int `json:"output_tokens"`
843+ CacheReadInputTokens *int `json:"cache_read_input_tokens"`
844+ CacheCreationInputTokens *int `json:"cache_creation_input_tokens"`
829845 } `json:"usage"`
830846 }
831847 _ = json.Unmarshal([]byte(ev.Data), &m)
@@ -838,11 +854,13 @@ func (c *anthropicClient) runStream(ctx context.Context, resp *http.Response, re
838854 if m.Usage.OutputTokens > 0 {
839855 usage.OutputTokens = m.Usage.OutputTokens
840856 }
841- if m.Usage.CacheReadInputTokens > 0 {
842- usage.CacheReadTokens = m.Usage.CacheReadInputTokens
857+ if r := m.Usage.CacheReadInputTokens; r != nil && *r > 0 {
858+ usage.CacheReadTokens = *r
859+ cacheReported = true
843860 }
844- if m.Usage.CacheCreationInputTokens > 0 {
845- usage.CacheWriteTokens = m.Usage.CacheCreationInputTokens
861+ if w := m.Usage.CacheCreationInputTokens; w != nil && *w > 0 {
862+ usage.CacheWriteTokens = *w
863+ cacheReported = true
846864 }
847865 switch m.Delta.StopReason {
848866 case "end_turn", "stop_sequence":
@@ -666,8 +666,12 @@ func (c *anthropicClient) runStream(ctx context.Context, resp *http.Response, re
666 blockOrder []int // insertion order of indexes666 blockOrder []int // insertion order of indexes
667 activeIdx = -1667 activeIdx = -1
668 usage Usage668 usage Usage
669- stop = StopEnd669+ // cacheReported is set once the stream carries explicit cache
670- finalErr error670+ // usage fields, so the cache hit ratio is only reported when the
671+ // endpoint measured it (some Anthropic-compatible APIs omit them).
672+ cacheReported bool
673+ stop = StopEnd
674+ finalErr error
671 )675 )
672 _ = activeIdx // read-only indicator used for legacy parity676 _ = activeIdx // read-only indicator used for legacy parity
673 677
@@ -700,6 +704,10 @@ func (c *anthropicClient) runStream(ctx context.Context, resp *http.Response, re
700 }704 }
701 705
702 sendDone := func() {706 sendDone := func() {
707+ if cacheReported {
708+ usage.CacheMeasuredPromptTokens = usage.PromptTokens()
709+ usage.CacheMeasuredReadTokens = usage.CacheReadTokens
710+ }
703 usage.CostUSD = ComputeCost(model, usage)711 usage.CostUSD = ComputeCost(model, usage)
704 out <- EventUsage{Usage: usage}712 out <- EventUsage{Usage: usage}
705 out <- EventDone{Stop: stop, Err: finalErr, Message: assembleMsg()}713 out <- EventDone{Stop: stop, Err: finalErr, Message: assembleMsg()}
@@ -800,10 +808,10 @@ func (c *anthropicClient) runStream(ctx context.Context, resp *http.Response, re
800 var m struct {808 var m struct {
801 Message struct {809 Message struct {
802 Usage struct {810 Usage struct {
803- InputTokens int `json:"input_tokens"`811+ InputTokens int `json:"input_tokens"`
804- OutputTokens int `json:"output_tokens"`812+ OutputTokens int `json:"output_tokens"`
805- CacheReadInputTokens int `json:"cache_read_input_tokens"`813+ CacheReadInputTokens *int `json:"cache_read_input_tokens"`
806- CacheCreationInputTokens int `json:"cache_creation_input_tokens"`814+ CacheCreationInputTokens *int `json:"cache_creation_input_tokens"`
807 } `json:"usage"`815 } `json:"usage"`
808 } `json:"message"`816 } `json:"message"`
809 }817 }
@@ -814,18 +822,26 @@ func (c *anthropicClient) runStream(ctx context.Context, resp *http.Response, re
814 // which can be 50-70% of cost.822 // which can be 50-70% of cost.
815 usage.InputTokens = m.Message.Usage.InputTokens823 usage.InputTokens = m.Message.Usage.InputTokens
816 usage.OutputTokens = m.Message.Usage.OutputTokens824 usage.OutputTokens = m.Message.Usage.OutputTokens
817- usage.CacheReadTokens = m.Message.Usage.CacheReadInputTokens825+ usage.CacheReadTokens = 0
818- usage.CacheWriteTokens = m.Message.Usage.CacheCreationInputTokens826+ usage.CacheWriteTokens = 0
827+ if r := m.Message.Usage.CacheReadInputTokens; r != nil {
828+ usage.CacheReadTokens = *r
829+ cacheReported = true
830+ }
831+ if w := m.Message.Usage.CacheCreationInputTokens; w != nil {
832+ usage.CacheWriteTokens = *w
833+ cacheReported = true
834+ }
819 case "message_delta":835 case "message_delta":
820 var m struct {836 var m struct {
821 Delta struct {837 Delta struct {
822 StopReason string `json:"stop_reason"`838 StopReason string `json:"stop_reason"`
823 } `json:"delta"`839 } `json:"delta"`
824 Usage struct {840 Usage struct {
825- InputTokens int `json:"input_tokens"`841+ InputTokens int `json:"input_tokens"`
826- OutputTokens int `json:"output_tokens"`842+ OutputTokens int `json:"output_tokens"`
827- CacheReadInputTokens int `json:"cache_read_input_tokens"`843+ CacheReadInputTokens *int `json:"cache_read_input_tokens"`
828- CacheCreationInputTokens int `json:"cache_creation_input_tokens"`844+ CacheCreationInputTokens *int `json:"cache_creation_input_tokens"`
829 } `json:"usage"`845 } `json:"usage"`
830 }846 }
831 _ = json.Unmarshal([]byte(ev.Data), &m)847 _ = json.Unmarshal([]byte(ev.Data), &m)
@@ -838,11 +854,13 @@ func (c *anthropicClient) runStream(ctx context.Context, resp *http.Response, re
838 if m.Usage.OutputTokens > 0 {854 if m.Usage.OutputTokens > 0 {
839 usage.OutputTokens = m.Usage.OutputTokens855 usage.OutputTokens = m.Usage.OutputTokens
840 }856 }
841- if m.Usage.CacheReadInputTokens > 0 {857+ if r := m.Usage.CacheReadInputTokens; r != nil && *r > 0 {
842- usage.CacheReadTokens = m.Usage.CacheReadInputTokens858+ usage.CacheReadTokens = *r
859+ cacheReported = true
843 }860 }
844- if m.Usage.CacheCreationInputTokens > 0 {861+ if w := m.Usage.CacheCreationInputTokens; w != nil && *w > 0 {
845- usage.CacheWriteTokens = m.Usage.CacheCreationInputTokens862+ usage.CacheWriteTokens = *w
863+ cacheReported = true
846 }864 }
847 switch m.Delta.StopReason {865 switch m.Delta.StopReason {
848 case "end_turn", "stop_sequence":866 case "end_turn", "stop_sequence":
M packages/provider/provider_test.go +49 -0
@@ -554,6 +554,55 @@ func TestAnthropicStreamHappyPath(t *testing.T) {
554554 }
555555 }
556556
557+func anthropicStreamUsage(t *testing.T, messageStart string) Usage {
558+ t.Helper()
559+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
560+ w.Header().Set("content-type", "text/event-stream")
561+ w.WriteHeader(http.StatusOK)
562+ _, _ = w.Write([]byte("event: message_start\ndata: " + messageStart + "\n\n" +
563+ "event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}\n\n" +
564+ "event: message_stop\ndata: {}\n\n"))
565+ }))
566+ defer srv.Close()
567+
568+ evs, err := NewAnthropic("x", srv.URL).Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
569+ if err != nil {
570+ t.Fatal(err)
571+ }
572+ var usage Usage
573+ for ev := range evs {
574+ if e, ok := ev.(EventUsage); ok {
575+ usage = e.Usage
576+ }
577+ }
578+ return usage
579+}
580+
581+func TestAnthropicStreamReportsCacheHitRatio(t *testing.T) {
582+ usage := anthropicStreamUsage(t, `{"message":{"usage":{"input_tokens":20,"output_tokens":0,"cache_read_input_tokens":900,"cache_creation_input_tokens":80}}}`)
583+ if usage.CacheReadTokens != 900 || usage.CacheWriteTokens != 80 {
584+ t.Fatalf("cache tokens = R%d W%d, want R900 W80", usage.CacheReadTokens, usage.CacheWriteTokens)
585+ }
586+ ratio, ok := usage.CacheHitRatio()
587+ if !ok || math.Abs(ratio-0.9) > 1e-9 {
588+ t.Fatalf("cache hit ratio = %v, %v, want 0.9, true", ratio, ok)
589+ }
590+}
591+
592+func TestAnthropicStreamKnownCacheMissReportsZeroRatio(t *testing.T) {
593+ usage := anthropicStreamUsage(t, `{"message":{"usage":{"input_tokens":100,"output_tokens":0,"cache_read_input_tokens":0,"cache_creation_input_tokens":0}}}`)
594+ if ratio, ok := usage.CacheHitRatio(); !ok || ratio != 0 {
595+ t.Fatalf("cache hit ratio = %v, %v, want 0, true", ratio, ok)
596+ }
597+}
598+
599+func TestAnthropicStreamWithoutCacheFieldsLeavesRatioUnknown(t *testing.T) {
600+ usage := anthropicStreamUsage(t, `{"message":{"usage":{"input_tokens":100,"output_tokens":0}}}`)
601+ if ratio, ok := usage.CacheHitRatio(); ok {
602+ t.Fatalf("cache hit ratio = %v, true, want unknown", ratio)
603+ }
604+}
605+
557606 func TestClaudeOpus5Catalog(t *testing.T) {
558607 m, err := FindModel("anthropic", "claude-opus-5")
559608 if err != nil {
@@ -554,6 +554,55 @@ func TestAnthropicStreamHappyPath(t *testing.T) {
554 }554 }
555 }555 }
556 556
557+func anthropicStreamUsage(t *testing.T, messageStart string) Usage {
558+ t.Helper()
559+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
560+ w.Header().Set("content-type", "text/event-stream")
561+ w.WriteHeader(http.StatusOK)
562+ _, _ = w.Write([]byte("event: message_start\ndata: " + messageStart + "\n\n" +
563+ "event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}\n\n" +
564+ "event: message_stop\ndata: {}\n\n"))
565+ }))
566+ defer srv.Close()
567+
568+ evs, err := NewAnthropic("x", srv.URL).Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
569+ if err != nil {
570+ t.Fatal(err)
571+ }
572+ var usage Usage
573+ for ev := range evs {
574+ if e, ok := ev.(EventUsage); ok {
575+ usage = e.Usage
576+ }
577+ }
578+ return usage
579+}
580+
581+func TestAnthropicStreamReportsCacheHitRatio(t *testing.T) {
582+ usage := anthropicStreamUsage(t, `{"message":{"usage":{"input_tokens":20,"output_tokens":0,"cache_read_input_tokens":900,"cache_creation_input_tokens":80}}}`)
583+ if usage.CacheReadTokens != 900 || usage.CacheWriteTokens != 80 {
584+ t.Fatalf("cache tokens = R%d W%d, want R900 W80", usage.CacheReadTokens, usage.CacheWriteTokens)
585+ }
586+ ratio, ok := usage.CacheHitRatio()
587+ if !ok || math.Abs(ratio-0.9) > 1e-9 {
588+ t.Fatalf("cache hit ratio = %v, %v, want 0.9, true", ratio, ok)
589+ }
590+}
591+
592+func TestAnthropicStreamKnownCacheMissReportsZeroRatio(t *testing.T) {
593+ usage := anthropicStreamUsage(t, `{"message":{"usage":{"input_tokens":100,"output_tokens":0,"cache_read_input_tokens":0,"cache_creation_input_tokens":0}}}`)
594+ if ratio, ok := usage.CacheHitRatio(); !ok || ratio != 0 {
595+ t.Fatalf("cache hit ratio = %v, %v, want 0, true", ratio, ok)
596+ }
597+}
598+
599+func TestAnthropicStreamWithoutCacheFieldsLeavesRatioUnknown(t *testing.T) {
600+ usage := anthropicStreamUsage(t, `{"message":{"usage":{"input_tokens":100,"output_tokens":0}}}`)
601+ if ratio, ok := usage.CacheHitRatio(); ok {
602+ t.Fatalf("cache hit ratio = %v, true, want unknown", ratio)
603+ }
604+}
605+
557 func TestClaudeOpus5Catalog(t *testing.T) {606 func TestClaudeOpus5Catalog(t *testing.T) {
558 m, err := FindModel("anthropic", "claude-opus-5")607 m, err := FindModel("anthropic", "claude-opus-5")
559 if err != nil {608 if err != nil {

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close