calculateContextTokens function

int calculateContextTokens(
  1. Usage usage
)

Calculate total context tokens from provider usage.

Ported from pi's calculateContextTokens: prefers Usage.totalTokens when the provider reported it, otherwise sums the components.

Implementation

int calculateContextTokens(Usage usage) {
  return usage.totalTokens != 0
      ? usage.totalTokens
      : usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
}