Skip to main content
Agent runs stream results as Server-Sent Events (SSE). Each event is a JSON object prefixed with data: on its own line. This guide covers parsing every event type, rendering tool calls in a UI, handling errors, and chaining multi-turn conversations with session IDs.
Prerequisites:
  • An agent created (see Build an Agent guide)
1

Basic streaming request

Send a message to an agent and open an SSE stream. Read lines, filter for data: prefixes, and parse each event as JSON.Endpoint: POST /api/agents/{id}/run/stream
The -N flag in curl disables output buffering so events appear in real time.
2

Parse all event types

Handle every event the stream can emit: start, chunk, tool_call, tool_result, step_started, step_completed, complete, and error.Endpoint: POST /api/agents/{id}/run/stream
3

Handle tool calls in the UI

Display tool calls and results as collapsible cards in a chat UI. Use the tool_call event to show a loading indicator, then update with the tool_result.Endpoint: POST /api/agents/{id}/run/stream
Tool calls always appear in pairs: a tool_call event followed by a tool_result event. Multiple tool calls can occur in a single step if the agent decides to call several tools.
4

Error handling

Handle error events from the stream and connection-level failures. Always wrap your stream reader in a try/catch.Endpoint: POST /api/agents/{id}/run/stream
Error events have an optional code field (e.g., “rate_limited”, “context_length_exceeded”). Always check for HTTP-level errors before reading the stream.
5

Multi-turn with session_id

The start event includes a session_id. Pass it in subsequent requests to continue the conversation with full message history.Endpoint: POST /api/agents/{id}/run/stream
Sessions persist the full conversation history. You can also list session messages via GET /api/sessions/{session_id}/messages.

What’s Next

Streaming Patterns

Understand the SSE protocol and event lifecycle in depth.

Build an Agent

Create an agent from scratch with tools and knowledge bases.

Agents API Reference

Full endpoint documentation for agent runs.