MCPcopy
hub / github.com/simstudioai/sim / applyHostedKeyCostToResult

Function applyHostedKeyCostToResult

apps/sim/tools/index.ts:639–671  ·  view source on GitHub ↗

* 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
)

Source from the content-addressed store, hash-verified

637 * Reports custom dimension usage, calculates cost, and merges it into the output.
638 */
639async 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
673import { normalizeToolId } from '@/tools/normalize'
674

Callers 1

executeToolFunction · 0.85

Calls 2

processHostedKeyCostFunction · 0.85

Tested by

no test coverage detected