Skip to main content
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:
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 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:
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:
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.

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