Documentation Index
Fetch the complete documentation index at: https://docs.definable.ai/llms.txt
Use this file to discover all available pages before exploring further.
Stream agent responses to display output as it is generated.
Create the agent
from definable.agent import Agent
agent = Agent(
model="gpt-4o",
instructions="You are a storyteller. Write vivid, engaging stories.",
)
# Sync streaming
for 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}")
Export your API key
export OPENAI_API_KEY=sk-***
Run
python agent_streaming.py
Tokens appear in your terminal as they are generated.
Async Streaming
import asyncio
from definable.agent import Agent
agent = 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())