Streaming
Set stream: true to receive the response as server-sent events (SSE): tokens
arrive as they're generated. This is identical to the OpenAI streaming protocol,
so any OpenAI-compatible client handles it out of the box.
Python
from openai import OpenAI
client = OpenAI(
api_key="pgsk_your_key_here",
base_url="https://api.pgsgrove.com/v1",
)
stream = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[{"role": "user", "content": "Explain gradient descent in two sentences."}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
JavaScript / TypeScript
const stream = await client.chat.completions.create({
model: "deepseek-v4-pro",
messages: [{ role: "user", content: "Explain gradient descent in two sentences." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
curl
curl https://api.pgsgrove.com/v1/chat/completions \
-H "Authorization: Bearer $PGS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4-pro",
"stream": true,
"messages": [{ "role": "user", "content": "Count to five." }]
}'
The stream ends with a final data: [DONE] line.
Usage on streamed calls
By default a streamed response does not include a usage summary. To get token
counts back on the final chunk, pass stream_options:
stream = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[...],
stream=True,
stream_options={"include_usage": True},
)
Billing does not depend on this flag; you are metered accurately either way. It's purely so your client can read the token totals.
Cancelling a stream
If your client disconnects, generation stops immediately and you are billed only for what was produced up to that point. There's no need to "drain" a stream you no longer need; just close the connection (in the OpenAI SDKs, break out of the loop or abort the request).
Closing early is the recommended way to cap spend on an over-long response.