66
77@dataclass
88class RequestUsage :
9- """Usage details for a single API request.
10-
11- This is useful for cost calculation when different pricing rates apply based on
12- per-request token counts (e.g., Anthropic's 200K token threshold pricing).
13- """
9+ """Usage details for a single API request."""
1410
1511 input_tokens : int
1612 """Input tokens for this individual request."""
@@ -52,29 +48,22 @@ class Usage:
5248 """Total tokens sent and received, across all requests."""
5349
5450 request_usage_entries : list [RequestUsage ] = field (default_factory = list )
55- """List of individual request usage details for accurate per-request cost calculation.
56-
57- This field preserves the token counts for each individual API request made during a run.
58- This is particularly useful for providers like Anthropic that have different pricing
59- tiers based on per-request token counts (e.g., different rates for requests with more
60- or fewer than 200K tokens).
51+ """List of RequestUsage entries for accurate per-request cost calculation.
6152
6253 Each call to `add()` automatically creates an entry in this list if the added usage
6354 represents a new request (i.e., has non-zero tokens).
6455
6556 Example:
6657 For a run that makes 3 API calls with 100K, 150K, and 80K input tokens each,
6758 the aggregated `input_tokens` would be 330K, but `request_usage_entries` would
68- preserve the [100K, 150K, 80K] breakdown needed for accurate cost calculation.
59+ preserve the [100K, 150K, 80K] breakdown, which could be helpful for detailed
60+ cost calculation or context window management.
6961 """
7062
7163 def add (self , other : "Usage" ) -> None :
7264 """Add another Usage object to this one, aggregating all fields.
7365
74- This method automatically preserves individual request details for accurate
75- cost calculation. When adding a Usage object that represents a single request
76- (requests=1), it creates an RequestUsage entry to preserve the
77- per-request token breakdown.
66+ This method automatically preserves request_usage_entries.
7867
7968 Args:
8069 other: The Usage object to add to this one.
@@ -93,7 +82,7 @@ def add(self, other: "Usage") -> None:
9382 + other .output_tokens_details .reasoning_tokens
9483 )
9584
96- # Automatically preserve individual request details for accurate cost calculation .
85+ # Automatically preserve request_usage_entries .
9786 # If the other Usage represents a single request with tokens, record it.
9887 if other .requests == 1 and other .total_tokens > 0 :
9988 individual_usage = RequestUsage (
0 commit comments