Use the agent and context created in the previous step:
const stream = agent.stream(
{
sessionId: "reference-session-001",
message: "Explain the highest-risk parts of this project.",
},
context,
);
for await (const event of stream) {
console.log(event.type, event.payload);
}
const terminal = await stream.result;
console.log(terminal.type);Iteration reads live events for this request. stream.result resolves to the
terminal event for that same run. Live progress does not replace the terminal
result.
Cancel when the caller leaves
Pass an AbortSignal when the stream belongs to a request that may disconnect:
const stream = agent.stream(
{
sessionId: "reference-session-001",
message: "Review the deployment configuration.",
signal: abortController.signal,
},
context,
);Aborting cancels that upstream run. The terminal promise resolves with
run.cancelled, which should remain visible as a deliberate outcome rather
than being converted into a generic network error.
Use a durable background subscription when observation must continue after the original request ends.