Skip to content

Commit ca43ab8

Browse files
authored
docs(ai-chat): document stopping generation for custom agents (#3976)
## Summary Adds a "Stopping generation" section to the Custom agents page. It documents how stop works when you drop down from `chat.agent` to `chat.createSession`: pass `turn.signal` (a combined stop-and-cancel `AbortSignal`) to `streamText`, and `turn.complete()` cleans up the aborted partial, accumulates it as its own assistant message, and keeps the run alive for the next turn. `turn.stopped` distinguishes a user stop from a full run cancel. Until now the createSession stop story only existed as scattered fields in the reference table; the client side (`transport.stopGeneration`) and the `chat.agent` run-callback signals were documented, but not the custom-agent turn loop. Steering for these backends is already covered on the pending messages page, which this page links to.
1 parent 9feb765 commit ca43ab8

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

docs/ai-chat/custom-agents.mdx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,38 @@ for await (const turn of session) {
179179
}
180180
```
181181

182+
## Stopping generation
183+
184+
The frontend stops a turn with [`transport.stopGeneration(chatId)`](/ai-chat/frontend#stop-generation), which writes a stop signal to the session's input stream. It aborts the current turn's generation but keeps the run alive, so the next message continues on the same session.
185+
186+
`turn.signal` is a combined stop-and-cancel `AbortSignal`, fresh each turn. Pass it to `streamText` so the stop reaches the model, then let `turn.complete()` finish the turn:
187+
188+
```ts trigger/my-chat.ts
189+
for await (const turn of session) {
190+
const result = streamText({
191+
model: anthropic("claude-sonnet-4-5"),
192+
messages: turn.messages,
193+
abortSignal: turn.signal, // fires on a user stop OR a run cancel
194+
stopWhen: stepCountIs(15),
195+
});
196+
197+
await turn.complete(result);
198+
199+
if (turn.stopped) {
200+
// user stopped this turn — the partial response is already accumulated
201+
}
202+
}
203+
```
204+
205+
On a stop, `turn.complete()` cleans up the aborted parts of the partial response, accumulates it as its own assistant message, and writes turn-complete. The run does not end — the loop continues to the next turn.
206+
207+
Read `turn.stopped` to tell a user stop from a full run cancel:
208+
209+
- **User stop** (`transport.stopGeneration`): `turn.signal` aborts, `turn.stopped` is `true`, the partial response is accumulated, and the run stays alive for the next message.
210+
- **Run cancel** (cancelled, expired, or `maxDuration` exceeded): `turn.signal` aborts, `turn.stopped` is `false`, and `turn.complete()` returns without accumulating because the run is ending.
211+
212+
A hand-rolled loop wires this itself with `chat.createStopSignal()` and `chat.cleanupAbortedParts()`. Two things `createSession` handles for you are easy to get wrong there — see the [hand-rolled loop checklist](#hand-rolled-loop-checklist).
213+
182214
## Hand-rolled loop with primitives
183215

184216
For full control, skip `createSession` and compose the primitives directly:

0 commit comments

Comments
 (0)