AI Agent Streaming Response 2026
Design the event protocol before writing the stream. Then handle proxy buffering, disconnects and cancellation, which only show up in production.
📖 Keep reading
An agent that calls three tools takes ten seconds or more. Streaming does not change the reasoning, it changes how much of that time the user can see.
Define event types first: status, token, tool_start, tool_result, usage, error, done. Sending raw text means the frontend cannot distinguish tool progress from generation, and cannot render a tool result as a card.
Server: FastAPI with sse-starlette's EventSourceResponse, yielding JSON payloads per event and setting ping=15 for keepalive. The ping matters because proxies drop idle connections, a problem invisible in local testing.
Client: EventSource only issues GET, so long inputs exceed URL limits and need fetch with ReadableStream. EventSource reconnects automatically after about three seconds and re-sends the same request, which re-runs tools; dedupe by request id server-side or close explicitly. Append token text as text nodes, never innerHTML.
Make waiting legible: emit status before each tool, show step n of m when known, and insert interim findings for tasks over 30 seconds. Silence is what users read as broken.
Production traps: proxy_buffering on in Nginx collapses the stream into one burst, so turn it off and raise proxy_read_timeout; managed platforms time out at 30-60 seconds, forcing async job patterns for long tasks; client disconnects must cancel downstream work or you keep paying; do not stream raw chain-of-thought; and retrying after partial output double-bills tokens.
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.