← Back to docs

Streaming

Handle events, usage and terminal errors.

A stream may contain usage events with no choices, completion markers or errors after output has begun. Process each event and check the terminal state; an initial HTTP 200 does not prove completion.

Time to first byte and the idle-stream timeout are different from the route time cap. That cap depends on the endpoint, the model and configuration, and defaults to 600,000ms for non-streaming calls. A client, proxy or provider may end the call sooner; this is not a duration guarantee.

Streaming

event: message_start
data: {"type":"message_start","message":{"id":"msg_...","role":"assistant","content":[],"stop_reason":null}}

event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}

event: content_block_delta
data: {"delta":{"text":"Hello","type":"text_delta"},"index":0,"type":"content_block_delta"}

event: content_block_stop
data: {"type":"content_block_stop","index":0}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":128}}

event: message_stop
data: {"type":"message_stop"}

Response headers

HTTP headersMeaning
x-request-idSave x-request-id and the error timestamp for support; some legacy routes return x-api-request-id instead. Never include API keys or sensitive data in a bug report.
Retry-AfterHonor Retry-After and use bounded exponential backoff with jitter. Cap attempts and total time. For video jobs that may already exist, look up the job ID before submitting again.
x-api-engineConfirms the response was served by the ThAPI gateway.
x-api-billed-usageBilled token usage for a non-streaming call. Streaming responses do not include this header.

For streaming requests, token/cost headers are not available (cost is only known when the stream ends) — check the Usage page.

Snippets are protocol-verified; run a small test call and verify the Usage log before deploying to production.