Guides

Streaming

What it is for

Without streaming, you wait for the whole reply. With stream set to true, the reply arrives in pieces as it is written, so you can show text straight away.

Stream a reply

curl https://antarya.ai/v1/chat \
  -H "Authorization: Bearer $ANTARYA_API_KEY" \
  -H "Content-Type: application/json" \
  -N \
  -d '{
    "stream": true,
    "messages": [
      {"role": "user", "content": "Draft a reply that declines the meeting."}
    ]
  }'
Output
event: reply.start
data: {"id": "req_03", "model": "antarya"}

event: content.delta
data: {"index": 0, "type": "text", "text": "Thank you for the invitation. "}

event: content.delta
data: {"index": 0, "type": "text", "text": "I am unable to join on Thursday."}

event: reply.done
data: {"stop_reason": "end_turn", "usage": {"input_tokens": 14, "output_tokens": 19}}

Events

The response is a stream of server-sent events. Each has an event: name and a data: line of JSON.

Event When it arrives Data
reply.start Once, first. id and model.
thinking Only when include_thinking is true, between reply.start and the first content.delta. step, stage and summary.
content.delta Many times. index, type and the next piece of text.
reply.done Once, last. stop_reason and usage.
error If the request fails after the stream has begun. The same error object as other failures, with a request_id.

While Antarya works, the server sends a : ping comment line every 15 seconds so the connection stays open. Ignore it. A thinking event is a short note on progress: stage is a short label and summary says what that step did. Thinking notes are not counted as output tokens.

Join the text of every content.delta in order to get the full reply. Treat reply.done as the end: usage is final there. A request that fails before the stream begins returns an ordinary error status instead of an error event.

Parameters

NameTypeRequiredDescription
streambooleanNoSet to true to receive server-sent events instead of one JSON body. The default is false.
include_thinkingbooleanNoSet to true to receive `thinking` events before the first `content.delta`. The default is false.
messagesarrayYesRequiredThe conversation, as in any chat request.

Next steps