> ## Documentation Index
> Fetch the complete documentation index at: https://polos.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Agent Streaming

> Stream agent responses in real-time

Stream agent responses as they're generated.

## Stream text chunks

<CodeGroup>
  ```python Python theme={null}
  result = await storyteller.stream(polos, "Tell me a story about a robot")

  # Iterate over text chunks as they arrive
  async for chunk in result.text_chunks:
      print(chunk, end="", flush=True)
  ```

  ```typescript TypeScript theme={null}
  const result = await storyteller.stream(polos, {
    input: 'Tell me a story about a robot',
  });

  // Iterate over text chunks as they arrive
  for await (const chunk of result.textChunks) {
    process.stdout.write(chunk);
  }
  ```
</CodeGroup>

## Stream all events

<CodeGroup>
  ```python Python theme={null}
  result = await storyteller.stream(polos, "Write a haiku")

  async for event in result.events:
      if event.event_type == "text_delta":
          print(event.data.get("content", ""), end="", flush=True)
      elif event.event_type == "tool_call":
          tool_name = event.data.get("tool_call", {}).get("function", {}).get("name")
          print(f"\n[Tool: {tool_name}]")
      elif event.event_type == "agent_finish":
          print("\n[Done]")
  ```

  ```typescript TypeScript theme={null}
  const result = await storyteller.stream(polos, {
    input: 'Write a haiku',
  });

  for await (const event of result.events) {
    if (event.eventType === 'text_delta') {
      const content = event.data['content'];
      if (typeof content === 'string') {
        process.stdout.write(content);
      }
    } else if (event.eventType === 'tool_call') {
      const toolCall = event.data['tool_call'] as Record<string, unknown>;
      const fn = toolCall?.['function'] as Record<string, unknown>;
      console.log(`\n[Tool: ${fn?.['name']}]`);
    } else if (event.eventType === 'agent_finish') {
      console.log('\n[Done]');
    }
  }
  ```
</CodeGroup>

## Get final text

<CodeGroup>
  ```python Python theme={null}
  result = await storyteller.stream(polos, "What are benefits of reading?")

  # Wait for completion and get accumulated text
  final_text = await result.text()
  print(final_text)
  ```

  ```typescript TypeScript theme={null}
  const result = await storyteller.stream(polos, {
    input: 'What are benefits of reading?',
  });

  // Wait for completion and get accumulated text
  const finalText = await result.text();
  console.log(finalText);
  ```
</CodeGroup>

## Run it

<CodeGroup>
  ```bash Python theme={null}
  git clone https://github.com/polos-dev/polos.git
  cd polos/python-examples/03-agent-streaming
  cp .env.example .env  # Add your POLOS_PROJECT_ID and API key
  uv sync
  python main.py
  ```

  ```bash TypeScript theme={null}
  git clone https://github.com/polos-dev/polos.git
  cd polos/typescript-examples/03-agent-streaming
  cp .env.example .env  # Add your POLOS_PROJECT_ID and API key
  npm install
  npx tsx main.ts
  ```
</CodeGroup>

Open [http://localhost:5173](http://localhost:5173) to view your agents and workflows, run them from the UI, and see execution traces.

[Python example on GitHub](https://github.com/polos-dev/polos/tree/main/python-examples/03-agent-streaming) | [TypeScript example on GitHub](https://github.com/polos-dev/polos/tree/main/typescript-examples/03-agent-streaming)
