Skip to content
Sign in

API and SDKs

Streaming

Receive the answer as it is written, as server-sent events, in the OpenAI, Anthropic or Gemini format.

On this page

Every endpoint that writes text can send its answer as server-sent events (text/event-stream), so the first words show while the rest is still being written. Each format turns streaming on in its own way and names its events in its own words; usage, time limits and failures midway work the same in all of them. To install and configure a client, see SDKs.

KeyLoading your keys

Chat Completions

Set stream to true. The answer comes as chat.completion.chunk objects, one per data: line.

curl -N https://api.impact-relay.com/v1/chat/completions \  -H "Authorization: Bearer $IMPACT_API_KEY" \  -H "Content-Type: application/json" \  -d '{    "model": "claude-sonnet-5-5",    "stream": true,    "stream_options": { "include_usage": true },    "messages": [{ "role": "user", "content": "Hello!" }]  }'
data: {"id":"gen_5b1f0c9e2a7d4e63b8a4c2f1d09e7a35","object":"chat.completion.chunk","created":1790000000,"model":"claude-sonnet-5-5","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}data: {"id":"gen_5b1f0c9e2a7d4e63b8a4c2f1d09e7a35","object":"chat.completion.chunk","created":1790000000,"model":"claude-sonnet-5-5","choices":[{"index":0,"delta":{"content":"Hello there,"},"finish_reason":null}]}data: {"id":"gen_5b1f0c9e2a7d4e63b8a4c2f1d09e7a35","object":"chat.completion.chunk","created":1790000000,"model":"claude-sonnet-5-5","choices":[{"index":0,"delta":{"content":" nice to meet you!"},"finish_reason":null}]}data: {"id":"gen_5b1f0c9e2a7d4e63b8a4c2f1d09e7a35","object":"chat.completion.chunk","created":1790000000,"model":"claude-sonnet-5-5","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}data: {"id":"gen_5b1f0c9e2a7d4e63b8a4c2f1d09e7a35","object":"chat.completion.chunk","created":1790000000,"model":"claude-sonnet-5-5","choices":[],"usage":{"prompt_tokens":14,"prompt_tokens_details":{"cached_tokens":0},"completion_tokens":8,"completion_tokens_details":{"reasoning_tokens":0},"total_tokens":22,"cost":0.000023}}data: [DONE]

The first chunk opens the message with delta.role. Text then arrives in delta.content, reasoning in delta.reasoning_content, and a function call in delta.tool_calls. The answer's last chunk has the finish_reason, a usage chunk follows if you asked for one, and data: [DONE] ends the stream.

Responses

Set stream to true. Every event has a name, as OpenAI names them, and its data: line repeats the name in type.

curl -N https://api.impact-relay.com/v1/responses \  -H "Authorization: Bearer $IMPACT_API_KEY" \  -H "Content-Type: application/json" \  -d '{    "model": "claude-sonnet-5-5",    "stream": true,    "input": "Hello!"  }'
event: response.createddata: {"type":"response.created","sequence_number":0,"response":{"id":"gen_5b1f0c9e2a7d4e63b8a4c2f1d09e7a35","object":"response","created_at":1790000000,"model":"claude-sonnet-5-5","error":null,"incomplete_details":null,"status":"in_progress","output":[]}}event: response.in_progressdata: {"type":"response.in_progress","sequence_number":1,"response":{"id":"gen_5b1f0c9e2a7d4e63b8a4c2f1d09e7a35","object":"response","created_at":1790000000,"model":"claude-sonnet-5-5","error":null,"incomplete_details":null,"status":"in_progress","output":[]}}event: response.output_item.addeddata: {"type":"response.output_item.added","sequence_number":2,"output_index":0,"item":{"id":"msg_2f8c1d0a9b7e4c53a6d1e0f4b3c29a17","type":"message","role":"assistant","status":"in_progress","content":[]}}event: response.content_part.addeddata: {"type":"response.content_part.added","sequence_number":3,"item_id":"msg_2f8c1d0a9b7e4c53a6d1e0f4b3c29a17","output_index":0,"content_index":0,"part":{"type":"output_text","text":"","annotations":[]}}event: response.output_text.deltadata: {"type":"response.output_text.delta","sequence_number":4,"item_id":"msg_2f8c1d0a9b7e4c53a6d1e0f4b3c29a17","output_index":0,"content_index":0,"delta":"Hello! How can I"}event: response.output_text.deltadata: {"type":"response.output_text.delta","sequence_number":5,"item_id":"msg_2f8c1d0a9b7e4c53a6d1e0f4b3c29a17","output_index":0,"content_index":0,"delta":" help you today?"}event: response.output_text.donedata: {"type":"response.output_text.done","sequence_number":6,"item_id":"msg_2f8c1d0a9b7e4c53a6d1e0f4b3c29a17","output_index":0,"content_index":0,"text":"Hello! How can I help you today?"}event: response.content_part.donedata: {"type":"response.content_part.done","sequence_number":7,"item_id":"msg_2f8c1d0a9b7e4c53a6d1e0f4b3c29a17","output_index":0,"content_index":0,"part":{"type":"output_text","text":"Hello! How can I help you today?","annotations":[]}}event: response.output_item.donedata: {"type":"response.output_item.done","sequence_number":8,"output_index":0,"item":{"id":"msg_2f8c1d0a9b7e4c53a6d1e0f4b3c29a17","type":"message","role":"assistant","status":"completed","content":[{"type":"output_text","text":"Hello! How can I help you today?","annotations":[]}]}}event: response.completeddata: {"type":"response.completed","sequence_number":9,"response":{"id":"gen_5b1f0c9e2a7d4e63b8a4c2f1d09e7a35","object":"response","created_at":1790000000,"model":"claude-sonnet-5-5","error":null,"incomplete_details":null,"status":"completed","output":[{"id":"msg_2f8c1d0a9b7e4c53a6d1e0f4b3c29a17","type":"message","role":"assistant","status":"completed","content":[{"type":"output_text","text":"Hello! How can I help you today?","annotations":[]}]}],"usage":{"input_tokens":12,"input_tokens_details":{"cached_tokens":0},"output_tokens":10,"output_tokens_details":{"reasoning_tokens":0},"total_tokens":22}}}

response.created opens the response. Each item of the output opens with response.output_item.added and closes with response.output_item.done; between them text arrives in response.output_text.delta and a function call's arguments in response.function_call_arguments.delta. response.completed ends the stream with the whole response, or response.incomplete when the answer was cut short. Every event in order: Responses.

Messages

Set stream to true. Every event has a name, as Anthropic names them; the SDKs' messages.stream reads them for you.

curl -N https://api.impact-relay.com/v1/messages \  -H "x-api-key: $IMPACT_API_KEY" \  -H "anthropic-version: 2023-06-01" \  -H "content-type: application/json" \  -d '{    "model": "claude-sonnet-5-5",    "max_tokens": 1024,    "stream": true,    "messages": [{ "role": "user", "content": "Hello!" }]  }'
event: message_startdata: {"type":"message_start","message":{"id":"msg_gen_5b1f0c9e2a7d4e63b8a4c2f1d09e7a35","type":"message","role":"assistant","model":"claude-sonnet-5-5","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":12,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":1}}}event: content_block_startdata: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}event: content_block_deltadata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello! How can I help you today?"}}event: content_block_stopdata: {"type":"content_block_stop","index":0}event: message_deltadata: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"input_tokens":12,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":10}}event: message_stopdata: {"type":"message_stop"}

message_start opens the message. Each block of content is a content_block_start, its pieces as content_block_delta events, and a content_block_stop. message_delta carries the stop_reason, and message_stop ends the stream.

generateContent

Streaming is a method of its own: send the same body to :streamGenerateContent with ?alt=sse in the URL. Google's SDKs add alt=sse themselves.

curl -N "https://api.impact-relay.com/v1beta/models/gemini-3.8-flash:streamGenerateContent?alt=sse" \  -H "x-goog-api-key: $IMPACT_API_KEY" \  -H "Content-Type: application/json" \  -d '{    "contents": [{ "parts": [{ "text": "Hello!" }] }]  }'
data: {"candidates":[{"content":{"parts":[{"text":"Hello! How can I"}],"role":"model"},"index":0}],"modelVersion":"gemini-3.8-flash","responseId":"gen_5b1f0c9e2a7d4e63b8a4c2f1d09e7a35"}data: {"candidates":[{"content":{"parts":[{"text":" help you today?"}],"role":"model"},"index":0}],"modelVersion":"gemini-3.8-flash","responseId":"gen_5b1f0c9e2a7d4e63b8a4c2f1d09e7a35"}data: {"candidates":[{"content":{"parts":[{"text":""}],"role":"model"},"finishReason":"STOP","index":0}],"usageMetadata":{"promptTokenCount":2,"candidatesTokenCount":9,"totalTokenCount":11},"modelVersion":"gemini-3.8-flash","responseId":"gen_5b1f0c9e2a7d4e63b8a4c2f1d09e7a35"}

Each event is one data: line holding a whole response with the next parts of the answer in candidates. The last one carries the finishReason. There is no closing event: the stream ends when the connection closes.

Usage and cost

Each format reports the tokens a stream used in its own place:

  • Chat Completions. Only if the request sets stream_options.include_usage: the last chunk before [DONE] then has empty choices and a usage object, with the request's cost in dollars.
  • Responses. response.completed carries the whole response, usage included.
  • Messages. message_start has an estimate of the input in usage, and message_delta the exact counts.
  • generateContent. The last event carries usageMetadata.

Whatever the stream reports, Logs record the tokens and the cost of every request.

Long requests

A request has 30 minutes from its start to its last token. While a model thinks, a keep-alive every 5 seconds stops proxies from closing a quiet connection. The official SDKs skip these frames by themselves, except on /v1/responses: every stream there carries response.heartbeat events, which the OpenAI SDKs pass to your loop. Ignore the event types you don't handle.

Time limits and the keep-alive of each format
  • Time limits. An answer has 2 minutes to start. Once a stream runs, 1 to 5 minutes without output, depending on the model, end it with an upstream_timeout error. A request that passes 30 minutes ends the same way.
  • Keep-alives. /v1/chat/completions sends an SSE comment (: keep-alive), /v1/messages a ping event, :streamGenerateContent a blank line, and /v1/responses a response.heartbeat event. OpenAI's own API has no response.heartbeat: skip it, in a loop over the SDK's events as in a client that reads the stream itself.

Errors mid-stream

Once a stream has started the status is already 200, so a failure arrives as the stream's last event. On Chat Completions, Messages and generateContent the official SDKs raise it as an exception while you read the stream. On Responses the stream ends with a response.failed event, which the SDKs hand to your loop like any other: check for it, as the example does. Retry the whole request, as Errors says for its code.

data: {"error":{"code":"upstream_error","message":"The model failed to answer. Try again.","type":"upstream_error"}}
The error event of each format, overload and cancelling
  • Chat Completions. A data: line holding an error object, in place of [DONE], as above.
  • Responses. A response.failed event: response.error has the code, message and type. The OpenAI SDKs deliver it as an event and raise nothing.
  • Messages. An error event with the body of an Anthropic-format error.
  • generateContent. A last data: line that holds only error, in the shape of a Gemini-format error.
  • Overload. A provider that stays overloaded ends the stream with an overload error: server_is_overloaded on Chat Completions and Responses, overloaded_error on /v1/messages.
  • Cancelling. Closing the connection cancels the request at the provider, and Logs show it as Cancelled.

A stream that failed or was cancelled is charged for what was generated before it ended: nothing if nothing was streamed. When the provider didn't report the final token counts, Impact estimates them from the prompt and the output that was streamed.