Created
December 26, 2025 14:46
-
-
Save darekrossman/3451e62123c4d5377ce31fa0a4886263 to your computer and use it in GitHub Desktop.
accumulate provider metadata
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
| interface AccumulatedUsage { | |
| input_tokens: number; | |
| cache_creation_input_tokens: number; | |
| cache_read_input_tokens: number; | |
| output_tokens: number; | |
| cache_creation: { | |
| ephemeral_5m_input_tokens: number; | |
| ephemeral_1h_input_tokens: number; | |
| }; | |
| cost: number; | |
| } | |
| const accumulatedUsage: AccumulatedUsage = { | |
| input_tokens: 0, | |
| cache_creation_input_tokens: 0, | |
| cache_read_input_tokens: 0, | |
| output_tokens: 0, | |
| cache_creation: { | |
| ephemeral_5m_input_tokens: 0, | |
| ephemeral_1h_input_tokens: 0, | |
| }, | |
| cost: 0, | |
| } | |
| new ToolLoopAgent({ | |
| onStepFinish: async (data) => { | |
| accumulateUsageFromMetadata(accumulatedUsage, data.providerMetadata); | |
| } | |
| }) | |
| function accumulateUsageFromMetadata( | |
| accumulated: AccumulatedUsage, | |
| providerMetadata: Record<string, unknown> | undefined, | |
| ): void { | |
| if (!providerMetadata) return; | |
| // Extract anthropic.usage values | |
| const anthropic = providerMetadata.anthropic as | |
| | { usage?: Record<string, unknown> } | |
| | undefined; | |
| if (anthropic?.usage) { | |
| const usage = anthropic.usage; | |
| if (typeof usage.input_tokens === "number") { | |
| accumulated.input_tokens += usage.input_tokens; | |
| } | |
| if (typeof usage.cache_creation_input_tokens === "number") { | |
| accumulated.cache_creation_input_tokens += | |
| usage.cache_creation_input_tokens; | |
| } | |
| if (typeof usage.cache_read_input_tokens === "number") { | |
| accumulated.cache_read_input_tokens += usage.cache_read_input_tokens; | |
| } | |
| if (typeof usage.output_tokens === "number") { | |
| accumulated.output_tokens += usage.output_tokens; | |
| } | |
| // Handle nested cache_creation object | |
| const cacheCreation = usage.cache_creation as | |
| | Record<string, unknown> | |
| | undefined; | |
| if (cacheCreation) { | |
| if (typeof cacheCreation.ephemeral_5m_input_tokens === "number") { | |
| accumulated.cache_creation.ephemeral_5m_input_tokens += | |
| cacheCreation.ephemeral_5m_input_tokens; | |
| } | |
| if (typeof cacheCreation.ephemeral_1h_input_tokens === "number") { | |
| accumulated.cache_creation.ephemeral_1h_input_tokens += | |
| cacheCreation.ephemeral_1h_input_tokens; | |
| } | |
| } | |
| } | |
| // Extract gateway.cost | |
| const gateway = providerMetadata.gateway as { cost?: string } | undefined; | |
| if (gateway?.cost) { | |
| const costValue = parseFloat(gateway.cost); | |
| if (!isNaN(costValue)) { | |
| accumulated.cost += costValue; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment