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.
Python code
18 linesimport 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
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
- Use `yield from` with a sub-generator to chunk longer text into fixed-size batches.
- 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
More from AI & LLM integration patterns
- Cache LLM Completions by Hashing the Prompt in Python easy
- Chain of Thought Prompting in Python: Step-by-Step Reasoning Demo easy
- Circuit Breaker Pattern in Python for LLM API Calls medium
- Cosine Similarity to Retrieve Top K Chunks in Python easy
- Demonstrate Prompt Injection Bypass in Python easy
- How to Accumulate Streamed Tokens into a Final String in Python easy
Keep learning
Related tutorials and quizzes for this topic.