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 Style | How to Enable |
|---|---|
| OpenAI | "stream": true in request body |
| Anthropic | "stream": true in request body |
| Gemini | Use 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-sol",
"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-sol","choices":[{"index":0,"delta":{"content":"The"},"finish_reason":null}]}
data: {"id":"chatcmpl_abc","object":"chat.completion.chunk","created":1719584000,"model":"gpt-6-sol","choices":[{"index":0,"delta":{"content":" ocean"},"finish_reason":null}]}
data: {"id":"chatcmpl_abc","object":"chat.completion.chunk","created":1719584000,"model":"gpt-6-sol","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: {"id":"chatcmpl_abc","object":"chat.completion.chunk","created":1719584000,"model":"gpt-6-sol","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:
| Field | Meaning |
|---|---|
| silentapi.cost | Charge for the request, in credits |
| silentapi.fallback | ID of the fallback model that served the request, or null |
| usage | Token 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-sol",
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:
message_start: Message metadata.content_block_start: Starts a content block.content_block_delta: Adds content to the block.content_block_stop: Ends the content block.message_delta: Stop reason and usage, withsilentapi.cost.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:
| Event | Content |
|---|---|
| content_block_start | Starts the block. content_block.type is tool_use. |
| content_block_delta | delta.type is input_json_delta. delta.partial_json contains part of the function input JSON. |
| content_block_stop | Ends 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-3.8-flash: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-3.8-flash",
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 Endpoint | Model | Stream Format You Receive |
|---|---|---|
| /v1/chat/completions | Supported text or multimodal model | OpenAI SSE |
| /v1/messages | Supported text or multimodal model | Anthropic SSE |
| :streamGenerateContent | Supported text or multimodal model | Gemini SSE |
Billing During Streaming
Streaming uses the same model prices as requests without streaming. The cost appears in silentapi.cost in the final event after successful completion.
If a stream is interrupted, SilentAPI charges the reported usage. If usage is absent, it estimates input tokens and output tokens from the content received.
A service failure before output or usage releases the hold. An interrupted stream can close before it sends the cost event.
SilentAPI can use a fallback while it opens a stream. It cannot retry with another model after output starts. If the service fails during output, the stream ends.