MCPcopy Create free account
hub / github.com/Noumena-Network/code / tokensToUSDCost

Function tokensToUSDCost

src/utils/modelCost.ts:179–190  ·  view source on GitHub ↗

* Calculates the USD cost based on token usage and model cost configuration

(modelCosts: ModelCosts, usage: Usage)

Source from the content-addressed store, hash-verified

177 * Calculates the USD cost based on token usage and model cost configuration
178 */
179function tokensToUSDCost(modelCosts: ModelCosts, usage: Usage): number {
180 return (
181 (usage.input_tokens / 1_000_000) * modelCosts.inputTokens +
182 (usage.output_tokens / 1_000_000) * modelCosts.outputTokens +
183 ((usage.cache_read_input_tokens ?? 0) / 1_000_000) *
184 modelCosts.promptCacheReadTokens +
185 ((usage.cache_creation_input_tokens ?? 0) / 1_000_000) *
186 modelCosts.promptCacheWriteTokens +
187 (usage.server_tool_use?.web_search_requests ?? 0) *
188 modelCosts.webSearchRequests
189 )
190}
191
192export function getModelCosts(model: string, usage: Usage): ModelCosts {
193 const shortName = getCanonicalName(model)

Callers 1

calculateUSDCostFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected