> ## 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.

# Agent with Streaming

> Stream agent responses token by token in real time.

Stream agent responses to display output as it is generated.

<Steps>
  <Step title="Create the agent">
    ```python agent_streaming.py theme={null}
    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}")
    ```
  </Step>

  <Step title="Install dependencies">
    <Snippet file="install-definable.mdx" />
  </Step>

  <Step title="Export your API key">
    <Snippet file="export-openai-key.mdx" />
  </Step>

  <Step title="Run">
    ```bash theme={null}
    python agent_streaming.py
    ```

    Tokens appear in your terminal as they are generated.
  </Step>
</Steps>

## Async Streaming

```python theme={null}
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())
```
