Stream an agent reply
Watch a long turn as it happens, so your script can show progress instead of waiting two minutes for an answer.
The examples use the ELOWEN_URL and ELOWEN_TOKEN variables set up in Send your first agent API request.
Send a streaming request
Add "stream": true to the run. The answer arrives as server-sent events, so keep the connection open:
curl -sSN "$ELOWEN_URL/api/v1/agent/runs" \
-H "Authorization: Bearer $ELOWEN_TOKEN" \
-H "Content-Type: application/json" \
-d '{"message":"Check the open pull requests","stream":true}'
The response type is text/event-stream. Each event has a name and a JSON data line. Any server-sent-events client works; a plain read of the response body works too.
Read the events
event: start
data: {"conversationId":"brain-12-…"}
event: tool
data: {"phase":"start","id":"t1","name":"Bash","detail":"gh pr list"}
event: tool
data: {"phase":"end","id":"t1","ok":true}
event: delta
data: {"text":"There are three open"}
event: delta
data: {"text":" pull requests…"}
event: final
data: {"conversationId":"brain-12-…","reply":"There are three open pull requests…","usage":{"tokens":2210,"costUsd":0.019}}
| Event | Data |
|---|---|
start |
{ conversationId }, always first. |
delta |
{ text }, the next piece of the answer. |
tool |
{ phase: "start", id?, name, detail? } when a tool starts, { phase: "end", id?, ok } when it ends. |
error |
{ message } when the turn fails. The stream then ends without final. |
final |
{ conversationId, reply, usage } when the turn is finished. |
Collect the delta pieces in order for the full answer, or use final, which carries the same reply and its usage.
When a turn fails
A turn that fails during the stream sends error and ends without final. The event carries the failure message.
A turn that cannot start at all answers 500 instead of opening a stream: for example when the agent is unavailable or the model you asked for could not be switched to. The body then carries { "error": "…", "conversationId": "…" }.
If you disconnect
The turn keeps running when your script goes away, and its answer lands in the conversation. You can pick it up there, or continue the conversation later with its conversationId.
Related guides
See Send your first agent API request for the fields of a run, and Agent API errors for the status codes a stream can answer with.