feat: comprehensive token usage tracking for V2 #3
Reference in New Issue
Block a user
Delete Branch "feature/comprehensive-token-usage-tracking"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Details map[string]inttoUsagefor provider-specific token breakdowns (reasoning, cached, audio, thoughts tokens)StreamOptions.IncludeUsageUsageMetadatafrom final chunk*UsagefromChat.Send*,Generate[T]/GenerateWith[T], andAgent.Run*(breaking signature changes)UsageTrackermiddleware to accumulate detail values withDetails()accessorModelPricingandPricingRegistryfor cost computation with cached token discountsBreaking Changes
Chat.Send(string, error)(string, *Usage, error)Chat.SendMessage(string, error)(string, *Usage, error)Chat.SendWithImages(string, error)(string, *Usage, error)Generate[T](T, error)(T, *Usage, error)GenerateWith[T](T, error)(T, *Usage, error)Agent.Run(string, error)(string, *Usage, error)Agent.RunMessages(string, error)(string, *Usage, error)Files Changed
v2/provider/provider.goDetailsmap + key constants toUsagev2/response.goDetailsmap,addUsagehelper, re-export constantsv2/llm.goDetailsinconvertProviderResponsev2/openai/openai.gov2/anthropic/anthropic.gov2/google/google.gov2/chat.go*UsagefromSend/SendMessage/SendWithImagesv2/generate.go*UsagefromGenerate[T]/GenerateWith[T]v2/agent/agent.go*UsagefromRun/RunMessages, updateAsToolv2/middleware.goUsageTrackerdetail accumulation +Details()accessorv2/pricing.goModelPricing,PricingRegistryTest Plan
go build ./...compilesgo test ./...— all tests passgo vet ./...— no issuesgolangci-lint run ./...— no new issuesUsageTrackerGenerate[T]returning usageAgent.Runreturning usagePricingRegistry.Cost()with cached token discountCloses #2
Also created steve/steveternet#47 for V1 audit.
🤖 Generated with Claude Code