data: on its own line. This guide walks through parsing every event type, rendering tool calls in a UI, handling errors gracefully, and chaining multi-turn conversations with session IDs.
Prerequisites:
- An agent created (see Build an Agent guide)
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/streamThe -N flag in curl disables output buffering so events appear in real time.
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/streamHandle 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/streamTool 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.
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/streamError events have an optional
code field (e.g., “rate_limited”, “context_length_exceeded”). Always check for HTTP-level errors before reading the stream.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/streamSessions 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.