Streaming
Set "stream": true and the response arrives as Server-Sent
Events — token deltas as they are generated, in the OpenAI chunk shape.
Enabling streaming
curl https://api.astrodyne.ai/v1/chat/completions \
-H "Authorization: Bearer $ASTRODYNE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "YOUR_MODEL_ID",
"stream": true,
"messages": [{"role": "user", "content": "Hello"}]
}'
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.astrodyne.ai/v1",
api_key=os.environ["ASTRODYNE_API_KEY"],
)
stream = client.chat.completions.create(
model="YOUR_MODEL_ID",
messages=[{"role": "user", "content": "Hello"}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.astrodyne.ai/v1",
apiKey: process.env.ASTRODYNE_API_KEY,
});
const stream = await client.chat.completions.create({
model: "YOUR_MODEL_ID",
messages: [{ role: "user", content: "Hello" }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
How chunks arrive
The response has Content-Type: text/event-stream. Each event is
a data: line containing a JSON chunk; text arrives at
choices[0].delta.content. A final chunk carries
finish_reason, then the stream ends with:
data: {"object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
What is available during the stream
- The
X-Astrodyne-Request-Idheader is on the response from the start — capture it before reading the body. - Token usage is not included in stream chunks. Authoritative usage and the settled charge are recorded when the stream completes, and appear with the request in the Request Explorer.
Mid-stream failures and disconnects
If a failure occurs after streaming has begun, Astrodyne emits one sanitized
error event in the standard error shape with
"object": "error", then closes the stream without
[DONE]. If your client disconnects mid-stream, the request is
settled for what was actually delivered — you are never charged when nothing
was delivered, and the final state is visible in the Request Explorer either
way.
AbortController)
is the supported way to stop a stream early. The Playground's Stop button does
exactly this.