anthropicUsage function
Anthropic Messages API usage shape:
{ usage: { input_tokens, output_tokens, cache_read_input_tokens, cache_creation_input_tokens } }.
Cache reads are billed at the discounted rate; cache creation is
billed at full input rate (we already capture that in input_tokens).
final response = await MeteredCall.run(
provider: 'anthropic',
model: 'claude-sonnet-4-6',
call: () => myAnthropicClient.messages.create(...),
extract: (r) => anthropicUsage(r.toJson()),
);
Implementation
MeterUsage anthropicUsage(Map<String, Object?> response) {
final Map<String, Object?>? usage = _readMap(response['usage']);
if (usage == null) return MeterUsage.empty;
final int input = _readInt(usage['input_tokens']) ?? 0;
final int output = _readInt(usage['output_tokens']) ?? 0;
final int cacheRead = _readInt(usage['cache_read_input_tokens']) ?? 0;
return MeterUsage(
tokensIn: input,
tokensOut: output,
cachedTokensIn: cacheRead,
);
}