Streaming
Token streaming, and the reasoning-first caveat for UIs.
Supported and recommended for anything user-facing:
{"model": "cf-chat", "messages": [...], "stream": true}stream = client.chat.completions.create(
model="cf-chat",
messages=[{"role": "user", "content": "Write a haiku about GPUs."}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)With stream_options.include_usage, the final chunk carries prompt and
completion token counts.
What the example prints
The loop above prints each content delta as it arrives, so the answer
appears fragment by fragment:
Thousands of cores hum
fanning out to paint the dark,
green light in the caseThe same response as it arrived on the wire, one chunk per delta (from a real run; note chunks split mid-word):
"\n\nThousands" " of" " cores" " hum" "\n" "f" "anning" " out" " to"
" paint" " the" " dark" "," "\n" "green" " light" " in" " the" " case"In that run, those 19 content chunks were preceded by 201
reasoning_content chunks that the loop deliberately skips.
The first chunk is thinking, not the answer
One caveat if you are driving a UI. The first chunk arrives fast (about
0.4 s in the run above), but it is reasoning, not the answer: the
first chunk of actual content lands only once the model has finished
thinking (2.2 s in that run; longer when the model thinks harder). So
either render the reasoning as it streams, or show a thinking indicator
until content starts, otherwise the user watches an empty box.