How to calculate only uncached tokens

An openai Agents SDK returns total agent’s loop tokens usage object as:

inputTokens,
inputTokensDetails: [{cached_tokens}],
outputTokens

so, here the inputTokens are all input tokens, including cached and uncached, or only uncached tokens?
Is it correct to calculate uncached tokens as inputTokens - inputTokensDetails.cached_tokens

Hi and welcome back!

Yes. inputTokens is the total input token count, including both cached and uncached tokens.

The Agents SDK docs describe inputTokens as the number of input tokens used across all requests, and inputTokensDetails as the breakdown for those input tokens.

So this is the right calculation:

uncachedInputTokens = inputTokens - cachedTokens

For the Agents SDK inputTokensDetails is an array. Sum the cached token entries first:

const cachedTokens =
  usage.inputTokensDetails?.reduce(
    (sum, details) => sum + (details.cached_tokens ?? details.cachedTokens ?? 0),
    0
  ) ?? 0;

const uncachedInputTokens = usage.inputTokens - cachedTokens;

For cost estimation treat them as separate buckets:

cost =
  uncachedInputTokens * normalInputTokenRate +
  cachedTokens * cachedInputTokenRate +
  outputTokens * outputTokenRate;

As @vb mentioned, inputTokens represents the total input tokens including both cached and uncached tokens, so calculating uncached tokens as inputTokens - cachedTokens is the correct approach. Really appreciate the clear clarification on this @vb.

~Smith