Closed
Description
Bug description
When using openai-agents-python
with Runner.run_streamed
, the context.usage
values in the hooks (input_tokens
, output_tokens
, total_tokens
, requests
) are always 0
.
This issue does not happen when using Runner.run
(non-streaming mode), where the usage values are correctly populated.
Debug information
- Agents SDK version:
v0.0.12
- Platform: macOS
Repro steps
# Tested with multiple models (gpt-4o, o4-mini, o3-mini)
run_config = RunConfig(
model="o4-mini",
model_settings=ModelSettings(include_usage=True)
)
class AIAgentsHooks(RunHooks):
def __init__(self):
self.event_counter = 0
def _usage_to_str(self, usage: Usage) -> str:
return f"{usage.requests} requests, {usage.input_tokens} input tokens, {usage.output_tokens} output tokens, {usage.total_tokens} total tokens"
async def on_agent_start(self, context: RunContextWrapper, agent: Agent) -> None:
print(f"Start: {self._usage_to_str(context.usage)}")
async def on_agent_end(self, context: RunContextWrapper, agent: Agent, output: Any) -> None:
print(f"End: {self._usage_to_str(context.usage)}")
hooks = AIAgentsHooks()
# Run in streaming mode
result = Runner.run_streamed(
starting_agent=agent,
input=input,
run_config=run_config,
hooks=hooks
)
async for event in result.stream_events():
if event.type == "raw_response_event" and hasattr(event.data, "delta"):
delta = event.data.delta
if delta:
yield StreamingChunk(
data=ChunkData(delta=delta, finish_reason=None)
)
Output (streamed)
The output is always: 0 requests, 0 input tokens, 0 output tokens, 0 total tokens
If the same config is run with Runner.run, usage works correctly:
result = await Runner.run(
starting_agent=agent,
input=input,
run_config=run_config,
hooks=hooks
)
Output (non-streamed):
requests, 123 input tokens, 56 output tokens, 179 total tokens
Expected behavior
In streaming mode, the context.usage values should reflect actual usage data just like in non-streaming mode.