MCPcopy
hub / github.com/codeaashu/claude-code / tokensToUSDCost

Function tokensToUSDCost

src/utils/modelCost.ts:131–142  ·  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

129 * Calculates the USD cost based on token usage and model cost configuration
130 */
131function tokensToUSDCost(modelCosts: ModelCosts, usage: Usage): number {
132 return (
133 (usage.input_tokens / 1_000_000) * modelCosts.inputTokens +
134 (usage.output_tokens / 1_000_000) * modelCosts.outputTokens +
135 ((usage.cache_read_input_tokens ?? 0) / 1_000_000) *
136 modelCosts.promptCacheReadTokens +
137 ((usage.cache_creation_input_tokens ?? 0) / 1_000_000) *
138 modelCosts.promptCacheWriteTokens +
139 (usage.server_tool_use?.web_search_requests ?? 0) *
140 modelCosts.webSearchRequests
141 )
142}
143
144export function getModelCosts(model: string, usage: Usage): ModelCosts {
145 const shortName = getCanonicalName(model)

Callers 1

calculateUSDCostFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected