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
+15-5Lines changed: 15 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,25 +30,35 @@ Usage is aggregated across all model calls during the run (including tool calls
30
30
31
31
## Accessing usage with sessions
32
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.
33
+
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.
34
34
35
35
```python
36
36
session = SQLiteSession("my_conversation")
37
37
38
38
first =await Runner.run(agent, "Hi!", session=session)
39
-
print(first.context_wrapper.usage.total_tokens)
39
+
print(first.context_wrapper.usage.total_tokens)# Usage for first run
40
40
41
41
second =await Runner.run(agent, "Can you elaborate?", session=session)
42
-
print(second.context_wrapper.usage.total_tokens) #includes both turns
42
+
print(second.context_wrapper.usage.total_tokens) #Usage for second run
43
43
```
44
44
45
+
Note that while sessions preserve conversation context between runs, the usage metrics returned by each `Runner.run()` call represent only that particular execution. In sessions, previous messages may be re-fed as input to each run, which affects the input token count in consequent turns.
46
+
45
47
## Using usage in hooks
46
48
47
-
If you’re using `RunHooks`, the `context` object passed to each hook contains `usage`. This lets you log usage at key lifecycle moments.
49
+
If you're using `RunHooks`, the `context` object passed to each hook contains `usage`. This lets you log usage at key lifecycle moments.
0 commit comments