What this thread does
The problem
You run a Node/Express app that calls the Anthropic SDK. Every response carries a usage object, and you want a dollar figure per call from it. Your environment (a locked-down sandbox, no package manager, no install step) rules out ready-made packages, so you want one small file you can read in a minute.
What the docs say
Anthropic's prompt caching page says input_tokens counts only the tokens after the last cache breakpoint, and gives the total as cache_read_input_tokens plus cache_creation_input_tokens plus input_tokens. So pricing only input_tokens undercounts a cached app. The same page shows an example response where cache_creation_input_tokens equals the sum of a cache_creation object that splits writes into ephemeral_5m_input_tokens and ephemeral_1h_input_tokens.
The pricing page lists per-model rates in USD per million tokens (base input, 5-minute cache write, 1-hour cache write, cache hit, output) and says to see claude.com/pricing for the most current figures. It states the cache multipliers against base input: 1.25x for a 5-minute write, 2x for a 1-hour write, and 0.1x for a read, with lower read multipliers on some newer models. The Batch API halves input and output prices, US-only inference on Claude 4.6 and later models adds 1.1x, and these stack. Because the rates differ by model and change over time, I do not copy any price into this thread.
The function
Copy this into a file such as cost.js and require it from your handler. Fill the rates object by hand from the pricing page for the model you call.
function usdFor(usage, rates) {
const m = 1000000;
const cc = usage.cache_creation;
const write5m = cc ? (cc.ephemeral_5m_input_tokens || 0) : (usage.cache_creation_input_tokens || 0);
const write1h = cc ? (cc.ephemeral_1h_input_tokens || 0) : 0;
const read = usage.cache_read_input_tokens || 0;
return (
(usage.input_tokens || 0) * rates.input +
(usage.output_tokens || 0) * rates.output +
write5m * rates.cacheWrite5m +
write1h * rates.cacheWrite1h +
read * rates.cacheRead
) / m;
}
module.exports = { usdFor };
An unrecorded editor run (not a stored lab run or ticketed trial) of it with Node on the docs' sample usage (2048 input, 1800 read, 148 five-minute and 100 one-hour writes, 503 output) against one row of the pricing page and the hand arithmetic matched to the cent fraction. I have not run it inside an Express app or against a real invoice.
Mistakes to check
- Pricing input_tokens alone when caching is on.
- Using one rate for every model; keep a rates entry per model id.
- Forgetting the Batch discount or the 1.1x US-only multiplier on Claude 4.6 and later models if you use them, and the higher long-prompt prices some models have.
- Streaming: the usage numbers arrive across stream events and message_delta counts are cumulative, so use the final accumulated message's usage rather than adding event counts or using the first event.
- Treating the result as an invoice. It is an estimate; Anthropic's Console is the billing record.
Sources
Read for this thread: the Anthropic prompt caching page and the pricing page linked above (usage field names, the example response, multipliers). A web search found no zero-dependency Node calculator to cite, so none is recommended here. The streaming point was checked on 2026-11-17 against Anthropic's streaming docs.
Started from a post by @Anthropic (official docs), packaged by the editor. Original post