Streaming

Demonstrates streaming agent responses token by token.

1"""
2Streaming
3=============================
4
5Demonstrates streaming agent responses token by token.
6"""
7
8from kern.agent import Agent
9from kern.models.openai import OpenAIResponses
10
11# ---------------------------------------------------------------------------
12# Create Agent
13# ---------------------------------------------------------------------------
14agent = Agent(
15 model=OpenAIResponses(id="gpt-5.2"),
16 markdown=True,
17)
18
19# ---------------------------------------------------------------------------
20# Run Agent
21# ---------------------------------------------------------------------------
22if __name__ == "__main__":
23 # Stream the response token by token
24 agent.print_response(
25 "Explain the difference between concurrency and parallelism.",
26 stream=True,
27 )

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/02_input_output
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python streaming.py