Skip to content

Commit 7376734

Browse files
committed
docs(ai-chat): document chat.endRun()
Add an "Ending a run on your terms" section to backend.mdx covering chat.endRun() — exit after the current turn without the upgrade-required signal, for one-shot or self-terminating agents.
1 parent a809b97 commit 7376734

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

docs/ai-chat/backend.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,25 @@ The `reason` field tells you why messages are being prepared:
967967

968968
Chat agent runs are pinned to the worker version they started on. When you deploy a new version, suspended runs resume on the old code. Call `chat.requestUpgrade()` in `onTurnStart` to skip `run()` and exit immediately — the transport re-triggers the same message on the latest version. See the [Version Upgrades pattern](/ai-chat/patterns/version-upgrades) for the full guide.
969969

970+
### Ending a run on your terms
971+
972+
By default, a chat agent stays idle after each turn waiting for the next user message. Call `chat.endRun()` from `run()`, `chat.defer()`, `onBeforeTurnComplete`, or `onTurnComplete` to exit the loop once the current turn finishes — no upgrade signal, no idle wait.
973+
974+
```ts
975+
chat.agent({
976+
id: "one-shot",
977+
run: async ({ messages, signal }) => {
978+
// Single-response agent — exit after this turn.
979+
chat.endRun();
980+
return streamText({ model: openai("gpt-4o"), messages, abortSignal: signal });
981+
},
982+
});
983+
```
984+
985+
The current turn streams through normally, `onBeforeTurnComplete` / `onTurnComplete` fire, the turn-complete chunk is written, and the run exits instead of suspending. The next user message on the same `chatId` starts a fresh run via the standard continuation flow.
986+
987+
Use this when the agent knows its work is done (budget exhausted, goal achieved, one-shot response) rather than relying on the idle timeout. Unlike `chat.requestUpgrade()`, no `upgrade-required` signal is sent to the client, so there's no version-migration semantics.
988+
970989
### Runtime configuration
971990

972991
#### chat.setTurnTimeout()

0 commit comments

Comments
 (0)