stream: true, process
each event in order, and close the response when the terminal event arrives.
OpenAI Chat Completions
data:. The final event is data: [DONE]. A final
usage chunk may have an empty choices array.
OpenAI Responses
response.completed or response.incomplete.
Function arguments arrive in response.function_call_arguments.delta events.
Anthropic Messages
message_start,
content_block_delta, message_delta, and message_stop. Use the official SDK
to assemble content blocks and tool inputs.
Disconnects and retries
A disconnected stream may already have produced billable tokens. Do not append a blind retry to the partial assistant message.- Record whether the terminal event was received.
- If no user-visible output was consumed, retry with exponential backoff and jitter.
- If partial output was consumed, either show it as interrupted or restart the turn as a new request.
- For
429, honorRetry-Afterbefore retrying. - Do not retry
400or401until the request or credential is corrected.