Streaming Responses (SSE)
Stream LLM output token-by-token over Server-Sent Events for snappy UIs and progress indication.
Streaming Responses (SSE) is a free AI Agents lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Stream?
Without streaming, you wait for the full response before showing anything. With a 50-token response that takes 5 seconds, the user sees nothing for 5 seconds.
Streaming shows tokens as they arrive — same total time, but the UX feels instant.
Server-Sent Events (SSE)
OpenAI and Anthropic stream over SSE — a one-way HTTP protocol where the server pushes events with format data: {...}\n\n.
Most SDKs hide SSE behind an iterator interface.
Streaming with OpenAI
Set stream=True and iterate the response:
stream = client.chat.completions.create(
model='gpt-4o-mini',
messages=messages,
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end='', flush=True)Collecting the Full Response
Accumulate deltas to get the final string:
buf = []
for chunk in stream:
delta = chunk.choices[0].delta.content or ''
buf.append(delta)
print(delta, end='', flush=True)
full_text = ''.join(buf)
messages.append({'role': 'assistant', 'content': full_text})Streaming with Anthropic
Same pattern, slightly different API:
with client.messages.stream(
model='claude-sonnet-4-5',
max_tokens=1024,
messages=messages,
) as stream:
for text in stream.text_stream:
print(text, end='', flush=True)
final = stream.get_final_message()
print('\ndone, tokens:', final.usage.output_tokens)Streaming Tool Calls
Tool calls also stream. With OpenAI, the function name and arguments arrive in pieces:
for chunk in stream:
delta = chunk.choices[0].delta
if delta.tool_calls:
for tc in delta.tool_calls:
# tc.function.name and tc.function.arguments arrive as partial strings
passBackpressure and Cancellation
If the user closes the page, abort the stream to stop burning tokens:
try:
for chunk in stream:
if request_was_cancelled():
stream.close()
break
finally:
stream.close()Streaming Through a Web Server
For FastAPI, use StreamingResponse:
from fastapi.responses import StreamingResponse
def token_generator():
for chunk in stream:
delta = chunk.choices[0].delta.content or ''
yield f'data: {delta}\n\n'
return StreamingResponse(token_generator(), media_type='text/event-stream')Buffer for Sentences
For text-to-speech or chunked display, buffer until a sentence ends:
buf = ''
for chunk in stream:
buf += chunk.choices[0].delta.content or ''
while '. ' in buf:
sentence, buf = buf.split('. ', 1)
speak(sentence + '.')Parsing Streamed JSON
For JSON outputs, you cannot parse mid-stream. Options:
- Buffer the full output then parse once
- Use a streaming JSON parser (
ijson) to emit fields as they complete
Latency Metrics: TTFT and TPS
- TTFT — time to first token (perceived responsiveness)
- TPS — tokens per second (throughput)
Streaming optimizes TTFT, not total time. Aim for TTFT < 500ms for chat UX.
Why Stream?
What is the primary benefit of streaming?
Recap
Streaming is a UX win, not a speed win. Implement it in any agent that talks to a human in real time.
Frequently asked questions
Is the “Streaming Responses (SSE)” lesson free?
Yes — the full text of “Streaming Responses (SSE)” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Streaming Responses (SSE)”?
Stream LLM output token-by-token over Server-Sent Events for snappy UIs and progress indication. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Streaming Responses (SSE)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.