* Apply post-execution hosted-key cost tracking to a successful tool result. * Reports custom dimension usage, calculates cost, and merges it into the output.
( finalResult: ToolResponse, tool: ToolConfig, params: Record<string, unknown>, executionContext: ExecutionContext | undefined, requestId: string, envVarName: string | undefined )
| 637 | * Reports custom dimension usage, calculates cost, and merges it into the output. |
| 638 | */ |
| 639 | async function applyHostedKeyCostToResult( |
| 640 | finalResult: ToolResponse, |
| 641 | tool: ToolConfig, |
| 642 | params: Record<string, unknown>, |
| 643 | executionContext: ExecutionContext | undefined, |
| 644 | requestId: string, |
| 645 | envVarName: string | undefined |
| 646 | ): Promise<void> { |
| 647 | await reportCustomDimensionUsage(tool, params, finalResult.output, executionContext, requestId) |
| 648 | |
| 649 | const { cost: hostedKeyCost, metadata } = await processHostedKeyCost( |
| 650 | tool, |
| 651 | params, |
| 652 | finalResult.output, |
| 653 | executionContext, |
| 654 | requestId |
| 655 | ) |
| 656 | |
| 657 | const provider = tool.hosting?.byokProviderId || tool.id |
| 658 | const key = envVarName ?? 'unknown' |
| 659 | hostedKeyMetrics.recordUsed({ provider, tool: tool.id, key }) |
| 660 | hostedKeyMetrics.recordCostCharged(hostedKeyCost, { provider, tool: tool.id }) |
| 661 | |
| 662 | if (hostedKeyCost > 0) { |
| 663 | finalResult.output = { |
| 664 | ...finalResult.output, |
| 665 | cost: { |
| 666 | ...metadata, |
| 667 | total: hostedKeyCost, |
| 668 | }, |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | import { normalizeToolId } from '@/tools/normalize' |
| 674 |
no test coverage detected