Skip to content

Commit ebc5443

Browse files
habemaseratch
andauthored
Add documentation for token usage tracking (#1518)
Co-authored-by: Kazuhiro Sera <[email protected]>
1 parent 7222a84 commit ebc5443

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

docs/usage.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
```

examples/basic/usage_tracking.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import asyncio
2+
3+
from pydantic import BaseModel
4+
5+
from agents import Agent, Runner, Usage, function_tool
6+
7+
8+
class Weather(BaseModel):
9+
city: str
10+
temperature_range: str
11+
conditions: str
12+
13+
14+
@function_tool
15+
def get_weather(city: str) -> Weather:
16+
"""Get the current weather information for a specified city."""
17+
return Weather(city=city, temperature_range="14-20C", conditions="Sunny with wind.")
18+
19+
20+
def print_usage(usage: Usage) -> None:
21+
print("\n=== Usage ===")
22+
print(f"Requests: {usage.requests}")
23+
print(f"Input tokens: {usage.input_tokens}")
24+
print(f"Output tokens: {usage.output_tokens}")
25+
print(f"Total tokens: {usage.total_tokens}")
26+
27+
28+
async def main() -> None:
29+
agent = Agent(
30+
name="Usage Demo",
31+
instructions="You are a concise assistant. Use tools if needed.",
32+
tools=[get_weather],
33+
)
34+
35+
result = await Runner.run(agent, "What's the weather in Tokyo?")
36+
37+
print("\nFinal output:")
38+
print(result.final_output)
39+
40+
# Access usage from the run context
41+
print_usage(result.context_wrapper.usage)
42+
43+
44+
if __name__ == "__main__":
45+
asyncio.run(main())
46+

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ plugins:
6868
- context.md
6969
- guardrails.md
7070
- multi_agent.md
71+
- usage.md
7172
- Models:
7273
- models/index.md
7374
- models/litellm.md
@@ -165,6 +166,7 @@ plugins:
165166
- context.md
166167
- guardrails.md
167168
- multi_agent.md
169+
- usage.md
168170
- モデル:
169171
- models/index.md
170172
- models/litellm.md

0 commit comments

Comments
 (0)