* Extracts token usage from an Interaction's Usage object. * The Interactions API provides total_input_tokens, total_output_tokens, total_tokens, * and total_reasoning_tokens (for thinking models). * * Also handles the raw API field name total_thought_tokens which the SDK may * map to total_rea
(usage: Interactions.Usage | undefined)
| 492 | * map to total_reasoning_tokens. |
| 493 | */ |
| 494 | function extractInteractionUsage(usage: Interactions.Usage | undefined): { |
| 495 | inputTokens: number |
| 496 | outputTokens: number |
| 497 | reasoningTokens: number |
| 498 | totalTokens: number |
| 499 | } { |
| 500 | if (!usage) { |
| 501 | return { inputTokens: 0, outputTokens: 0, reasoningTokens: 0, totalTokens: 0 } |
| 502 | } |
| 503 | |
| 504 | const usageLogger = createLogger('DeepResearchUsage') |
| 505 | usageLogger.info('Raw interaction usage', { usage: JSON.stringify(usage) }) |
| 506 | |
| 507 | const inputTokens = usage.total_input_tokens ?? 0 |
| 508 | const outputTokens = usage.total_output_tokens ?? 0 |
| 509 | const reasoningTokens = |
| 510 | usage.total_reasoning_tokens ?? |
| 511 | ((usage as Record<string, unknown>).total_thought_tokens as number) ?? |
| 512 | 0 |
| 513 | const totalTokens = usage.total_tokens ?? inputTokens + outputTokens |
| 514 | |
| 515 | return { inputTokens, outputTokens, reasoningTokens, totalTokens } |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Builds a standard ProviderResponse from a completed deep research interaction. |
no test coverage detected