|
| 1 | +# Usage |
| 2 | + |
| 3 | +The Agents SDK automatically tracks token usage for every run. You can access it from the run context and use it to monitor costs, enforce limits, or record analytics. |
| 4 | + |
| 5 | +## What is tracked |
| 6 | + |
| 7 | +- **requests**: number of LLM API calls made |
| 8 | +- **input_tokens**: total input tokens sent |
| 9 | +- **output_tokens**: total output tokens received |
| 10 | +- **total_tokens**: input + output |
| 11 | +- **details**: |
| 12 | + - `input_tokens_details.cached_tokens` |
| 13 | + - `output_tokens_details.reasoning_tokens` |
| 14 | + |
| 15 | +## Accessing usage from a run |
| 16 | + |
| 17 | +After `Runner.run(...)`, access usage via `result.context_wrapper.usage`. |
| 18 | + |
| 19 | +```python |
| 20 | +result = await Runner.run(agent, "What's the weather in Tokyo?") |
| 21 | +usage = result.context_wrapper.usage |
| 22 | + |
| 23 | +print("Requests:", usage.requests) |
| 24 | +print("Input tokens:", usage.input_tokens) |
| 25 | +print("Output tokens:", usage.output_tokens) |
| 26 | +print("Total tokens:", usage.total_tokens) |
| 27 | +``` |
| 28 | + |
| 29 | +Usage is aggregated across all model calls during the run (including tool calls and handoffs). |
| 30 | + |
| 31 | +## Accessing usage with sessions |
| 32 | + |
| 33 | +When you use a `Session` (e.g., `SQLiteSession`), usage continues to accumulate across turns within the same run. Each call to `Runner.run(...)` returns the run’s cumulative usage at that point. |
| 34 | + |
| 35 | +```python |
| 36 | +session = SQLiteSession("my_conversation") |
| 37 | + |
| 38 | +first = await Runner.run(agent, "Hi!", session=session) |
| 39 | +print(first.context_wrapper.usage.total_tokens) |
| 40 | + |
| 41 | +second = await Runner.run(agent, "Can you elaborate?", session=session) |
| 42 | +print(second.context_wrapper.usage.total_tokens) # includes both turns |
| 43 | +``` |
| 44 | + |
| 45 | +## Using usage in hooks |
| 46 | + |
| 47 | +If you’re using `RunHooks`, the `context` object passed to each hook contains `usage`. This lets you log usage at key lifecycle moments. |
| 48 | + |
| 49 | +```python |
| 50 | +class MyHooks(RunHooks): |
| 51 | + async def on_agent_end(self, context: RunContextWrapper, agent: Agent, output: Any) -> None: |
| 52 | + u = context.usage |
| 53 | + print(f"{agent.name} → {u.requests} requests, {u.total_tokens} total tokens") |
| 54 | +``` |
0 commit comments