Skip to content

Usage

TokenUsage dataclass

Provider counts. Cached and reasoning tokens are subsets of input and output.

Source code in src/openai_agents/codex/usage.py
@dataclass(frozen=True, kw_only=True)
class TokenUsage:
    """Provider counts. Cached and reasoning tokens are subsets of input and output."""

    input_tokens: int
    cached_input_tokens: int
    cache_write_input_tokens: int
    output_tokens: int
    reasoning_output_tokens: int
    total_tokens: int

Usage dataclass

Source code in src/openai_agents/codex/usage.py
@dataclass(frozen=True, kw_only=True)
class Usage:
    responses: list[ResponseUsage]
    """One entry per completed model response in this turn, in arrival order."""

    thread: TokenUsage | None = None
    """All reported tokens in the conversation through the completed turn."""

    model_context_window: int | None = None

    @property
    def turn(self) -> TokenUsage | None:
        """Exact total for these responses, when all supplied usage."""
        counts = [response.tokens for response in self.responses if response.tokens is not None]
        if not counts or len(counts) != len(self.responses):
            return None
        return TokenUsage(
            input_tokens=sum(c.input_tokens for c in counts),
            cached_input_tokens=sum(c.cached_input_tokens for c in counts),
            cache_write_input_tokens=sum(c.cache_write_input_tokens for c in counts),
            output_tokens=sum(c.output_tokens for c in counts),
            reasoning_output_tokens=sum(c.reasoning_output_tokens for c in counts),
            total_tokens=sum(c.total_tokens for c in counts),
        )

responses instance-attribute

responses: list[ResponseUsage]

One entry per completed model response in this turn, in arrival order.

thread class-attribute instance-attribute

thread: TokenUsage | None = None

All reported tokens in the conversation through the completed turn.

turn property

turn: TokenUsage | None

Exact total for these responses, when all supplied usage.