Stream agent responses token by token in real time.
Stream agent responses to display output as it is generated.
1
Create the agent
agent_streaming.py
from definable.agent import Agentagent = Agent( model="gpt-4o", instructions="You are a storyteller. Write vivid, engaging stories.",)# Sync streamingfor event in agent.run_stream("Tell me a story about a robot discovering art."): if event.event == "RunContent" and event.content: print(event.content, end="", flush=True) elif event.event == "ToolCallStarted": print(f"\n> Calling {event.tool.tool_name}...") elif event.event == "RunCompleted": print(f"\n\nTokens: {event.metrics.total_tokens}")
2
Install dependencies
pip install definable
3
Export your API key
export OPENAI_API_KEY=sk-***
4
Run
python agent_streaming.py
Tokens appear in your terminal as they are generated.
import asynciofrom definable.agent import Agentagent = Agent(model="gpt-4o", instructions="You are a helpful assistant.")async def main(): async for event in agent.arun_stream("Explain quantum computing."): if event.event == "RunContent" and event.content: print(event.content, end="", flush=True)asyncio.run(main())