You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/usage.md
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,7 @@ The Agents SDK automatically tracks token usage for every run. You can access it
8
8
-**input_tokens**: total input tokens sent
9
9
-**output_tokens**: total output tokens received
10
10
-**total_tokens**: input + output
11
+
-**request_usage_entries**: list of per-request usage breakdowns
11
12
-**details**:
12
13
-`input_tokens_details.cached_tokens`
13
14
-`output_tokens_details.reasoning_tokens`
@@ -46,6 +47,17 @@ result = await Runner.run(agent, "What's the weather in Tokyo?")
46
47
print(result.context_wrapper.usage.total_tokens)
47
48
```
48
49
50
+
## Per-request usage tracking
51
+
52
+
The SDK automatically tracks usage for each API request in `request_usage_entries`, useful for detailed cost calculation and monitoring context window consumption.
53
+
54
+
```python
55
+
result =await Runner.run(agent, "What's the weather in Tokyo?")
56
+
57
+
for request inenumerate(result.context_wrapper.usage.request_usage_entries):
58
+
print(f"Request {i +1}: {request.input_tokens} in, {request.output_tokens} out")
59
+
```
60
+
49
61
## Accessing usage with sessions
50
62
51
63
When you use a `Session` (e.g., `SQLiteSession`), each call to `Runner.run(...)` returns usage for that specific run. Sessions maintain conversation history for context, but each run's usage is independent.
@@ -78,5 +90,6 @@ class MyHooks(RunHooks):
78
90
For detailed API documentation, see:
79
91
80
92
-[`Usage`][agents.usage.Usage] - Usage tracking data structure
0 commit comments