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:
Streaming
To show the model’s text while the provider generates it, passonChunk to aiModel. aiModel then streams each model call instead of waiting for the complete response:
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
startarrives for a step, discard the text of the step’s earlier attempt. finishmeans 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 afterfinisharrives. If the stream fails, is interrupted, or ends early, for example at the provider’s content filter, or if the response doesn’t match theoutputschema, nofinisharrives.- 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 theoutputschema, an answer thatafterRunrejects, 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, thetext-deltaevents 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
onChunkreceives them withstep0.
Writing onChunk
onChunkruns 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.aiModelwaits for eachonChunkcall before it reads the next chunk, so a slow callback slows down the model call.- An error thrown from
onChunkfails the attempt, and Restate retries the model call. ATerminalErrorfails the run instead. onChunkbelongs 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.
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.