Price Each Request From Its Usage

Since Record a Trace per Request, every model span has kept the provider’s usage block, unread. When the trust’s treasurer asks what a summer of hikers’ questions will cost, those blocks are your evidence.

In this lesson, you’ll give each usage field its meaning, price every call a request made, and end with what a thousand questions cost for each feature, a number the treasurer can budget with.

Give Each Usage Field Its Meaning

A provider bills four kinds of token. Input tokens are the prompt your code sent. Cached tokens are the part of it read back from the provider’s prompt cache, at a fraction of the input price. Cache write tokens are the part stored in the cache for later calls, which costs more than plain input. Output tokens are what the model generated, reasoning included.

Even the field both providers call input tokens has a different meaning for each provider, so the same formula won’t work for both. Open the version for your provider. Each adds a tokens() function that turns the block into the same four counts, stored on the span as s.tokens, next to the raw s.usage:

OpenAI’s input_tokens already includes the cached reads and the cache writes, which it reports under input_tokens_details. So tokens() subtracts them to leave plain input. In model/openai.js, add tokens() and one line to record():

Language: JavaScript Filename: model/openai.js
function record(s, response) {
  s.model = response.model;
  s.stopReason =
    response.incomplete_details?.reason ?? response.status;
  s.usage = response.usage;
  if (response.usage) s.tokens = tokens(response.usage);
}

export function tokens(usage) {
  const details = usage.input_tokens_details ?? {};
  const cached = details.cached_tokens ?? 0;
  const cacheWrite = details.cache_write_tokens ?? 0;
  return {
    input: usage.input_tokens - cached - cacheWrite,
    cached,
    cacheWrite,
    output: usage.output_tokens,
  };
}

The if skips the stream’s early events, whose usage is still null.

Anthropic’s input_tokens counts only the tokens after the last cache breakpoint. The cache reads and writes are reported beside it, as cache_read_input_tokens and cache_creation_input_tokens, so the total input is all three added together. Anthropic’s prompt caching documentation gave that formula on December 2, 2026. In model/anthropic.js, add tokens() and one line to record():

Language: JavaScript Filename: model/anthropic.js
function record(s, message) {
  s.model = message.model;
  s.stopReason = message.stop_reason;
  s.usage = message.usage;
  s.tokens = tokens(message.usage);
}

export function tokens(usage) {
  return {
    input: usage.input_tokens,
    cached: usage.cache_read_input_tokens ?? 0,
    cacheWrite: usage.cache_creation_input_tokens ?? 0,
    output: usage.output_tokens,
  };
}

A streamed message’s final output count arrives in message_delta, so recompute the counts there too, right after the Object.assign() line in stream():

Language: JavaScript Filename: model/anthropic.js
if (event.type === "message_delta") {
  s.stopReason = event.delta.stop_reason;
  Object.assign(s.usage, event.usage);
  s.tokens = tokens(s.usage);
}

Using input_tokens alone as the total input count would leave out every cached and cache write token.

Read the other provider’s version too, because Exercise: Report Cost and Latency prices a day with usage blocks from both providers and checks both formulas. In short, OpenAI’s plain input is input_tokens minus the cached and cache write counts, while Anthropic’s is input_tokens itself, with the cache counts reported beside it.

Now every model span holds the same four keys, and nothing outside model/ reads a provider’s field names. If the two input counts still feel slippery, try a block of your own:

Price Every Call a Request Made

Prices differ by model and change, so keep them in one table with the date you checked them. Create model/prices.js with prices in dollars per million tokens, taken from the providers’ pricing pages on December 2, 2026:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.