Skip to content

Usage data unavailable when streaming run is aborted via AbortSignal #995

@sonh1230

Description

@sonh1230

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:

  1. The SDK accumulates usage only when a response_done event arrives (added in PR feat: track token usage while streaming responses for openai models #750)
  2. When signal.abort() is called, the SDK catches the AbortError internally in #runStreamLoop and returns silently — the response_done event never arrives
  3. 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';

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!
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions