FromZero2AI
CoursesPlaygroundBlog
Support on Ko-fi
Structured Outputs & AI API Design • Module A: Structured GenerationLesson 2: Streaming LLM Responses
PreviousNext

Lesson 2: Streaming LLM Responses

Implement real-time token streaming in FastAPI using Server-Sent Events (SSE). Handle streaming structured output, partial JSON validation, and client-side rendering of incremental responses.

Built with AI for beginners. Free forever.

Support on Ko-fi•About•Blog•Privacy Policy•Terms of Service

Streaming sends tokens to the client as they are generated — instead of waiting for the full response. For long responses, this cuts perceived latency from 5 seconds to <1 second. Every production chat interface uses streaming.

Streaming Pattern Selector

When to use

Direct Python scripts, CLI tools, or backend jobs where you want to print tokens as they arrive.

Setup

pip install anthropic — no extra dependencies.

import anthropic

client = anthropic.Anthropic()

# Method 1: stream context manager (recommended)
with client.messages.stream(
    model="claude-haiku-4-5-20251001",
    max_tokens=500,
    messages=[{"role": "user", "content": "Write a 3-paragraph essay about neural networks."}],
) as stream:
    for text_chunk in stream.text_stream:
        print(text_chunk, end="", flush=True)   # Print each token as it arrives
    final_message = stream.get_final_message()
    print(f"\nTotal tokens: {final_message.usage.input_tokens + final_message.usage.output_tokens}")

# Method 2: raw event stream (for custom event handling)
with client.messages.stream(
    model="claude-haiku-4-5-20251001",
    max_tokens=500,
    messages=[{"role": "user", "content": "Explain transformers."}],
) as stream:
    for event in stream:
        if event.type == "content_block_delta":
            print(event.delta.text, end="", flush=True)
        elif event.type == "message_stop":
            print("\n[Done]")

Set read timeout, not connect timeout, for streaming.A streaming response can take 30s+ to complete while receiving data continuously. Set httpx.Timeout(connect=5.0, read=60.0) — a short connect timeout to detect dead servers, but a long read timeout to allow the stream to complete.

Lesson 3 covers retry and fallback patterns for structured outputs — what to do when validation fails or the API returns an error mid-stream.