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

Function extractUsageAndCost

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

Source from the content-addressed store, hash-verified

214}
215
216function extractUsageAndCost(
217 usage: Record<string, unknown> | undefined | null,
218 model: string,
219): UsageData {
220 if (!usage) {
221 return {
222 inputTokens: 0,
223 outputTokens: 0,
224 cacheReadInputTokens: 0,
225 reasoningTokens: 0,
226 cost: 0,
227 }
228 }
229
230 const promptDetails = usage.prompt_tokens_details as
231 | Record<string, unknown>
232 | undefined
233 | null
234 const completionDetails = usage.completion_tokens_details as
235 | Record<string, unknown>
236 | undefined
237 | null
238 const inputTokens =
239 typeof usage.prompt_tokens === 'number' ? usage.prompt_tokens : 0
240 const outputTokens =
241 typeof usage.completion_tokens === 'number' ? usage.completion_tokens : 0
242 const cacheReadInputTokens =
243 typeof usage.cached_tokens === 'number'
244 ? usage.cached_tokens
245 : typeof promptDetails?.cached_tokens === 'number'
246 ? promptDetails.cached_tokens
247 : 0
248 const reasoningTokens =
249 typeof completionDetails?.reasoning_tokens === 'number'
250 ? completionDetails.reasoning_tokens
251 : 0
252
253 const pricing = getMoonshotPricing(model)
254 const nonCachedInputTokens = Math.max(0, inputTokens - cacheReadInputTokens)
255 const cost =
256 nonCachedInputTokens * pricing.inputCostPerToken +
257 cacheReadInputTokens * pricing.cachedInputCostPerToken +
258 outputTokens * pricing.outputCostPerToken
259
260 return {
261 inputTokens,
262 outputTokens,
263 cacheReadInputTokens,
264 reasoningTokens,
265 cost,
266 }
267}
268
269export async function handleMoonshotNonStream({
270 body,

Callers 2

handleMoonshotNonStreamFunction · 0.70
handleResponseFunction · 0.70

Calls 1

getMoonshotPricingFunction · 0.85

Tested by

no test coverage detected