Streaming Guide

    Stream agent responses in real-time using Server-Sent Events (SSE). The SDK handles reconnection, deduplication, and fallback polling automatically.

    Quick Start

    typescript
    const stream = await client.agents.stream('agent-uuid', {
      task: 'Analyze this quarter\'s performance metrics',
    });
    
    // Register listeners BEFORE connecting
    stream.on('textDelta', ({ delta }) => process.stdout.write(delta));
    stream.on('toolCall', ({ toolName, args }) => console.log('Tool:', toolName));
    stream.on('toolResult', ({ toolName, durationMs }) => console.log('Done:', toolName, durationMs + 'ms'));
    stream.on('complete', ({ execution }) => console.log('\nDone!', execution.status));
    stream.on('error', ({ error }) => console.error('Error:', error));
    
    await stream.connect();

    Stream Events

    EventPayloadDescription
    textDelta{ delta }Incremental text chunk
    toolCall{ toolName, args }Agent invoked a tool
    toolResult{ toolName, result, durationMs }Tool completed
    log{ type, title, content }Agent log entry
    delegationStarted{ delegationId, agentName }Sub-agent started (teams)
    delegationCompleted{ delegationId, result }Sub-agent completed
    paused{ reason, data }Execution paused
    complete{ execution }Execution finished
    error{ error }Execution failed
    connectionChange{ connected }SSE connection status

    Three Ways to Stream

    1. New Execution

    typescript
    const stream = await client.agents.stream('agent-uuid', { task: 'Hello' });
    stream.on('textDelta', ({ delta }) => process.stdout.write(delta));
    await stream.connect();

    2. Existing Execution

    typescript
    const { executionId } = await client.agents.execute('agent-uuid', { task: 'Long task' });
    
    // Stream later
    const stream = client.agents.streamExecution(executionId);
    stream.on('textDelta', ({ delta }) => process.stdout.write(delta));
    await stream.connect();

    3. Session Message

    typescript
    const stream = await client.sessions.stream('session-uuid', {
      message: 'Now summarize those findings',
    });
    stream.on('textDelta', ({ delta }) => process.stdout.write(delta));
    await stream.connect();

    Wait for Completion

    typescript
    const stream = await client.agents.stream('agent-uuid', { task: 'Process data' });
    await stream.connect();
    
    // Block until done
    const execution = await stream.wait();
    console.log('Output:', execution.output);

    Reconnection & Reliability

    The SDK handles connection issues automatically:

    • Exponential backoff reconnection (1s, 2s, 4s, 8s, 10s cap)
    • Up to 5 reconnection attempts before falling back to HTTP polling
    • Polling fallback every 2 seconds
    • Events deduplicated by ID — no duplicate processing after reconnect