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

Function extractUsageAndCost

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

Source from the content-addressed store, hash-verified

201}
202
203function extractUsageAndCost(
204 usage: Record<string, unknown> | undefined | null,
205 model: string,
206): UsageData {
207 if (!usage)
208 return {
209 inputTokens: 0,
210 outputTokens: 0,
211 cacheReadInputTokens: 0,
212 reasoningTokens: 0,
213 cost: 0,
214 }
215 const promptDetails = usage.prompt_tokens_details as
216 | Record<string, unknown>
217 | undefined
218 | null
219 const completionDetails = usage.completion_tokens_details as
220 | Record<string, unknown>
221 | undefined
222 | null
223
224 const inputTokens =
225 typeof usage.prompt_tokens === 'number' ? usage.prompt_tokens : 0
226 const outputTokens =
227 typeof usage.completion_tokens === 'number' ? usage.completion_tokens : 0
228 const cacheReadInputTokens =
229 typeof promptDetails?.cached_tokens === 'number'
230 ? promptDetails.cached_tokens
231 : 0
232 const reasoningTokens =
233 typeof completionDetails?.reasoning_tokens === 'number'
234 ? completionDetails.reasoning_tokens
235 : 0
236
237 // Fireworks doesn't return cost — compute from token counts and known pricing
238 const pricing = getFireworksPricing(model)
239 const nonCachedInputTokens = Math.max(0, inputTokens - cacheReadInputTokens)
240 const cost =
241 nonCachedInputTokens * pricing.inputCostPerToken +
242 cacheReadInputTokens * pricing.cachedInputCostPerToken +
243 outputTokens * pricing.outputCostPerToken
244
245 return {
246 inputTokens,
247 outputTokens,
248 cacheReadInputTokens,
249 reasoningTokens,
250 cost,
251 }
252}
253
254export async function handleFireworksNonStream({
255 body,

Callers 2

handleFireworksNonStreamFunction · 0.70
handleResponseFunction · 0.70

Calls 1

getFireworksPricingFunction · 0.85

Tested by

no test coverage detected