claude_cost/usage.rs
1//! Token usage block.
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6/// Four-field token usage as returned by Anthropic API and Bedrock Converse.
7///
8/// Field names match Anthropic's API; the `From` impls in this crate map
9/// Bedrock Converse's camelCase names (`cacheReadInputTokens`,
10/// `cacheWriteInputTokens` -> `cache_creation_input_tokens`) onto these.
11#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
12#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13#[cfg_attr(feature = "serde", serde(default))]
14pub struct Usage {
15 /// Tokens in the input prompt that were not cached.
16 pub input_tokens: u64,
17 /// Tokens in the model output.
18 pub output_tokens: u64,
19 /// Tokens written to the prompt cache on this request (cache miss).
20 pub cache_creation_input_tokens: u64,
21 /// Tokens served from the prompt cache (cache hit).
22 pub cache_read_input_tokens: u64,
23}
24
25impl Usage {
26 /// True when the request hit the prompt cache.
27 pub fn cache_hit(&self) -> bool {
28 self.cache_read_input_tokens > 0
29 }
30
31 /// Total tokens billed (input + output + cache_creation + cache_read).
32 pub fn total_tokens(&self) -> u64 {
33 self.input_tokens
34 + self.output_tokens
35 + self.cache_creation_input_tokens
36 + self.cache_read_input_tokens
37 }
38
39 /// Build a Usage from Bedrock Converse's camelCase JSON shape.
40 ///
41 /// The `inputTokens` field on Bedrock includes only fresh input (cache
42 /// fields are reported separately), so we map them 1:1.
43 pub fn from_bedrock_converse(
44 input_tokens: u64,
45 output_tokens: u64,
46 cache_read_input_tokens: u64,
47 cache_write_input_tokens: u64,
48 ) -> Self {
49 Self {
50 input_tokens,
51 output_tokens,
52 cache_creation_input_tokens: cache_write_input_tokens,
53 cache_read_input_tokens,
54 }
55 }
56}