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

# Progress and streaming

> Report a running agent's steps and tool calls to clients, and stream the model's text.

A run reports its steps and tool calls as progress events, which follow the journal. To show the model's text while the provider generates it, stream the model calls.

## Progress events

`onProgress` receives the run's progress events:

```typescript {"CODE_LOAD::harness/src/steering.ts#progress"}  theme={null}
const feed = restate.sendClient(progressFeed, job);
const result = yield* auditor.run(request, {
  *onProgress(event) {
    feed.publish(event); // a one-way send: the run does not wait for subscribers
  },
});
```

| Event | Reports |
| - | - |
| `step` | The start of a step |
| `tool-start`, `tool-info`, `tool-progress`, `tool-end` | A tool call starting, updating its display information, reporting progress, and finishing |
| `steering`, `steering-rejected` | A steering message that the run accepted or rejected |
| `handoff` | A [handoff](https://docs.restate.dev/harness/sub-agents.md#sub-agents-and-handoffs) to another agent |
| `compaction` | A summarized or truncated request, or a summary that failed |
| `error-note` | An error or a guardrail rejection that the model received as a note instead of failing the run |

The run waits for `onProgress` before it continues, and on replay it delivers the same events again. Publish each event with a journaled one-way send, as in the example, so the run doesn't wait for subscribers and a replay doesn't publish an event twice.
The [progress feed lesson](https://github.com/restatedev/ai-examples/blob/main/restate-harness/examples/src/lessons/16_progress_feed.ts) implements the `ProgressFeed` object that clients subscribe to.

A `tool` reports partial progress with `context.progress(value)`. The context of an `asyncTool` has no `progress`:

```typescript {"CODE_LOAD::harness/src/steering.ts#tool_progress"}  theme={null}
const auditor = agent({
  model,
  instructions: "Count stock with countStock, then name the warehouse with the least stock.",
  tools: {
    countStock: tool({
      description: "Count stock in the given warehouses.",
      input: z.object({warehouses: z.array(z.string()).max(10)}),
      *execute({warehouses}, context) {
        const counts: Record<string, number> = {};
        for (const warehouse of warehouses) {
          counts[warehouse] = warehouse.length * 7;
          yield* context.progress(`counted ${warehouse}`);
        }
        return counts;
      },
    }),
  },
});
```

Sub-agents and forks report their events as progress of the parent's tool call.

## Streaming

To show the model's text while the provider generates it, pass `onChunk` to `aiModel`. `aiModel` then streams each model call instead of waiting for the complete response:

```typescript {"CODE_LOAD::harness/src/steering.ts#stream"}  theme={null}
import {openai} from "@ai-sdk/openai";
import {aiModel} from "@restatedev/harness/provider";

const streamingModel = aiModel({
  model: openai("gpt-5.6-luna"),
  async onChunk(event, {step}) {
    if (event.type === "start") await clearText(step); // a retried call starts over
    if (event.type === "text-delta") await publishText(step, event.text);
  },
});
```

Each attempt of a model call delivers a `start` event, then a `text-delta` or `reasoning-delta` event for each new piece of text, and a `finish` event when the stream ends with a complete response or a reply cut off at the output token limit.
The second argument contains the request's `step` and the call's abort `signal`.
Only `aiModel` supports `onChunk`.

### Durability

Restate doesn't journal chunks.
`aiModel` passes each chunk to `onChunk` as it arrives. When the stream ends, `aiModel` builds the complete response from it: the text, tool calls, reasoning, structured output, and token usage. Restate journals that response as one entry.
Until then, the chunks exist only in the memory of the service process.

| Situation | Result |
| - | - |
| The process crashes, or the provider fails with a retryable error, during the stream | The model call has no journaled response, so Restate runs it again from the beginning. `onChunk` receives a new `start` for the same step, followed by the text of the new attempt. |
| The process crashes after the response is journaled | On replay, Restate returns the journaled response without calling the provider. `onChunk` receives no events for that step. |
| A client disconnects during the stream and reconnects | Restate has no chunks to resend. The client receives only what your handler stores or sends, such as progress events or the run's result. |

### Chunk events

* When a `start` arrives for a step, discard the text of the step's earlier attempt.
* `finish` means that the provider stream ended. It doesn't confirm that the run accepted the response or that Restate journaled it. Steering can still discard the response after `finish` arrives. If the stream fails, is interrupted, or ends early, for example at the provider's content filter, or if the response doesn't match the `output` schema, no `finish` arrives.
* Chunks arrive before the run checks the response. Text that the run discards later has already reached `onChunk`, for example output that doesn't match the `output` schema, an answer that `afterRun` rejects, or a response that steering discards. If a [guardrail](https://docs.restate.dev/harness/guardrails.md) must check text before users see it, don't stream that text.
* Text and reasoning stream, but tool call arguments don't. With `output`, the `text-delta` events contain the raw JSON text.
* Every call through the model streams, including summaries: the summary of a stopped run, and compaction summaries that use the same model. These calls have no step, so `onChunk` receives them with `step` 0.

### Writing `onChunk`

* `onChunk` runs inside the journaled model call, not in the handler, so it can't use Restate operations such as state, service calls, or signals. Write chunks directly to a system outside Restate, such as a WebSocket or a pub/sub service.
* `aiModel` waits for each `onChunk` call before it reads the next chunk, so a slow callback slows down the model call.
* An error thrown from `onChunk` fails the attempt, and Restate [retries](https://docs.restate.dev/harness/models.md#retries) the model call. A `TerminalError` fails the run instead.
* `onChunk` belongs to the model, not to a run. Runs that share the model deliver their chunks to the same callback, and nothing in the second argument identifies the run.

For updates that a client must receive after a crash or a reconnect, use [progress events](#progress-events): `onProgress` runs in the handler, so it can store or send each event with a journaled operation. Progress events report steps and tool calls, not the model's text.
The [AG-UI lesson](https://github.com/restatedev/ai-examples/blob/main/restate-harness/examples/src/lessons/18_ag_ui.ts) stores AG-UI events, built from progress events, in object state, so a client that reconnects receives the conversation so far and then follows the run in progress.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.