Skip to content

Commit f76bf33

Browse files
authored
Docs: Fix confusing session usage docs (#1637)
1 parent a9bdf8e commit f76bf33

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

docs/usage.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,35 @@ Usage is aggregated across all model calls during the run (including tool calls
3030

3131
## Accessing usage with sessions
3232

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.
3434

3535
```python
3636
session = SQLiteSession("my_conversation")
3737

3838
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
4040

4141
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
4343
```
4444

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+
4547
## Using usage in hooks
4648

47-
If youre 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.
4850

4951
```python
5052
class MyHooks(RunHooks):
5153
async def on_agent_end(self, context: RunContextWrapper, agent: Agent, output: Any) -> None:
5254
u = context.usage
5355
print(f"{agent.name}{u.requests} requests, {u.total_tokens} total tokens")
54-
```
56+
```
57+
58+
## API Reference
59+
60+
For detailed API documentation, see:
61+
62+
- [`Usage`][agents.usage.Usage] - Usage tracking data structure
63+
- [`RunContextWrapper`][agents.run.RunContextWrapper] - Access usage from run context
64+
- [`RunHooks`][agents.run.RunHooks] - Hook into usage tracking lifecycle

0 commit comments

Comments
 (0)