From 0a002fd81bacda02f85e4558e9e26cd11063210b Mon Sep 17 00:00:00 2001 From: Theodore Li Date: Fri, 13 Feb 2026 14:41:00 -0800 Subject: [PATCH] Include more metadata in cost output --- apps/sim/lib/core/config/feature-flags.ts | 6 ++--- apps/sim/tools/exa/answer.ts | 2 +- apps/sim/tools/exa/find_similar_links.ts | 2 +- apps/sim/tools/exa/get_contents.ts | 2 +- apps/sim/tools/exa/research.ts | 2 +- apps/sim/tools/exa/search.ts | 2 +- apps/sim/tools/index.ts | 31 ++++++++++++----------- 7 files changed, 24 insertions(+), 23 deletions(-) diff --git a/apps/sim/lib/core/config/feature-flags.ts b/apps/sim/lib/core/config/feature-flags.ts index 9f746c5b1..6e65bebd4 100644 --- a/apps/sim/lib/core/config/feature-flags.ts +++ b/apps/sim/lib/core/config/feature-flags.ts @@ -21,9 +21,9 @@ export const isTest = env.NODE_ENV === 'test' /** * Is this the hosted version of the application */ -export const isHosted = - getEnv('NEXT_PUBLIC_APP_URL') === 'https://www.sim.ai' || - getEnv('NEXT_PUBLIC_APP_URL') === 'https://www.staging.sim.ai' +export const isHosted = true + // getEnv('NEXT_PUBLIC_APP_URL') === 'https://www.sim.ai' || + // getEnv('NEXT_PUBLIC_APP_URL') === 'https://www.staging.sim.ai' /** * Is billing enforcement enabled diff --git a/apps/sim/tools/exa/answer.ts b/apps/sim/tools/exa/answer.ts index 937f533ab..9b2a6f3f4 100644 --- a/apps/sim/tools/exa/answer.ts +++ b/apps/sim/tools/exa/answer.ts @@ -31,7 +31,7 @@ export const answerTool: ToolConfig = { }, }, hosting: { - envKeys: ['EXA_API_KEY'], + envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'], apiKeyParam: 'apiKey', byokProviderId: 'exa', pricing: { diff --git a/apps/sim/tools/exa/find_similar_links.ts b/apps/sim/tools/exa/find_similar_links.ts index babe871e3..055d9016b 100644 --- a/apps/sim/tools/exa/find_similar_links.ts +++ b/apps/sim/tools/exa/find_similar_links.ts @@ -80,7 +80,7 @@ export const findSimilarLinksTool: ToolConfig< }, }, hosting: { - envKeys: ['EXA_API_KEY'], + envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'], apiKeyParam: 'apiKey', byokProviderId: 'exa', pricing: { diff --git a/apps/sim/tools/exa/get_contents.ts b/apps/sim/tools/exa/get_contents.ts index 6e6392dc0..3365eb8f6 100644 --- a/apps/sim/tools/exa/get_contents.ts +++ b/apps/sim/tools/exa/get_contents.ts @@ -65,7 +65,7 @@ export const getContentsTool: ToolConfig = }, }, hosting: { - envKeys: ['EXA_API_KEY'], + envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'], apiKeyParam: 'apiKey', byokProviderId: 'exa', pricing: { diff --git a/apps/sim/tools/exa/search.ts b/apps/sim/tools/exa/search.ts index d4406010c..c371fa3b9 100644 --- a/apps/sim/tools/exa/search.ts +++ b/apps/sim/tools/exa/search.ts @@ -90,7 +90,7 @@ export const searchTool: ToolConfig = { }, }, hosting: { - envKeys: ['EXA_API_KEY'], + envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'], apiKeyParam: 'apiKey', byokProviderId: 'exa', pricing: { diff --git a/apps/sim/tools/index.ts b/apps/sim/tools/index.ts index 3953d54bd..f9e7d4bbc 100644 --- a/apps/sim/tools/index.ts +++ b/apps/sim/tools/index.ts @@ -209,9 +209,14 @@ function calculateToolCost( } } +interface HostedKeyCostResult { + cost: number + metadata?: Record +} + /** * Calculate and log hosted key cost for a tool execution. - * Logs to usageLog for audit trail and returns cost for accumulation in userStats. + * Logs to usageLog for audit trail and returns cost + metadata for output. */ async function processHostedKeyCost( tool: ToolConfig, @@ -219,14 +224,14 @@ async function processHostedKeyCost( response: Record, executionContext: ExecutionContext | undefined, requestId: string -): Promise { +): Promise { if (!tool.hosting?.pricing) { - return 0 + return { cost: 0 } } const { cost, metadata } = calculateToolCost(tool.hosting.pricing, params, response) - if (cost <= 0) return 0 + if (cost <= 0) return { cost: 0 } // Log to usageLog table for audit trail if (executionContext?.userId) { @@ -247,7 +252,7 @@ async function processHostedKeyCost( } } - return cost + return { cost, metadata } } /** @@ -643,15 +648,13 @@ export async function executeTool( // Calculate hosted key cost and merge into output.cost if (hostedKeyInfo.isUsingHostedKey && finalResult.success) { - const hostedKeyCost = await processHostedKeyCost(tool, contextParams, finalResult.output, executionContext, requestId) + const { cost: hostedKeyCost, metadata } = await processHostedKeyCost(tool, contextParams, finalResult.output, executionContext, requestId) if (hostedKeyCost > 0) { - const existingCost = finalResult.output?.cost || {} finalResult.output = { ...finalResult.output, cost: { - input: existingCost.input || 0, - output: existingCost.output || 0, - total: (existingCost.total || 0) + hostedKeyCost, + total: hostedKeyCost, + ...metadata, }, } } @@ -708,15 +711,13 @@ export async function executeTool( // Calculate hosted key cost and merge into output.cost if (hostedKeyInfo.isUsingHostedKey && finalResult.success) { - const hostedKeyCost = await processHostedKeyCost(tool, contextParams, finalResult.output, executionContext, requestId) + const { cost: hostedKeyCost, metadata } = await processHostedKeyCost(tool, contextParams, finalResult.output, executionContext, requestId) if (hostedKeyCost > 0) { - const existingCost = finalResult.output?.cost || {} finalResult.output = { ...finalResult.output, cost: { - input: existingCost.input || 0, - output: existingCost.output || 0, - total: (existingCost.total || 0) + hostedKeyCost, + total: hostedKeyCost, + ...metadata, }, } }