How to Stream Tokens from a Mock LLM in Python

Simulate real-time LLM streaming by yielding tokens one at a time with a delay, making it easy to test streaming UIs.

Easy Python 3.9+ Aug 9, 2026 AI & LLM integration patterns 15 views 0 copies

Python code

18 lines
Python 3.9+
import time
from typing import Generator


def stream_tokens(text: str, delay: float = 0.05) -> Generator[str, None, None]:
    """Simulate an LLM streaming tokens word by word."""
    for word in text.split():
        yield word
        time.sleep(delay)


if __name__ == "__main__":
    sample = "Hello world! This is a simulated streaming output."
    collected = []
    for token in stream_tokens(sample):
        print(token, end=" ", flush=True)
        collected.append(token)
    print("\nCollected tokens:", collected)

Output

stdout
Hello world! This is a simulated streaming output. 
Collected tokens: ['Hello', 'world!', 'This', 'is', 'a', 'simulated', 'streaming', 'output.']

How it works

The stream_tokens function is a generator — it uses yield instead of return, so each call to next() (or iteration) resumes execution right after the last yield, producing one word at a time. The time.sleep adds a realistic delay to mimic network latency, making the streaming visible in the output. The typing.Generator annotation clarifies the return type to static checkers and readers.

Common mistakes

  • Using `return` instead of `yield`, which turns the function into a regular function and breaks the iteration.
  • Forgetting `flush=True` in `print`, causing output to appear all at once instead of incrementally.
  • Calling the generator function without iterating over it (must use `for` loop or `next()`).

Variations

  1. Use `yield from` with a sub-generator to chunk longer text into fixed-size batches.
  2. Yield full lines or sentences instead of words by splitting on `.` or `\n`.

Real-world use cases

  • Testing a chat UI in development without a live LLM API — iterate on the streaming display logic locally.
  • Demoing token-by-token streaming at a tech talk or internal showcase where a real API call is impractical.
  • Stress-testing a frontend that renders partial model responses by varying the delay parameter.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from AI & LLM integration patterns

Related tutorials and quizzes for this topic.