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

# Streaming

> Observe an agent's work in real time

<Info>
  Source: [`03_streaming.py`](https://github.com/opper-ai/opper-sdks/blob/main/python/examples/agents/03_streaming.py) · [`03-streaming.ts`](https://github.com/opper-ai/opper-sdks/blob/main/typescript/examples/agents/03-streaming.ts)
</Info>

Use `agent.stream(input)` to receive lifecycle events as the agent runs. After iterating, call `.result()` for the final `RunResult`.

## Iterate events

<CodeGroup>
  ```python Python theme={null}
  import sys
  from opperai.agent import Agent, tool

  agent = Agent(name="weather", instructions="...", tools=[get_weather])

  stream = agent.stream("What's the weather in Paris and Tokyo?")

  async for event in stream:
      match event.type:
          case "iteration_start":
              print(f"\n[Iteration {event.iteration}]")
          case "text_delta":
              sys.stdout.write(event.text)
              sys.stdout.flush()
          case "tool_start":
              print(f"-> {event.name}({event.input})")
          case "tool_end":
              print(f"<- {event.name} -> {event.output} ({event.duration_ms:.0f}ms)")

  result = await stream.result()
  print("\nFinal:", result.output)
  ```

  ```typescript TypeScript theme={null}
  import { Agent } from "opperai";

  const agent = new Agent({ name: "weather", instructions: "...", tools: [getWeather] });

  const stream = agent.stream("What's the weather in Paris and Tokyo?");

  for await (const event of stream) {
    switch (event.type) {
      case "iteration_start":
        console.log(`\n[Iteration ${event.iteration}]`);
        break;
      case "text_delta":
        process.stdout.write(event.text);
        break;
      case "tool_start":
        console.log(`-> ${event.name}(${JSON.stringify(event.input)})`);
        break;
      case "tool_end":
        console.log(`<- ${event.name} -> ${JSON.stringify(event.output)} (${event.durationMs}ms)`);
        break;
    }
  }

  const result = await stream.result();
  console.log("\nFinal:", result.output);
  ```
</CodeGroup>

## Event types

| Event                     | Fired when                                            |
| ------------------------- | ----------------------------------------------------- |
| `iteration_start`         | A new think→act cycle begins.                         |
| `text_delta`              | The model emits a chunk of final text.                |
| `tool_start` / `tool_end` | A tool is about to run / has completed.               |
| `result`                  | The final `RunResult` (also returned by `.result()`). |

The full set is exported as `AgentStreamEvent` in TypeScript and lives in `opperai.agent._stream` in Python.
