Skip to content

Usage

Usage dataclass

Source code in src/cai/sdk/agents/usage.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@dataclass
class Usage:
    requests: int = 0
    """Total requests made to the LLM API."""

    input_tokens: int = 0
    """Total input tokens sent, across all requests."""

    output_tokens: int = 0
    """Total output tokens received, across all requests."""

    total_tokens: int = 0
    """Total tokens sent and received, across all requests."""

    cache_creation_input_tokens: Optional[int] = None
    """Tokens written to cache (extra cost for cache writes)."""

    cache_read_input_tokens: Optional[int] = None
    """Tokens read from cache (savings from cache hits)."""

    def add(self, other: "Usage") -> None:
        self.requests += other.requests if other.requests else 0
        self.input_tokens += other.input_tokens if other.input_tokens else 0
        self.output_tokens += other.output_tokens if other.output_tokens else 0
        self.total_tokens += other.total_tokens if other.total_tokens else 0
        # Add cache metrics (handle None values)
        if other.cache_creation_input_tokens:
            self.cache_creation_input_tokens = (self.cache_creation_input_tokens or 0) + other.cache_creation_input_tokens
        if other.cache_read_input_tokens:
            self.cache_read_input_tokens = (self.cache_read_input_tokens or 0) + other.cache_read_input_tokens

requests class-attribute instance-attribute

requests: int = 0

Total requests made to the LLM API.

input_tokens class-attribute instance-attribute

input_tokens: int = 0

Total input tokens sent, across all requests.

output_tokens class-attribute instance-attribute

output_tokens: int = 0

Total output tokens received, across all requests.

total_tokens class-attribute instance-attribute

total_tokens: int = 0

Total tokens sent and received, across all requests.

cache_creation_input_tokens class-attribute instance-attribute

cache_creation_input_tokens: Optional[int] = None

Tokens written to cache (extra cost for cache writes).

cache_read_input_tokens class-attribute instance-attribute

cache_read_input_tokens: Optional[int] = None

Tokens read from cache (savings from cache hits).