# Streams

A `Stream[Commentary]` capture streams what an agent says while it works,
before its final answer:

```kedi
[answer: Stream[Commentary]] << Inspect the parser and report what breaks.
> show: `answer`
= <answer>
```

While the agent reads files and runs tools, `> show:` prints each message it
says before a tool call. The program then returns the final answer.

## A Stream Is a Capture

A stream is a capture like any other: a promise of the call's value. Its value
is the final answer, a `str`, exactly what `[answer] << ...` returns.
`<answer>`, Python (`` `answer` `` in an expression) and every other ordinary
read wait for that value:

```kedi
[answer: Stream[Commentary]] << Inspect the parser and report what breaks.
[summary] << Summarize <answer> in one sentence.
= <summary>
```

## Reading the Chunks

Two readers take the chunks instead, as they arrive:

- ``> show: `answer` `` prints each chunk and continues when the call ends. It
  shows the commentary only; read the value with `<answer>`.
- ``> loop [piece]: `answer`: `` runs once per chunk, in arrival order.

```kedi
[answer: Stream[Commentary]] << Inspect the parser and report what breaks.
[notes: list[str]] = `[]`
> loop [piece]: `answer`:
  `notes.append(piece.text)`
= <answer> (after <`len(notes)`> notes)
```

`piece` is a `Commentary`. `<piece>`, `str(piece)` and `piece.text` give its
text.

Each chunk is a completed message the agent said before a tool call: the same
commentary that [Stream Events](../agentic-engineering/stream-events.md)
publish. Provider deltas and hidden reasoning never stream, a subagent's
commentary is its own, and the final answer comes only as the value.

Every reader sees every chunk from the first. A stream can be shown and looped
over any number of times, also after its call has ended.

## Accounting and Errors

The call is an ordinary text call:

- history records its value;
- `> budget:` counts its model requests as for any call, and its usage is reported
  when it ends;
- hooks see the value.

Once the first chunk has arrived, Kedi never restarts the call, since readers
have already seen that chunk. Recovery inside the agent's run still applies: an
adapter's request retries and fallback models retry only the failing model
request, so no chunk repeats. A failure the run does not recover from ends the
stream. Its error is raised when a `> loop` over the stream has read the chunks
before it, and when the value is read.

## Adapters

A stream needs an adapter that publishes its commentary, the `stream_events`
capability:

- Pydantic AI, LangChain, the Claude Agent SDK, Codex, ACP and A2A;
- JSON-RPC bridges that negotiate `events`.

Any other adapter fails before the call, and the editor reports it.

Kedi runs independent calls concurrently, so `> show:` prints the chunks while
the call runs. With `KEDI_PARALLEL=0`, calls run one at a time, and a stream's
chunks are read after its call has ended.

An input in the Kedi REPL waits for its calls before it ends, so a stream shown
in a later input replays its chunks. The REPL shows every call's commentary
while it runs anyway: see [Progress](../tooling/repl.md#progress). Kedi
Notebook shows a cell's output when the cell finishes.

## Limits

`Stream[Commentary]` is, for now, the only stream, and only a raw capture
streams. A template field, a procedure return or a nested type
(`list[Stream[Commentary]]`) cannot stream, and Kedi and the editor refuse
them.
