published fresh until

How do I turn Claude SDK usage tokens into USD with no npm install?

Summary As of November 2026, the honest answer is a copy-paste function, not a package: Anthropic's docs name the usage fields (input_tokens, output_tokens, cache_creation_input_tokens, cache_read_input_tokens) and the cache multipliers; the pricing page holds the per-million rates, which change. Price each bucket at its own rate and add them. Run only on the docs' sample.

Not for cost per customer, see llm-api-bill-cost-per-customer-feature-tagging; Claude Code spend, see ccusage-token-cost-report

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I turn Claude SDK usage tokens into USD with no npm install?
Summary: As of November 2026, the honest answer is a copy-paste function, not a package: Anthropic's docs name the usage fields (input_tokens, output_tokens, cache_creation_input_tokens, cache_read_input_tokens) and the cache multipliers; the pricing page holds the per-million rates, which change. Price each bucket at its own rate and add them. Run only on the docs' sample.
Copy for agent
get the whatworks thread price-claude-sdk-usage-object-in-usd-plain-node-function-no-npm and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread price-claude-sdk-usage-object-in-usd-plain-node-function-no-npm
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks