-
Notifications
You must be signed in to change notification settings - Fork 612
Open
Labels
Description
Describe the bug
When a streaming run is aborted via AbortSignal, result.state.usage returns all zeros (inputTokens: 0, outputTokens: 0, totalTokens: 0), even though the model has already generated partial content and consumed tokens.
This happens because:
- The SDK accumulates usage only when a
response_doneevent arrives (added in PR feat: track token usage while streaming responses for openai models #750) - When
signal.abort()is called, the SDK catches theAbortErrorinternally in#runStreamLoopand returns silently — theresponse_doneevent never arrives - The consumer's
for awaitloop ends normally with no error, andresult.state.usageremains at zero
This makes it impossible to track token consumption for cancelled runs, which is important for billing and quota management in production applications.
Debug information
- Agents SDK version: v0.4.6+
- Runtime environment: Node.js 22
Repro steps
import { Agent, run } from '@openai/agents';
async function main() {
const controller = new AbortController();
const agent = new Agent({ name: 'Test', instructions: 'You are a helpful assistant.' });
const result = await run(agent, 'Write a long essay about AI', {
stream: true,
signal: controller.signal,
});
let content = '';
for await (const event of result) {
if (event.type === 'raw_model_stream_event') {
const data = event.data as any;
if (data.type === 'output_text_delta' && data.delta) {
content += data.delta;
if (content.length > 100) {
controller.abort();
}
}
}
}
console.log(`Content length: ${content.length}`); // > 0
console.log(`Usage: ${JSON.stringify(result.state?.usage)}`); // All zeros!
}Reactions are currently unavailable