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
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.
When signal.abort() is called, the SDK catches the AbortError internally in #runStreamLoop and returns silently — the response_done event never arrives
The consumer's for await loop ends normally with no error, and result.state.usage remains 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';asyncfunctionmain(){constcontroller=newAbortController();constagent=newAgent({name: 'Test',instructions: 'You are a helpful assistant.'});constresult=awaitrun(agent,'Write a long essay about AI',{stream: true,signal: controller.signal,});letcontent='';forawait(consteventofresult){if(event.type==='raw_model_stream_event'){constdata=event.dataasany;if(data.type==='output_text_delta'&&data.delta){content+=data.delta;if(content.length>100){controller.abort();}}}}console.log(`Content length: ${content.length}`);// > 0console.log(`Usage: ${JSON.stringify(result.state?.usage)}`);// All zeros!}
Describe the bug
When a streaming run is aborted via
AbortSignal,result.state.usagereturns all zeros (inputTokens: 0,outputTokens: 0,totalTokens: 0), even though the model has already generated partial content and consumed tokens.This happens because:
response_doneevent arrives (added in PR feat: track token usage while streaming responses for openai models #750)signal.abort()is called, the SDK catches theAbortErrorinternally in#runStreamLoopand returns silently — theresponse_doneevent never arrivesfor awaitloop ends normally with no error, andresult.state.usageremains at zeroThis makes it impossible to track token consumption for cancelled runs, which is important for billing and quota management in production applications.
Debug information
Repro steps