Skip to content
All docs

Guides

Streaming

Set stream: true and the reply arrives word by word, as OpenAI’s server-sent events. Ask for usage and the last chunk says what the call cost.

Checked 26 October 2026

Stream a reply

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://nokeep.ai/v1",
    api_key=os.environ["NOKEEP_API_KEY"],
)

stream = client.chat.completions.create(
    model="deepseek-v4.1-flash-uncensored",
    messages=[{"role": "user", "content": "Tell me a story in three lines."}],
    stream=True,
    stream_options={"include_usage": True},
)
for chunk in stream:
    if chunk.choices:
        print(chunk.choices[0].delta.content or "", end="", flush=True)
    if chunk.usage:
        print("\ncredits:", chunk.usage.model_extra.get("nokeep_credits"))

What arrives

The stream
data: {"choices":[{"index":0,"delta":{"role":"assistant","content":""}}]}
data: {"choices":[{"index":0,"delta":{"content":"The"}}]}
data: {"choices":[{"index":0,"delta":{"content":" lighthouse"}}]}
…
data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: {"choices":[],"usage":{"prompt_tokens":18,"completion_tokens":61,"nokeep_credits":0}}
data: [DONE]
  • Words arrive in delta.content. With thinking on, the thinking arrives first, in delta.reasoning_content.
  • Tool calls arrive in delta.tool_calls, their arguments in pieces, as OpenAI sends them.
  • With stream_options.include_usage, a last chunk with an empty choices list carries usage, including nokeep_credits, what the call cost.
  • The stream ends with data: [DONE].

Stop a reply early

Close the connection and the model stops writing. You pay for what was sent before you stopped, and nothing after. In the SDKs, break out of the loop and close the stream; in an app, press its Stop button.

Python
for chunk in stream:
    text = chunk.choices[0].delta.content if chunk.choices else ""
    if text and "THE END" in text:
        stream.close()  # the model stops here, and so does the bill
        break

When the model stops first

If the model ends a stream before it finishes, the last event before [DONE] is an error in OpenAI’s shape, with the code stream_cut. What it wrote is billed, nothing more. Retry, or continue from what arrived.

A cut stream
data: {"error":{"type":"api_error","code":"stream_cut","message":"The model stopped before it finished. What it wrote is billed, nothing more."}}
data: [DONE]

Questions

Does streaming cost more?

No. A streamed reply costs the same tokens as a whole one.

Why is there no x-nokeep-credits header on a stream?

The headers are sent before the first word, when the cost isn’t known yet. Ask for usage and read nokeep_credits in the last chunk.