SilentAPI Logo SilentAPI

Streaming

Receive responses in real time as the model generates them

Streaming sends model output as it is generated. You do not need to wait for the full response. Supported text and multimodal models can also stream function calls.

Enabling Streaming

Set stream: true for the OpenAI Chat Completions API or Anthropic Messages API. Use streamGenerateContent for Gemini. SilentAPI returns server-sent events (SSE).

The Completions API and Responses API return a complete JSON response. They do not support streaming on SilentAPI.

API StyleHow to Enable
OpenAI"stream": true in request body
Anthropic"stream": true in request body
GeminiUse the :streamGenerateContent action

OpenAI Streaming Format

When streaming through /v1/chat/completions, the response is a server-sent events (SSE) stream of OpenAI chat completion chunks. The stream ends with data: [DONE].

curl -N https://api.silentapi.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-6.1-terra",
    "messages": [{"role":"user","content":"Write a short poem about the ocean"}],
    "stream": true
  }'

Example events. Cost and usage values are examples:

data: {"id":"chatcmpl_abc","object":"chat.completion.chunk","created":1719584000,"model":"gpt-6.1-terra","choices":[{"index":0,"delta":{"content":"The"},"finish_reason":null}]}

data: {"id":"chatcmpl_abc","object":"chat.completion.chunk","created":1719584000,"model":"gpt-6.1-terra","choices":[{"index":0,"delta":{"content":" ocean"},"finish_reason":null}]}

data: {"id":"chatcmpl_abc","object":"chat.completion.chunk","created":1719584000,"model":"gpt-6.1-terra","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl_abc","object":"chat.completion.chunk","created":1719584000,"model":"gpt-6.1-terra","choices":[],"usage":{"prompt_tokens":8,"completion_tokens":45,"total_tokens":53},"silentapi":{"cost":0.000038,"fallback":null}}

data: [DONE]

Usage and Cost

The final chunk before [DONE] includes these fields:

FieldMeaning
silentapi.costCharge for the request, in credits
silentapi.fallbackID of the fallback model that served the request, or null
silentapi.request_idID of the request, the same as the X-Request-Id header and the Request Log
usageToken usage. Only present when the service reports usage.

Using the OpenAI SDK

from openai import OpenAI

client = OpenAI(api_key="YOUR_API_KEY", base_url="https://api.silentapi.com/v1")

stream = client.chat.completions.create(
    model="gpt-6.1-terra",
    messages=[{"role": "user", "content": "Write a poem"}],
    stream=True
)

for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="")

Anthropic Streaming Format

When streaming via /v1/messages, the response is an SSE stream with named events matching the Anthropic streaming specification.

curl -N https://api.silentapi.com/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_API_KEY" \
  -d '{
    "model": "claude-sonnet-5.5",
    "max_tokens": 1024,
    "messages": [{"role":"user","content":"Write a haiku about rain"}],
    "stream": true
  }'

The Anthropic Messages API sends these events in order:

  1. message_start: Message metadata.
  2. content_block_start: Starts a content block.
  3. content_block_delta: Adds content to the block.
  4. content_block_stop: Ends the content block.
  5. message_delta: Stop reason and usage, with silentapi.cost.
  6. message_stop: Ends the stream.
event: message_start
data: {"type":"message_start","message":{"id":"msg_abc","type":"message","role":"assistant","content":[],"usage":{"input_tokens":7,"output_tokens":0}}}

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

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

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

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

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":12},"silentapi":{"cost":0.000045,"fallback":null}}

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

Tool Use Streaming

A function call uses these events:

EventContent
content_block_startStarts the block. content_block.type is tool_use.
content_block_deltadelta.type is input_json_delta. delta.partial_json contains part of the function input JSON.
content_block_stopEnds the block

Using the Anthropic SDK

import anthropic

client = anthropic.Anthropic(
    api_key="YOUR_API_KEY",
    base_url="https://api.silentapi.com"
)

with client.messages.stream(
    model="claude-sonnet-5.5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a haiku"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Gemini Streaming Format

When streaming through :streamGenerateContent, the response is a server-sent events (SSE) stream of Gemini response chunks.

curl -N "https://api.silentapi.com/v1beta/models/gemini-4-neon:streamGenerateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: YOUR_API_KEY" \
  -d '{
    "contents": [{ "parts": [{ "text": "Describe a sunset" }] }]
  }'

Each chunk contains candidate parts with incremental text:

data: {"candidates":[{"content":{"parts":[{"text":"The sun"}],"role":"model"}}]}

data: {"candidates":[{"content":{"parts":[{"text":" dips below"}],"role":"model"}}]}

data: {"candidates":[{"content":{"parts":[],"role":"model"},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":4,"candidatesTokenCount":32,"totalTokenCount":36},"silentapi":{"cost":0.000021,"fallback":null}}

Using the Google GenAI SDK

from google import genai

client = genai.Client(
    api_key="YOUR_API_KEY",
    http_options={"base_url": "https://api.silentapi.com"}
)

response = client.models.generate_content_stream(
    model="gemini-4-neon",
    contents="Describe a sunset"
)

for chunk in response:
    if chunk.text:
        print(chunk.text, end="")

Cross-Format Streaming

SilentAPI translates between streaming formats. A request to /v1/messages returns Anthropic SSE events.

Request EndpointModelStream Format You Receive
/v1/chat/completionsSupported text or multimodal modelOpenAI SSE
/v1/messagesSupported text or multimodal modelAnthropic SSE
:streamGenerateContentSupported text or multimodal modelGemini SSE

Keep-Alive Comments

While a model works without sending output, for example during long reasoning, OpenAI and Anthropic streams receive the comment line : keep-alive every 15 seconds. SSE clients and SDKs ignore comment lines, so a slow request keeps its connection open and can be told apart from a stalled one. Gemini streams do not receive comments, because some Gemini SDKs cannot read them.

: keep-alive

: keep-alive

data: {"id":"chatcmpl_abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"The"},"finish_reason":null}]}

Time Limit

Each request has a time limit that is set for the account and starts at 1 hour, for streaming and non-streaming requests alike. A request that reaches its limit is stopped and not charged. The response is a 504 error with the code time_limit.

data: {"error":{"message":"The request reached its dynamic time limit and was stopped, it was not charged.","type":"upstream_error","param":null,"code":"time_limit"}}

data: [DONE]

Failed Streams

When a stream fails, SilentAPI still sends the usage and the charged cost, which is 0, so your logs can record the request. OpenAI streams send a final chunk with empty choices right before the error event. Anthropic and Gemini streams add usage or usageMetadata and silentapi to the error event. When the service reported no usage, the token counts are estimates.

data: {"id":"chatcmpl_abc","object":"chat.completion.chunk","created":1719584000,"model":"gpt-6.1-terra","choices":[],"usage":{"prompt_tokens":48210,"completion_tokens":0,"total_tokens":48210},"silentapi":{"cost":0,"fallback":null,"request_id":"req_0b9c8d7e6f5a4b3c2d1e0f9a"}}

data: {"error":{"message":"The service could not complete the request, try again shortly.","type":"upstream_error","param":null,"code":"bad_gateway"}}

data: [DONE]

Billing During Streaming

Streaming uses the same model prices as requests without streaming. The cost appears in silentapi.cost in the final event.

A stream that fails on our side, including one that reaches its time limit, is never charged, and this happens automatically.

If your client closes the connection before the stream ends, the first 20 closed streams each day are free. After that, SilentAPI charges the usage delivered so far, and if the service reported no usage, it estimates the tokens from the content received.

SilentAPI can use a fallback while it opens a stream, but it cannot switch to another model once output has started. If the service fails during output, the stream ends.