MCPcopy Create free account
hub / github.com/coder/coder / accumulateUsage

Function accumulateUsage

aibridge/intercept/messages/base.go:528–573  ·  view source on GitHub ↗

accumulateUsage accumulates usage statistics from source into dest. It handles both [anthropic.Usage] and [anthropic.MessageDeltaUsage] types through [any]. The function uses reflection to handle the differences between the types: - [anthropic.Usage] has CacheCreation field with ephemeral tokens - [

(dest, src any)

Source from the content-addressed store, hash-verified

526// - [anthropic.Usage] has CacheCreation field with ephemeral tokens
527// - [anthropic.MessageDeltaUsage] doesn't have CacheCreation field
528func accumulateUsage(dest, src any) {
529 switch d := dest.(type) {
530 case *anthropic.Usage:
531 if d == nil {
532 return
533 }
534 switch s := src.(type) {
535 case anthropic.Usage:
536 // Usage -> Usage
537 d.CacheCreation.Ephemeral1hInputTokens += s.CacheCreation.Ephemeral1hInputTokens
538 d.CacheCreation.Ephemeral5mInputTokens += s.CacheCreation.Ephemeral5mInputTokens
539 d.CacheCreationInputTokens += s.CacheCreationInputTokens
540 d.CacheReadInputTokens += s.CacheReadInputTokens
541 d.InputTokens += s.InputTokens
542 d.OutputTokens += s.OutputTokens
543 d.ServerToolUse.WebSearchRequests += s.ServerToolUse.WebSearchRequests
544 case anthropic.MessageDeltaUsage:
545 // MessageDeltaUsage -> Usage
546 d.CacheCreationInputTokens += s.CacheCreationInputTokens
547 d.CacheReadInputTokens += s.CacheReadInputTokens
548 d.InputTokens += s.InputTokens
549 d.OutputTokens += s.OutputTokens
550 d.ServerToolUse.WebSearchRequests += s.ServerToolUse.WebSearchRequests
551 }
552 case *anthropic.MessageDeltaUsage:
553 if d == nil {
554 return
555 }
556 switch s := src.(type) {
557 case anthropic.Usage:
558 // Usage -> MessageDeltaUsage (only common fields)
559 d.CacheCreationInputTokens += s.CacheCreationInputTokens
560 d.CacheReadInputTokens += s.CacheReadInputTokens
561 d.InputTokens += s.InputTokens
562 d.OutputTokens += s.OutputTokens
563 d.ServerToolUse.WebSearchRequests += s.ServerToolUse.WebSearchRequests
564 case anthropic.MessageDeltaUsage:
565 // MessageDeltaUsage -> MessageDeltaUsage
566 d.CacheCreationInputTokens += s.CacheCreationInputTokens
567 d.CacheReadInputTokens += s.CacheReadInputTokens
568 d.InputTokens += s.InputTokens
569 d.OutputTokens += s.OutputTokens
570 d.ServerToolUse.WebSearchRequests += s.ServerToolUse.WebSearchRequests
571 }
572 }
573}
574
575// For centralized requests, markKeyOnError extracts an
576// Anthropic SDK error from err and marks the key based on

Callers 3

ProcessRequestMethod · 0.85
TestAccumulateUsageFunction · 0.85
ProcessRequestMethod · 0.85

Calls

no outgoing calls

Tested by 1

TestAccumulateUsageFunction · 0.68