Description
I've created a coding agentic flow using @anthropic-ai/sdk@0.60.0, and later converted the same flow to use the ai package and @ai-sdk/anthropic.
Using the official anthropic sdk directly, I've had to track usage myself for each step of a conversation/tool usage. I've summed up the numbers and everything seemed correct number-wise:
Input tokens: 10572
Output tokens: 499
Cache creation input tokens: 5485
Cache read input tokens: 16455
In the first step, I wrote 5485 tokens to the cache (on the system prompts). There were then 3 conversation steps where the cache was read (5485 * 3 = 16455). Everything makes sense.
I then converted my flow to the ai package.
I've used streamText, and after result.toUIMessageStream() has all been consumed, I've printed:
console.log("Usage:");
console.log(await result.usage);
which gave me:
Usage:
{
inputTokens: 4532,
outputTokens: 222,
totalTokens: 4754,
cachedInputTokens: 5439
}
totalTokens = inputTokens + outputTokens, so nothing interesting there.
cachedInputTokens... wasn't sure what this is. Based on the number it seemed like cache write, but where's the cache read? I later found out that this is not the write.
Upon further reading of the documentation, I found result.providerMetadata, so I printed:
console.log("Usage:");
console.log(await result.usage);
console.log("Anthropic Usage:");
console.log((await result.providerMetadata)?.anthropic?.usage);
Which gave me:
Usage:
{
inputTokens: 3056,
outputTokens: 240,
totalTokens: 3296,
cachedInputTokens: 5491
}
Anthropic Usage:
{
input_tokens: 3056,
output_tokens: 3,
cache_creation_input_tokens: 0,
cache_read_input_tokens: 5491,
cache_creation: { ephemeral_5m_input_tokens: 0, ephemeral_1h_input_tokens: 0 },
service_tier: 'standard'
}
Observations:
output_tokens === 3 seems wrong, and does not match outputTokens. The number in outputTokens does make sense.
cache_creation_input_tokens === 0 makes no sense. I did not cache those tokens prior to the conversation, and they were first written to the cache in that chat.
cache_read_input_tokens === 5491 matches the cachedInputTokens, but also makes no sense. The chat had several steps with several tool calls. It should have read the cache several times. Number seems like it points only to the last step.
I believe I've ruled out a bug in my code, so I wonder whether I'm missing something obvious.
Could it be the reported usage numbers are wrong when using ai (with anthropic; multi-step conversation with tools)?
I find that very unlikely in such widely used package, but this is the only conclusion I could have deduced.
AI SDK Version
ai: 5.0.24
@ai-sdk/anthropic: 3.0.15
Code of Conduct
Description
I've created a coding agentic flow using
@anthropic-ai/sdk@0.60.0, and later converted the same flow to use theaipackage and@ai-sdk/anthropic.Using the official anthropic sdk directly, I've had to track usage myself for each step of a conversation/tool usage. I've summed up the numbers and everything seemed correct number-wise:
In the first step, I wrote
5485tokens to the cache (on the system prompts). There were then 3 conversation steps where the cache was read (5485 * 3 = 16455). Everything makes sense.I then converted my flow to the
aipackage.I've used
streamText, and afterresult.toUIMessageStream()has all been consumed, I've printed:which gave me:
totalTokens = inputTokens + outputTokens, so nothing interesting there.cachedInputTokens... wasn't sure what this is. Based on the number it seemed like cache write, but where's the cache read? I later found out that this is not the write.Upon further reading of the documentation, I found
result.providerMetadata, so I printed:Which gave me:
Observations:
output_tokens === 3seems wrong, and does not matchoutputTokens. The number inoutputTokensdoes make sense.cache_creation_input_tokens === 0makes no sense. I did not cache those tokens prior to the conversation, and they were first written to the cache in that chat.cache_read_input_tokens === 5491matches thecachedInputTokens, but also makes no sense. The chat had several steps with several tool calls. It should have read the cache several times. Number seems like it points only to the last step.I believe I've ruled out a bug in my code, so I wonder whether I'm missing something obvious.
Could it be the reported usage numbers are wrong when using ai (with anthropic; multi-step conversation with tools)?
I find that very unlikely in such widely used package, but this is the only conclusion I could have deduced.
AI SDK Version
ai: 5.0.24
@ai-sdk/anthropic: 3.0.15
Code of Conduct