MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / extractUsageAndCost

Function extractUsageAndCost

web/src/llm-api/siliconflow.ts:95–112  ·  view source on GitHub ↗
(usage: Record<string, unknown> | undefined | null)

Source from the content-addressed store, hash-verified

93const SILICONFLOW_OUTPUT_COST_PER_TOKEN = 1.20 / 1_000_000
94
95function extractUsageAndCost(usage: Record<string, unknown> | undefined | null): UsageData {
96 if (!usage) return { inputTokens: 0, outputTokens: 0, cacheReadInputTokens: 0, reasoningTokens: 0, cost: 0 }
97 const promptDetails = usage.prompt_tokens_details as Record<string, unknown> | undefined | null
98 const completionDetails = usage.completion_tokens_details as Record<string, unknown> | undefined | null
99
100 const inputTokens = typeof usage.prompt_tokens === 'number' ? usage.prompt_tokens : 0
101 const outputTokens = typeof usage.completion_tokens === 'number' ? usage.completion_tokens : 0
102 const cacheReadInputTokens = typeof promptDetails?.cached_tokens === 'number' ? promptDetails.cached_tokens : 0
103 const reasoningTokens = typeof completionDetails?.reasoning_tokens === 'number' ? completionDetails.reasoning_tokens : 0
104
105 const nonCachedInputTokens = Math.max(0, inputTokens - cacheReadInputTokens)
106 const cost =
107 nonCachedInputTokens * SILICONFLOW_INPUT_COST_PER_TOKEN +
108 cacheReadInputTokens * SILICONFLOW_CACHED_INPUT_COST_PER_TOKEN +
109 outputTokens * SILICONFLOW_OUTPUT_COST_PER_TOKEN
110
111 return { inputTokens, outputTokens, cacheReadInputTokens, reasoningTokens, cost }
112}
113
114export async function handleSiliconFlowNonStream({
115 body,

Callers 2

handleResponseFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected