@trigger.dev/sdk 4.5.0-rc.6 → 4.5.0-rc.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/dist/commonjs/v3/ai.d.ts +171 -5
  2. package/dist/commonjs/v3/ai.js +309 -22
  3. package/dist/commonjs/v3/ai.js.map +1 -1
  4. package/dist/commonjs/v3/chat-server.d.ts +8 -0
  5. package/dist/commonjs/v3/chat-server.js +32 -10
  6. package/dist/commonjs/v3/chat-server.js.map +1 -1
  7. package/dist/commonjs/v3/chat-server.test.js +51 -0
  8. package/dist/commonjs/v3/chat-server.test.js.map +1 -1
  9. package/dist/commonjs/v3/createStartSessionAction.test.js +30 -0
  10. package/dist/commonjs/v3/createStartSessionAction.test.js.map +1 -1
  11. package/dist/commonjs/v3/sessions.d.ts +3 -2
  12. package/dist/commonjs/v3/sessions.js +3 -2
  13. package/dist/commonjs/v3/sessions.js.map +1 -1
  14. package/dist/commonjs/version.js +1 -1
  15. package/dist/esm/v3/ai.d.ts +171 -5
  16. package/dist/esm/v3/ai.js +309 -22
  17. package/dist/esm/v3/ai.js.map +1 -1
  18. package/dist/esm/v3/chat-server.d.ts +8 -0
  19. package/dist/esm/v3/chat-server.js +32 -10
  20. package/dist/esm/v3/chat-server.js.map +1 -1
  21. package/dist/esm/v3/chat-server.test.js +51 -0
  22. package/dist/esm/v3/chat-server.test.js.map +1 -1
  23. package/dist/esm/v3/createStartSessionAction.test.js +30 -0
  24. package/dist/esm/v3/createStartSessionAction.test.js.map +1 -1
  25. package/dist/esm/v3/sessions.d.ts +3 -2
  26. package/dist/esm/v3/sessions.js +3 -2
  27. package/dist/esm/v3/sessions.js.map +1 -1
  28. package/dist/esm/version.js +1 -1
  29. package/docs/ai/prompts.mdx +430 -0
  30. package/docs/ai-chat/actions.mdx +115 -0
  31. package/docs/ai-chat/anatomy.mdx +71 -0
  32. package/docs/ai-chat/backend.mdx +817 -0
  33. package/docs/ai-chat/background-injection.mdx +221 -0
  34. package/docs/ai-chat/changelog.mdx +850 -0
  35. package/docs/ai-chat/chat-local.mdx +174 -0
  36. package/docs/ai-chat/client-protocol.mdx +1081 -0
  37. package/docs/ai-chat/compaction.mdx +411 -0
  38. package/docs/ai-chat/custom-agents.mdx +364 -0
  39. package/docs/ai-chat/error-handling.mdx +415 -0
  40. package/docs/ai-chat/fast-starts.mdx +672 -0
  41. package/docs/ai-chat/frontend.mdx +580 -0
  42. package/docs/ai-chat/how-it-works.mdx +230 -0
  43. package/docs/ai-chat/lifecycle-hooks.mdx +530 -0
  44. package/docs/ai-chat/mcp.mdx +101 -0
  45. package/docs/ai-chat/overview.mdx +90 -0
  46. package/docs/ai-chat/patterns/branching-conversations.mdx +284 -0
  47. package/docs/ai-chat/patterns/code-sandbox.mdx +126 -0
  48. package/docs/ai-chat/patterns/database-persistence.mdx +414 -0
  49. package/docs/ai-chat/patterns/human-in-the-loop.mdx +275 -0
  50. package/docs/ai-chat/patterns/large-payloads.mdx +169 -0
  51. package/docs/ai-chat/patterns/oom-resilience.mdx +120 -0
  52. package/docs/ai-chat/patterns/persistence-and-replay.mdx +211 -0
  53. package/docs/ai-chat/patterns/recovery-boot.mdx +230 -0
  54. package/docs/ai-chat/patterns/skills.mdx +221 -0
  55. package/docs/ai-chat/patterns/sub-agents.mdx +383 -0
  56. package/docs/ai-chat/patterns/tool-result-auditing.mdx +148 -0
  57. package/docs/ai-chat/patterns/trusted-edge-signals.mdx +337 -0
  58. package/docs/ai-chat/patterns/version-upgrades.mdx +172 -0
  59. package/docs/ai-chat/pending-messages.mdx +343 -0
  60. package/docs/ai-chat/prompt-caching.mdx +206 -0
  61. package/docs/ai-chat/quick-start.mdx +161 -0
  62. package/docs/ai-chat/reference.mdx +909 -0
  63. package/docs/ai-chat/server-chat.mdx +263 -0
  64. package/docs/ai-chat/sessions.mdx +333 -0
  65. package/docs/ai-chat/testing.mdx +682 -0
  66. package/docs/ai-chat/tools.mdx +191 -0
  67. package/docs/ai-chat/types.mdx +242 -0
  68. package/docs/ai-chat/upgrade-guide.mdx +515 -0
  69. package/docs/apikeys.mdx +54 -0
  70. package/docs/building-with-ai.mdx +261 -0
  71. package/docs/bulk-actions.mdx +49 -0
  72. package/docs/changelog.mdx +6 -0
  73. package/docs/cli-deploy-commands.mdx +9 -0
  74. package/docs/cli-dev-commands.mdx +9 -0
  75. package/docs/cli-dev.mdx +8 -0
  76. package/docs/cli-init-commands.mdx +58 -0
  77. package/docs/cli-introduction.mdx +25 -0
  78. package/docs/cli-list-profiles-commands.mdx +42 -0
  79. package/docs/cli-login-commands.mdx +33 -0
  80. package/docs/cli-logout-commands.mdx +33 -0
  81. package/docs/cli-preview-archive.mdx +59 -0
  82. package/docs/cli-promote-commands.mdx +9 -0
  83. package/docs/cli-switch.mdx +43 -0
  84. package/docs/cli-update-commands.mdx +42 -0
  85. package/docs/cli-whoami-commands.mdx +33 -0
  86. package/docs/community.mdx +6 -0
  87. package/docs/config/config-file.mdx +602 -0
  88. package/docs/config/extensions/additionalFiles.mdx +38 -0
  89. package/docs/config/extensions/additionalPackages.mdx +40 -0
  90. package/docs/config/extensions/aptGet.mdx +34 -0
  91. package/docs/config/extensions/audioWaveform.mdx +20 -0
  92. package/docs/config/extensions/custom.mdx +380 -0
  93. package/docs/config/extensions/emitDecoratorMetadata.mdx +29 -0
  94. package/docs/config/extensions/esbuildPlugin.mdx +31 -0
  95. package/docs/config/extensions/ffmpeg.mdx +45 -0
  96. package/docs/config/extensions/lightpanda.mdx +56 -0
  97. package/docs/config/extensions/overview.mdx +67 -0
  98. package/docs/config/extensions/playwright.mdx +195 -0
  99. package/docs/config/extensions/prismaExtension.mdx +1014 -0
  100. package/docs/config/extensions/puppeteer.mdx +30 -0
  101. package/docs/config/extensions/pythonExtension.mdx +182 -0
  102. package/docs/config/extensions/syncEnvVars.mdx +291 -0
  103. package/docs/context.mdx +235 -0
  104. package/docs/database-connections.mdx +213 -0
  105. package/docs/deploy-environment-variables.mdx +435 -0
  106. package/docs/deployment/atomic-deployment.mdx +172 -0
  107. package/docs/deployment/overview.mdx +257 -0
  108. package/docs/deployment/preview-branches.mdx +224 -0
  109. package/docs/errors-retrying.mdx +379 -0
  110. package/docs/github-actions.mdx +222 -0
  111. package/docs/github-integration.mdx +136 -0
  112. package/docs/github-repo.mdx +8 -0
  113. package/docs/help-email.mdx +6 -0
  114. package/docs/help-slack.mdx +11 -0
  115. package/docs/hidden-tasks.mdx +56 -0
  116. package/docs/how-it-works.mdx +454 -0
  117. package/docs/how-to-reduce-your-spend.mdx +217 -0
  118. package/docs/idempotency.mdx +504 -0
  119. package/docs/introduction.mdx +223 -0
  120. package/docs/limits.mdx +241 -0
  121. package/docs/logging.mdx +195 -0
  122. package/docs/machines.mdx +952 -0
  123. package/docs/manual-setup.mdx +632 -0
  124. package/docs/mcp-agent-rules.mdx +41 -0
  125. package/docs/mcp-introduction.mdx +385 -0
  126. package/docs/mcp-tools.mdx +273 -0
  127. package/docs/migrating-from-v3.mdx +334 -0
  128. package/docs/observability/dashboards.mdx +102 -0
  129. package/docs/observability/query.mdx +585 -0
  130. package/docs/open-source-contributing.mdx +16 -0
  131. package/docs/open-source-self-hosting.mdx +541 -0
  132. package/docs/private-networking/aws-console-setup.mdx +304 -0
  133. package/docs/private-networking/overview.mdx +144 -0
  134. package/docs/private-networking/troubleshooting.mdx +78 -0
  135. package/docs/queue-concurrency.mdx +354 -0
  136. package/docs/quick-start.mdx +97 -0
  137. package/docs/realtime/auth.mdx +208 -0
  138. package/docs/realtime/backend/overview.mdx +45 -0
  139. package/docs/realtime/backend/streams.mdx +418 -0
  140. package/docs/realtime/backend/subscribe.mdx +225 -0
  141. package/docs/realtime/how-it-works.mdx +94 -0
  142. package/docs/realtime/overview.mdx +63 -0
  143. package/docs/realtime/react-hooks/overview.mdx +73 -0
  144. package/docs/realtime/react-hooks/streams.mdx +449 -0
  145. package/docs/realtime/react-hooks/subscribe.mdx +674 -0
  146. package/docs/realtime/react-hooks/swr.mdx +87 -0
  147. package/docs/realtime/react-hooks/triggering.mdx +194 -0
  148. package/docs/realtime/react-hooks/use-wait-token.mdx +34 -0
  149. package/docs/realtime/run-object.mdx +174 -0
  150. package/docs/replaying.mdx +72 -0
  151. package/docs/request-feature.mdx +6 -0
  152. package/docs/roadmap.mdx +6 -0
  153. package/docs/run-tests.mdx +20 -0
  154. package/docs/run-usage.mdx +113 -0
  155. package/docs/runs/heartbeats.mdx +38 -0
  156. package/docs/runs/max-duration.mdx +139 -0
  157. package/docs/runs/metadata.mdx +734 -0
  158. package/docs/runs/priority.mdx +31 -0
  159. package/docs/runs.mdx +396 -0
  160. package/docs/self-hosting/docker.mdx +458 -0
  161. package/docs/self-hosting/env/supervisor.mdx +74 -0
  162. package/docs/self-hosting/env/webapp.mdx +276 -0
  163. package/docs/self-hosting/kubernetes.mdx +601 -0
  164. package/docs/self-hosting/overview.mdx +108 -0
  165. package/docs/skills.mdx +85 -0
  166. package/docs/tags.mdx +120 -0
  167. package/docs/tasks/overview.mdx +697 -0
  168. package/docs/tasks/scheduled.mdx +382 -0
  169. package/docs/tasks/schemaTask.mdx +413 -0
  170. package/docs/tasks/streams.mdx +884 -0
  171. package/docs/triggering.mdx +1320 -0
  172. package/docs/troubleshooting-alerts.mdx +385 -0
  173. package/docs/troubleshooting-debugging-in-vscode.mdx +8 -0
  174. package/docs/troubleshooting-github-issues.mdx +6 -0
  175. package/docs/troubleshooting-uptime-status.mdx +6 -0
  176. package/docs/troubleshooting.mdx +398 -0
  177. package/docs/upgrading-packages.mdx +80 -0
  178. package/docs/vercel-integration.mdx +207 -0
  179. package/docs/versioning.mdx +56 -0
  180. package/docs/video-walkthrough.mdx +23 -0
  181. package/docs/wait-for-token.mdx +540 -0
  182. package/docs/wait-for.mdx +42 -0
  183. package/docs/wait-until.mdx +53 -0
  184. package/docs/wait.mdx +18 -0
  185. package/docs/writing-tasks-introduction.mdx +33 -0
  186. package/package.json +8 -5
  187. package/skills/trigger-authoring-chat-agent/SKILL.md +296 -0
  188. package/skills/trigger-authoring-tasks/SKILL.md +254 -0
  189. package/skills/trigger-chat-agent-advanced/SKILL.md +368 -0
  190. package/skills/trigger-cost-savings/SKILL.md +116 -0
  191. package/skills/trigger-realtime-and-frontend/SKILL.md +276 -0
@@ -0,0 +1,817 @@
1
+ ---
2
+ title: "Backend"
3
+ sidebarTitle: "Backend"
4
+ description: "Three approaches to building your chat backend — chat.agent(), session iterator, or raw task primitives."
5
+ ---
6
+
7
+ import RcBanner from "/snippets/ai-chat-rc-banner.mdx";
8
+
9
+ <RcBanner />
10
+
11
+ There are three abstraction levels for a chat backend. All three speak the same wire protocol, so the [frontend transport](/ai-chat/frontend) works unchanged whichever you pick.
12
+
13
+ | Capability | `chat.agent()` | `chat.createSession()` | Raw primitives |
14
+ | ------------------------------------- | -------------- | ------------------------------------------------------------- | -------------- |
15
+ | Turn loop, stop signals, accumulation | Managed | Managed | You write it |
16
+ | Lifecycle hooks | Yes | No — inline code per turn | No |
17
+ | Continuation recovery on new runs | Automatic | [Manual seeding](/ai-chat/custom-agents#continuation-runs-and-history-seeding) | Manual seeding |
18
+ | Compaction / steering | Built-in | Built-in | Manual |
19
+ | Head Start, actions, tool approvals | Yes | No | No |
20
+ | Custom stream conversion | No | Limited | Full control |
21
+ | Agent dashboard visibility | Yes | Yes (via `customAgent`) | Yes |
22
+
23
+ The raw-primitives column assumes [`chat.customAgent()`](/ai-chat/custom-agents) as the wrapper, which is what makes the task visible to the agent dashboard.
24
+
25
+ Start with `chat.agent()`. Drop to `chat.createSession()` when you want to own the per-turn code (model routing, persistence, custom telemetry) without rebuilding the turn loop. Drop to raw primitives only when you need full control over stream conversion or a custom protocol.
26
+
27
+ ## chat.agent()
28
+
29
+ The highest-level approach. Handles message accumulation, stop signals, turn lifecycle, and auto-piping automatically.
30
+
31
+ ### Simple: return a StreamTextResult
32
+
33
+ Return the `streamText` result from `run` and it's automatically piped to the frontend:
34
+
35
+ ```ts
36
+ import { chat } from "@trigger.dev/sdk/ai";
37
+ import { streamText, stepCountIs } from "ai";
38
+ import { anthropic } from "@ai-sdk/anthropic";
39
+
40
+ export const simpleChat = chat.agent({
41
+ id: "simple-chat",
42
+ run: async ({ messages, signal }) => {
43
+ return streamText({
44
+ ...chat.toStreamTextOptions(), // prepareStep, system, telemetry (see note below)
45
+ model: anthropic("claude-sonnet-4-5"),
46
+ system: "You are a helpful assistant.",
47
+ messages,
48
+ abortSignal: signal,
49
+ stopWhen: stepCountIs(15),
50
+ });
51
+ },
52
+ });
53
+ ```
54
+
55
+ <Warning>
56
+ **Always spread `chat.toStreamTextOptions()` first** (as above) so your explicit overrides win. It wires up the `prepareStep` callback behind [compaction](/ai-chat/compaction), [steering](/ai-chat/pending-messages), and [background injection](/ai-chat/background-injection), all of which silently no-op without it, and injects the system prompt from `chat.prompt()`, the resolved model (when you pass a `registry`), and telemetry metadata. Examples below keep the spread implicit for brevity, so include it in real code.
57
+ </Warning>
58
+
59
+ ### Using chat.pipe() for complex flows
60
+
61
+ For complex agent flows where `streamText` is called deep inside your code, use `chat.pipe()`. It works from **anywhere inside a task** — even nested function calls.
62
+
63
+ ```ts trigger/agent-chat.ts
64
+ import { chat } from "@trigger.dev/sdk/ai";
65
+ import { streamText } from "ai";
66
+ import { anthropic } from "@ai-sdk/anthropic";
67
+ import type { ModelMessage } from "ai";
68
+
69
+ export const agentChat = chat.agent({
70
+ id: "agent-chat",
71
+ run: async ({ messages }) => {
72
+ // Don't return anything — chat.pipe is called inside
73
+ await runAgentLoop(messages);
74
+ },
75
+ });
76
+
77
+ async function runAgentLoop(messages: ModelMessage[]) {
78
+ // ... agent logic, tool calls, etc.
79
+
80
+ const result = streamText({
81
+ model: anthropic("claude-sonnet-4-5"),
82
+ messages,
83
+ stopWhen: stepCountIs(15),
84
+ });
85
+
86
+ // Pipe from anywhere — no need to return it
87
+ await chat.pipe(result);
88
+ }
89
+ ```
90
+
91
+ ### Custom data parts
92
+
93
+ Add custom `data-*` parts to the assistant's response message via `chat.response.write()` (from `run()`) or the `writer` parameter in lifecycle hooks. Non-transient `data-*` chunks are automatically added to `responseMessage.parts` and surface in `onTurnComplete` for persistence:
94
+
95
+ ```ts
96
+ export const myChat = chat.agent({
97
+ id: "my-chat",
98
+ onBeforeTurnComplete: async ({ writer, turn }) => {
99
+ // This data part will be in responseMessage.parts in onTurnComplete
100
+ writer.write({
101
+ type: "data-metadata",
102
+ data: { turn, model: "gpt-4o", timestamp: Date.now() },
103
+ });
104
+ },
105
+ onTurnComplete: async ({ responseMessage }) => {
106
+ // responseMessage.parts includes the data-metadata part
107
+ await db.messages.save(responseMessage);
108
+ },
109
+ run: async ({ messages, signal }) => {
110
+ // Also works from run() via chat.response
111
+ chat.response.write({
112
+ type: "data-context",
113
+ data: { searchResults: results },
114
+ });
115
+
116
+ return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
117
+ },
118
+ });
119
+ ```
120
+
121
+ Add `transient: true` to data chunks that should stream to the frontend but NOT persist in the response message. Use this for progress indicators, loading states, and other temporary UI:
122
+
123
+ ```ts
124
+ // Transient — frontend sees it, but NOT in onTurnComplete's responseMessage
125
+ writer.write({
126
+ type: "data-progress",
127
+ id: "search",
128
+ data: { percent: 50 },
129
+ transient: true,
130
+ });
131
+ ```
132
+
133
+ <Info>
134
+ This matches the AI SDK's semantics: `data-*` chunks persist to `message.parts` by default. Only `transient: true` chunks are ephemeral. Non-data chunks (`text-delta`, `tool-*`, etc.) are handled by `streamText` and captured via `onFinish` — they don't need `chat.response`.
135
+ </Info>
136
+
137
+ <Note>
138
+ `chat.response` and the `writer` accumulation behavior work with `chat.agent` and `chat.createSession`. If you're using [`chat.customAgent`](/ai-chat/custom-agents), you own the accumulator — see the raw-task example for the manual pattern.
139
+ </Note>
140
+
141
+ ### Raw streaming with `chat.stream`
142
+
143
+ For low-level stream access (piping from subtasks, reading streams by run ID), use `chat.stream`. Chunks written via `chat.stream` go directly to the realtime output — they are **NOT** accumulated into the response message regardless of the `transient` flag.
144
+
145
+ ```ts
146
+ // Raw stream — always ephemeral, never in responseMessage
147
+ const { waitUntilComplete } = chat.stream.writer({
148
+ execute: ({ write }) => {
149
+ write({ type: "data-status", data: { message: "Processing..." } });
150
+ },
151
+ });
152
+ await waitUntilComplete();
153
+ ```
154
+
155
+ <Tip>
156
+ Use `data-*` chunk types (e.g. `data-status`, `data-progress`) for custom data. The AI SDK processes these into `DataUIPart` objects in `message.parts` on the frontend. Writing the same `type` + `id` again updates the existing part instead of creating a new one — useful for live progress.
157
+ </Tip>
158
+
159
+ `chat.stream` exposes the full stream API:
160
+
161
+ | Method | Description |
162
+ |--------|-------------|
163
+ | `chat.stream.writer(options)` | Write individual chunks via a callback |
164
+ | `chat.stream.pipe(stream, options?)` | Pipe a `ReadableStream` or `AsyncIterable` |
165
+ | `chat.stream.append(value, options?)` | Append raw data |
166
+ | `chat.stream.read(runId, options?)` | Read the stream by run ID |
167
+
168
+ For piping streams from subtasks to the parent chat (via `target: "root"`), see the [Sub-agents pattern](/ai-chat/patterns/sub-agents).
169
+
170
+ ### Backed by a Session
171
+
172
+ Every `chat.agent` conversation is backed by a durable [Session](/ai-chat/sessions): `externalId` is your `chatId`, `type` is `"chat.agent"`, and `taskIdentifier` is the agent's task ID. The session is the run manager. It owns the chat's runs, persists across run lifecycles, and orchestrates handoffs (idle continuation, `chat.requestUpgrade`). You rarely touch it directly, since `chat.stream`, `chat.messages`, and `chat.stopSignal` wrap everything, but `payload.sessionId` is there when you need to reach in, e.g. `sessions.open(payload.sessionId)` to write from a sub-agent or from outside the turn loop.
173
+
174
+ ### Tools
175
+
176
+ Declare your tools on the agent config, then read them back (typed) from the `run()` payload. Declaring them on the config, not just on `streamText`, is what lets the SDK re-apply each tool's `toModelOutput` when it re-converts history on later turns.
177
+
178
+ ```ts
179
+ const tools = { searchDocs };
180
+
181
+ export const myChat = chat.agent({
182
+ id: "my-chat",
183
+ tools,
184
+ run: async ({ messages, tools, signal }) =>
185
+ streamText({
186
+ ...chat.toStreamTextOptions({ tools }),
187
+ model: anthropic("claude-sonnet-4-5"),
188
+ messages,
189
+ abortSignal: signal,
190
+ stopWhen: stepCountIs(15),
191
+ }),
192
+ });
193
+ ```
194
+
195
+ See [Tools](/ai-chat/tools) for `toModelOutput` across turns, per-turn dynamic tools, the typed run payload, and how config tools relate to skills.
196
+
197
+ ### Lifecycle hooks
198
+
199
+ `chat.agent({ ... })` accepts hooks that fire in a fixed order around each turn, plus dedicated suspend/resume hooks. The full reference lives on its own page:
200
+
201
+ - [Lifecycle hooks](/ai-chat/lifecycle-hooks) — `onPreload`, `onChatStart`, `onValidateMessages`, `hydrateMessages`, `onTurnStart`, `onBeforeTurnComplete`, `onTurnComplete`, `onChatSuspend` / `onChatResume`, `exitAfterPreloadIdle`, plus how `ctx` plumbs through every callback.
202
+
203
+ **Per-turn order:** `onValidateMessages` → `hydrateMessages` → `onChatStart` (chat's first message only) → `onTurnStart` → `run()` → `onBeforeTurnComplete` → `onTurnComplete`.
204
+
205
+ ### Using prompts
206
+
207
+ Use [AI Prompts](/ai/prompts) to manage your system prompt as versioned, overridable config. Store the resolved prompt in a lifecycle hook with `chat.prompt.set()`, then spread `chat.toStreamTextOptions()` into `streamText` — it includes the system prompt, model, config, and telemetry automatically.
208
+
209
+ ```ts
210
+ import { chat } from "@trigger.dev/sdk/ai";
211
+ import { prompts } from "@trigger.dev/sdk";
212
+ import { streamText, createProviderRegistry } from "ai";
213
+ import { anthropic } from "@ai-sdk/anthropic";
214
+ import { z } from "zod";
215
+
216
+ const registry = createProviderRegistry({ anthropic });
217
+
218
+ const systemPrompt = prompts.define({
219
+ id: "my-chat-system",
220
+ model: "anthropic:claude-sonnet-4-5",
221
+ config: { temperature: 0.7 },
222
+ variables: z.object({ name: z.string() }),
223
+ content: `You are a helpful assistant for {{name}}.`,
224
+ });
225
+
226
+ export const myChat = chat.agent({
227
+ id: "my-chat",
228
+ clientDataSchema: z.object({ userId: z.string() }),
229
+ onChatStart: async ({ clientData }) => {
230
+ const user = await db.user.findUnique({ where: { id: clientData.userId } });
231
+ const resolved = await systemPrompt.resolve({ name: user.name });
232
+ chat.prompt.set(resolved);
233
+ },
234
+ run: async ({ messages, signal }) => {
235
+ return streamText({
236
+ ...chat.toStreamTextOptions({ registry }), // system, model, config, telemetry
237
+ messages,
238
+ abortSignal: signal,
239
+ stopWhen: stepCountIs(15),
240
+ });
241
+ },
242
+ });
243
+ ```
244
+
245
+ `chat.toStreamTextOptions()` returns an object with `system`, `model` (resolved via the registry), `temperature`, and `experimental_telemetry` — all from the stored prompt. Properties you set after the spread (like a client-selected model) take precedence.
246
+
247
+ **Which form to call:**
248
+
249
+ | Form | Use when |
250
+ |---|---|
251
+ | `chat.toStreamTextOptions()` | Default. Wires up `prepareStep` (compaction, steering, background injection), the stored prompt's `system` / `model` / `config`, and telemetry metadata. |
252
+ | `chat.toStreamTextOptions({ registry })` | You're using [Prompts](/ai/prompts) with a provider-prefixed model string (e.g. `"anthropic:claude-sonnet-4-5"`). The registry resolves the prefix to a real model instance via `createProviderRegistry({ anthropic, openai, ... })`. |
253
+ | `chat.toStreamTextOptions({ tools })` | You want HITL tool approvals — pass the same `tools` object you give to `streamText`. The SDK then knows which tool calls need to pause on `needsApproval: true`. |
254
+ | `chat.toStreamTextOptions({ registry, tools })` | Both of the above. |
255
+
256
+ <Tip>
257
+ See [Prompts](/ai/prompts) for the full guide — defining templates, variable schemas, dashboard
258
+ overrides, and the management SDK.
259
+ </Tip>
260
+
261
+ ### Stop generation
262
+
263
+ #### How stop works
264
+
265
+ Calling `stop()` from `useChat` sends a stop signal to the running task via input streams. The task's `streamText` call aborts (if you passed `signal` or `stopSignal`), but the **run stays alive** and waits for the next message. The partial response is captured and accumulated normally.
266
+
267
+ #### Abort signals
268
+
269
+ The `run` function receives three abort signals:
270
+
271
+ | Signal | Fires when | Use for |
272
+ | -------------- | ------------------------------------------- | ---------------------------------------------------------------------- |
273
+ | `signal` | Stop **or** cancel | Pass to `streamText` — handles both cases. **Use this in most cases.** |
274
+ | `stopSignal` | Stop only (per-turn, reset each turn) | Custom logic that should only run on user stop, not cancellation |
275
+ | `cancelSignal` | Run cancel, expire, or maxDuration exceeded | Cleanup that should only happen on full cancellation |
276
+
277
+ ```ts
278
+ export const myChat = chat.agent({
279
+ id: "my-chat",
280
+ run: async ({ messages, signal, stopSignal, cancelSignal }) => {
281
+ return streamText({
282
+ model: anthropic("claude-sonnet-4-5"),
283
+ messages,
284
+ abortSignal: signal, // Handles both stop and cancel
285
+ stopWhen: stepCountIs(15),
286
+ });
287
+ },
288
+ });
289
+ ```
290
+
291
+ <Tip>
292
+ Use `signal` (the combined signal) in most cases. The separate `stopSignal` and `cancelSignal` are
293
+ only needed if you want different behavior for stop vs cancel.
294
+ </Tip>
295
+
296
+ #### Detecting stop in callbacks
297
+
298
+ The `onTurnComplete` event includes a `stopped` boolean that indicates whether the user stopped generation during that turn:
299
+
300
+ ```ts
301
+ export const myChat = chat.agent({
302
+ id: "my-chat",
303
+ onTurnComplete: async ({ chatId, uiMessages, stopped }) => {
304
+ await db.chat.update({
305
+ where: { id: chatId },
306
+ data: { messages: uiMessages, lastStoppedAt: stopped ? new Date() : undefined },
307
+ });
308
+ },
309
+ run: async ({ messages, signal }) => {
310
+ return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
311
+ },
312
+ });
313
+ ```
314
+
315
+ You can also check stop status from **anywhere** during a turn using `chat.isStopped()`. This is useful inside `streamText`'s `onFinish` callback where the AI SDK's `isAborted` flag can be unreliable (e.g. when using `createUIMessageStream` + `writer.merge()`):
316
+
317
+ ```ts
318
+ import { chat } from "@trigger.dev/sdk/ai";
319
+ import { streamText } from "ai";
320
+
321
+ export const myChat = chat.agent({
322
+ id: "my-chat",
323
+ run: async ({ messages, signal }) => {
324
+ return streamText({
325
+ model: anthropic("claude-sonnet-4-5"),
326
+ messages,
327
+ abortSignal: signal,
328
+ onFinish: ({ isAborted }) => {
329
+ // isAborted may be false even after stop when using createUIMessageStream
330
+ const wasStopped = isAborted || chat.isStopped();
331
+ if (wasStopped) {
332
+ // handle stop — e.g. log analytics
333
+ }
334
+ },
335
+ stopWhen: stepCountIs(15),
336
+ });
337
+ },
338
+ });
339
+ ```
340
+
341
+ #### Cleaning up aborted messages
342
+
343
+ When stop happens mid-stream, the captured response message can contain parts in an incomplete state — tool calls stuck in `partial-call`, reasoning blocks still marked as `streaming`, etc. These can cause UI issues like permanent spinners.
344
+
345
+ `chat.agent` automatically cleans up the `responseMessage` when stop is detected before passing it to `onTurnComplete`. If you use `chat.pipe()` manually and capture response messages yourself, use `chat.cleanupAbortedParts()`:
346
+
347
+ ```ts
348
+ const cleaned = chat.cleanupAbortedParts(rawResponseMessage);
349
+ ```
350
+
351
+ This removes tool invocation parts stuck in `partial-call` state and marks any `streaming` text or reasoning parts as `done`.
352
+
353
+ <Note>
354
+ Stop signal delivery is best-effort. There is a small race window where the model may finish
355
+ before the stop signal arrives, in which case the turn completes normally with `stopped: false`.
356
+ This is expected and does not require special handling.
357
+ </Note>
358
+
359
+ ### Tool approvals
360
+
361
+ Tools with `needsApproval: true` pause execution until the user approves or denies via the frontend. Define the tool as normal and pass it to `streamText` — `chat.agent` handles the rest:
362
+
363
+ ```ts
364
+ const sendEmail = tool({
365
+ description: "Send an email. Requires human approval.",
366
+ inputSchema: z.object({ to: z.string(), subject: z.string(), body: z.string() }),
367
+ needsApproval: true,
368
+ execute: async ({ to, subject, body }) => {
369
+ await emailService.send({ to, subject, body });
370
+ return { sent: true };
371
+ },
372
+ });
373
+
374
+ export const myChat = chat.agent({
375
+ id: "my-chat",
376
+ run: async ({ messages, signal }) => {
377
+ return streamText({
378
+ model: anthropic("claude-sonnet-4-5"),
379
+ messages,
380
+ tools: { sendEmail },
381
+ abortSignal: signal,
382
+ stopWhen: stepCountIs(15),
383
+ });
384
+ },
385
+ });
386
+ ```
387
+
388
+ When the model calls an approval-required tool, the turn completes with the tool in `approval-requested` state. After the user approves on the frontend, the updated message is sent back and `chat.agent` replaces it in the conversation accumulator by matching the message ID. `streamText` then executes the approved tool and continues.
389
+
390
+ See [Tool approvals](/ai-chat/frontend#tool-approvals) in the frontend docs for the UI setup.
391
+
392
+ ### Persistence
393
+
394
+ To build a chat app that survives page refreshes you persist two things, both server-side from inside the agent:
395
+
396
+ 1. **Conversation state.** Full `UIMessage[]` keyed by `chatId`. Written from `onTurnStart` (so the user message is durable before streaming begins) and `onTurnComplete` (so the assistant reply lands).
397
+ 2. **Session state.** The transport's reconnect metadata: `publicAccessToken` and `lastEventId`. Written alongside the messages from the same hooks.
398
+
399
+ <Note>
400
+ Sessions let the transport reconnect to an existing run after a page refresh. Without them, every page load would start a new run, losing the conversation context that was accumulated in the previous run.
401
+ </Note>
402
+
403
+ For the full per-hook breakdown, race-condition warnings (atomic `lastEventId` writes, why not to use `chat.defer` in `onTurnStart`), token renewal via the `accessToken` callback, and an end-to-end three-file example, see [Database persistence](/ai-chat/patterns/database-persistence).
404
+
405
+ ### Pending messages (steering)
406
+
407
+ Users can send messages while the agent is executing tool calls. With `pendingMessages`, these messages are injected between tool-call steps, steering the agent mid-execution:
408
+
409
+ ```ts
410
+ export const myChat = chat.agent({
411
+ id: "my-chat",
412
+ pendingMessages: {
413
+ shouldInject: ({ steps }) => steps.length > 0,
414
+ },
415
+ run: async ({ messages, signal }) => {
416
+ return streamText({
417
+ ...chat.toStreamTextOptions({ registry }),
418
+ messages,
419
+ tools: {
420
+ /* ... */
421
+ },
422
+ abortSignal: signal,
423
+ stopWhen: stepCountIs(15),
424
+ });
425
+ },
426
+ });
427
+ ```
428
+
429
+ On the frontend, the `usePendingMessages` hook handles sending, tracking, and rendering injection points.
430
+
431
+ <Tip>
432
+ See [Pending Messages](/ai-chat/pending-messages) for the full guide — backend configuration,
433
+ frontend hook, queuing vs steering, and how injection works with all three chat variants.
434
+ </Tip>
435
+
436
+ ### Background injection
437
+
438
+ Inject context from background work into the conversation using `chat.inject()`. Combine with `chat.defer()` to run analysis between turns and inject results before the next response — self-review, RAG augmentation, safety checks, etc.
439
+
440
+ ```ts
441
+ export const myChat = chat.agent({
442
+ id: "my-chat",
443
+ onTurnComplete: async ({ messages }) => {
444
+ chat.defer(
445
+ (async () => {
446
+ const review = await generateObject({
447
+ /* ... */
448
+ });
449
+ if (review.object.needsImprovement) {
450
+ chat.inject([
451
+ {
452
+ role: "system",
453
+ content: `[Self-review]\n${review.object.suggestions.join("\n")}`,
454
+ },
455
+ ]);
456
+ }
457
+ })()
458
+ );
459
+ },
460
+ run: async ({ messages, signal }) => {
461
+ return streamText({ ...chat.toStreamTextOptions({ registry }), messages, abortSignal: signal });
462
+ },
463
+ });
464
+ ```
465
+
466
+ <Tip>
467
+ See [Background Injection](/ai-chat/background-injection) for the full guide — timing, self-review
468
+ example, and how it differs from pending messages.
469
+ </Tip>
470
+
471
+ ### Actions
472
+
473
+ Custom actions let the frontend send structured commands (undo, rollback, edit, regenerate) that modify the conversation state. **Actions are not turns**: they fire `hydrateMessages` (if set) and `onAction` only. The full surface (defining `actionSchema`, returning a model response from `onAction`, gating against pending HITL tool calls, and sending actions from the frontend) lives on its own page.
474
+
475
+ See [Actions](/ai-chat/actions).
476
+
477
+ ### Chat history
478
+
479
+ Imperative API for reading and modifying the accumulated message history. Works from any hook (`onAction`, `onTurnStart`, `onBeforeTurnComplete`, `onTurnComplete`, `hydrateMessages`) or from `run()` and AI SDK tools.
480
+
481
+ <Note>
482
+ The agent's accumulator — not `session.out` — is the source of truth for the full conversation. The `.out` stream is a bounded sliding window (roughly one turn at steady state, see [Records on `session.out`](/ai-chat/client-protocol#records-on-session-out)); the durable history lives in the agent's accumulator and is persisted to S3 between turns for fast next-run boots. `chat.history` reads and mutates that accumulator directly.
483
+ </Note>
484
+
485
+ **Reads.** Synchronous against the current accumulator state.
486
+
487
+ | Method | Description |
488
+ |--------|-------------|
489
+ | `chat.history.all()` | Returns a copy of the current accumulated UI messages. |
490
+ | `chat.history.getChain()` | Same as `all()`. Use whichever name reads better in context. |
491
+ | `chat.history.findMessage(messageId)` | Returns the message with that id, or `undefined`. |
492
+ | `chat.history.getPendingToolCalls()` | Tool calls on the most recent assistant message that are still in `input-available` state (waiting on `addToolOutput`). |
493
+ | `chat.history.getResolvedToolCalls()` | All tool calls in the chain in `output-available` or `output-error` state. |
494
+ | `chat.history.extractNewToolResults(message)` | Tool results in `message` whose `toolCallId` is not already resolved in the chain. Most useful in `hydrateMessages` against an incoming wire message, before the runtime merges it. |
495
+
496
+ Each pending and resolved entry is shaped `{ toolCallId, toolName, messageId }`. Each new-result entry is `{ toolCallId, toolName, output, errorText? }`, where `errorText` is set only for `output-error` parts.
497
+
498
+ **Mutations.** Applied at lifecycle checkpoints (after hooks return). Multiple mutations in the same hook compose correctly.
499
+
500
+ | Method | Description |
501
+ |--------|-------------|
502
+ | `chat.history.set(messages)` | Replace all messages. Same as `chat.setMessages()`. |
503
+ | `chat.history.remove(messageId)` | Remove a specific message by ID. |
504
+ | `chat.history.rollbackTo(messageId)` | Keep messages up to and including the given ID (undo). |
505
+ | `chat.history.replace(messageId, message)` | Replace a specific message by ID (edit). |
506
+ | `chat.history.slice(start, end?)` | Keep only messages in the given range. |
507
+
508
+ ```ts
509
+ // Undo the last exchange in onAction
510
+ onAction: async ({ action }) => {
511
+ if (action.type === "undo") {
512
+ chat.history.slice(0, -2);
513
+ }
514
+ },
515
+
516
+ // Trim history in onTurnComplete
517
+ onTurnComplete: async ({ uiMessages }) => {
518
+ if (uiMessages.length > 50) {
519
+ chat.history.slice(-20);
520
+ }
521
+ },
522
+ ```
523
+
524
+ The HITL reads let an action or hook decide what to do without walking the accumulator manually:
525
+
526
+ ```ts
527
+ // Refuse a regenerate while a tool call is still awaiting an answer
528
+ onAction: async ({ action }) => {
529
+ if (action.type === "regenerate") {
530
+ if (chat.history.getPendingToolCalls().length > 0) return;
531
+ chat.history.slice(0, -1);
532
+ }
533
+ },
534
+
535
+ // Side-effect once per net-new tool result when wire messages come in
536
+ hydrateMessages: async ({ incomingMessages }) => {
537
+ for (const msg of incomingMessages) {
538
+ for (const r of chat.history.extractNewToolResults(msg)) {
539
+ await onToolResolved({ id: r.toolCallId, output: r.output, errorText: r.errorText });
540
+ }
541
+ }
542
+ return incomingMessages;
543
+ },
544
+ ```
545
+
546
+ `extractNewToolResults` compares against the *current* chain. Inside `onTurnComplete`, the chain already contains the just-finished `responseMessage`, so it returns `[]`. Use it where the message is from outside the accumulator: `hydrateMessages` (incoming wire), `onAction` if the action carries a message, or any custom pre-merge code path.
547
+
548
+ ### prepareMessages
549
+
550
+ Transform model messages before they're used anywhere — in `run()`, in compaction rebuilds, and in compaction results. Define once, applied everywhere.
551
+
552
+ Use this for Anthropic cache breaks, injecting system context, stripping PII, etc.
553
+
554
+ ```ts
555
+ export const myChat = chat.agent({
556
+ id: "my-chat",
557
+ prepareMessages: ({ messages, reason }) => {
558
+ // Add Anthropic cache breaks to the last message
559
+ if (messages.length === 0) return messages;
560
+ const last = messages[messages.length - 1];
561
+ return [
562
+ ...messages.slice(0, -1),
563
+ {
564
+ ...last,
565
+ providerOptions: {
566
+ ...last.providerOptions,
567
+ anthropic: { cacheControl: { type: "ephemeral" } },
568
+ },
569
+ },
570
+ ];
571
+ },
572
+ run: async ({ messages, signal }) => {
573
+ return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
574
+ },
575
+ });
576
+ ```
577
+
578
+ The `reason` field tells you why messages are being prepared:
579
+
580
+ | Reason | Description |
581
+ | ---------------------- | ------------------------------------------------- |
582
+ | `"run"` | Messages being passed to `run()` for `streamText` |
583
+ | `"compaction-rebuild"` | Rebuilding from a previous compaction summary |
584
+ | `"compaction-result"` | Fresh compaction just produced these messages |
585
+
586
+ ### Version upgrades
587
+
588
+ 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.
589
+
590
+ ### Ending a run on your terms
591
+
592
+ 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.
593
+
594
+ ```ts
595
+ chat.agent({
596
+ id: "one-shot",
597
+ run: async ({ messages, signal }) => {
598
+ // Single-response agent — exit after this turn.
599
+ chat.endRun();
600
+ return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
601
+ },
602
+ });
603
+ ```
604
+
605
+ 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.
606
+
607
+ 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.
608
+
609
+ <Warning>
610
+ If you persist `lastEventId` to your own storage for cross-page-load resume, **don't clear it on `chat.endRun()`**. The cursor is sessionId-keyed and stays valid across Run boundaries — clearing it forces the next `sendMessages` to subscribe from `seq_num=0`, where it may hit the prior turn's stale `turn-complete` record and close the stream empty before the new Run's chunks arrive.
611
+ </Warning>
612
+
613
+ ### Runtime configuration
614
+
615
+ #### chat.setTurnTimeout()
616
+
617
+ Override how long the run stays suspended waiting for the next message. Call from inside `run()`:
618
+
619
+ ```ts
620
+ run: async ({ messages, signal }) => {
621
+ chat.setTurnTimeout("2h"); // Wait longer for this conversation
622
+ return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
623
+ },
624
+ ```
625
+
626
+ #### chat.setIdleTimeoutInSeconds()
627
+
628
+ Override how long the run stays idle (active, using compute) after each turn:
629
+
630
+ ```ts
631
+ run: async ({ messages, signal }) => {
632
+ chat.setIdleTimeoutInSeconds(60); // Stay idle for 1 minute
633
+ return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
634
+ },
635
+ ```
636
+
637
+ <Info>
638
+ Longer idle timeout means faster responses but more compute usage. Set to `0` to suspend
639
+ immediately after each turn (minimum latency cost, slight delay on next message).
640
+ </Info>
641
+
642
+ #### Stream options
643
+
644
+ Control how `streamText` results are converted to the frontend stream via `toUIMessageStream()`. Set static defaults on the task, or override per-turn.
645
+
646
+ ##### Error handling with onError
647
+
648
+ When `streamText` encounters an error mid-stream (rate limits, API failures, network errors), the `onError` callback converts it to a string that's sent to the frontend as an `{ type: "error", errorText }` chunk. The AI SDK's `useChat` receives this via its `onError` callback.
649
+
650
+ By default, the raw error message is sent to the frontend. Use `onError` to sanitize errors and avoid leaking internal details:
651
+
652
+ ```ts
653
+ export const myChat = chat.agent({
654
+ id: "my-chat",
655
+ uiMessageStreamOptions: {
656
+ onError: (error) => {
657
+ // Log the full error server-side for debugging
658
+ console.error("Stream error:", error);
659
+ // Return a sanitized message — this is what the frontend sees
660
+ if (error instanceof Error && error.message.includes("rate limit")) {
661
+ return "Rate limited — please wait a moment and try again.";
662
+ }
663
+ return "Something went wrong. Please try again.";
664
+ },
665
+ },
666
+ run: async ({ messages, signal }) => {
667
+ return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
668
+ },
669
+ });
670
+ ```
671
+
672
+ `onError` is also called for tool execution errors, so a single handler covers both LLM errors and tool failures.
673
+
674
+ On the frontend, handle the error in `useChat`:
675
+
676
+ ```tsx
677
+ const { messages, sendMessage } = useChat({
678
+ transport,
679
+ onError: (error) => {
680
+ // error.message contains the string returned by your onError handler
681
+ toast.error(error.message);
682
+ },
683
+ });
684
+ ```
685
+
686
+ ##### Reasoning and sources
687
+
688
+ Control which AI SDK features are forwarded to the frontend:
689
+
690
+ ```ts
691
+ export const myChat = chat.agent({
692
+ id: "my-chat",
693
+ uiMessageStreamOptions: {
694
+ sendReasoning: true, // Forward model reasoning (default: true)
695
+ sendSources: true, // Forward source citations (default: false)
696
+ },
697
+ run: async ({ messages, signal }) => {
698
+ return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
699
+ },
700
+ });
701
+ ```
702
+
703
+ ##### Custom message IDs
704
+
705
+ By default, response message IDs are generated using the AI SDK's built-in `generateId`. Pass a custom `generateMessageId` function to use your own ID format (e.g. UUID-v7):
706
+
707
+ ```ts
708
+ import { v7 as uuidv7 } from "uuid";
709
+
710
+ export const myChat = chat.agent({
711
+ id: "my-chat",
712
+ uiMessageStreamOptions: {
713
+ generateMessageId: () => uuidv7(),
714
+ },
715
+ run: async ({ messages, signal }) => {
716
+ return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
717
+ },
718
+ });
719
+ ```
720
+
721
+ With the `.withUIMessage()` builder, set it under `streamOptions`:
722
+
723
+ ```ts
724
+ import { v7 as uuidv7 } from "uuid";
725
+
726
+ export const myChat = chat
727
+ .withUIMessage<MyChatUIMessage>({
728
+ streamOptions: {
729
+ generateMessageId: () => uuidv7(),
730
+ sendReasoning: true,
731
+ },
732
+ })
733
+ .agent({
734
+ id: "my-chat",
735
+ run: async ({ messages, signal }) => {
736
+ return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
737
+ },
738
+ });
739
+ ```
740
+
741
+ <Info>
742
+ The generated ID is sent to the frontend in the stream's `start` chunk, so frontend and backend
743
+ always reference the same ID for each message. This is important for features like tool
744
+ approvals, where the frontend resends an assistant message and the backend needs to match it
745
+ by ID in the conversation accumulator.
746
+ </Info>
747
+
748
+ ##### Per-turn overrides
749
+
750
+ Override per-turn with `chat.setUIMessageStreamOptions()` — per-turn values merge with the static config (per-turn wins on conflicts). The override is cleared automatically after each turn.
751
+
752
+ ```ts
753
+ run: async ({ messages, clientData, signal }) => {
754
+ // Enable reasoning only for certain models
755
+ if (clientData.model?.includes("claude")) {
756
+ chat.setUIMessageStreamOptions({ sendReasoning: true });
757
+ }
758
+ return streamText({ model: openai(clientData.model ?? "gpt-4o"), messages, abortSignal: signal });
759
+ },
760
+ ```
761
+
762
+ `chat.setUIMessageStreamOptions()` works across all abstraction levels — `chat.agent()`, `chat.createSession()` / `turn.complete()`, and `chat.pipeAndCapture()`.
763
+
764
+ See [ChatUIMessageStreamOptions](/ai-chat/reference#chatuimessagestreamoptions) for the full reference.
765
+
766
+ <Note>
767
+ `onFinish` is managed internally for response capture and cannot be overridden here. Use
768
+ `streamText`'s `onFinish` callback for custom finish handling, or use [raw task
769
+ mode](/ai-chat/custom-agents) for full control over `toUIMessageStream()`.
770
+ </Note>
771
+
772
+ ### Manual mode with task()
773
+
774
+ If you need full control over task options, use the standard `task()` with `ChatTaskPayload` and `chat.pipe()`:
775
+
776
+ ```ts
777
+ import { task } from "@trigger.dev/sdk";
778
+ import { chat, type ChatTaskPayload } from "@trigger.dev/sdk/ai";
779
+ import { streamText } from "ai";
780
+ import { anthropic } from "@ai-sdk/anthropic";
781
+
782
+ export const manualChat = task({
783
+ id: "manual-chat",
784
+ retry: { maxAttempts: 3 },
785
+ queue: { concurrencyLimit: 10 },
786
+ run: async (payload: ChatTaskPayload) => {
787
+ const result = streamText({
788
+ model: anthropic("claude-sonnet-4-5"),
789
+ messages: payload.messages,
790
+ stopWhen: stepCountIs(15),
791
+ });
792
+
793
+ await chat.pipe(result);
794
+ },
795
+ });
796
+ ```
797
+
798
+ <Warning>
799
+ Manual mode does not get automatic message accumulation or the `onTurnComplete`/`onChatStart`
800
+ lifecycle hooks. The `responseMessage` field in `onTurnComplete` will be `undefined` when using
801
+ `chat.pipe()` directly. Use `chat.agent()` for the full multi-turn experience.
802
+ </Warning>
803
+
804
+ ---
805
+
806
+ {/* Anchor stubs for inbound deep links to the sections that moved to /ai-chat/custom-agents. */}
807
+ <a id="chat-createsession" />
808
+ <a id="chat-customagent" />
809
+ <a id="raw-task-with-primitives" />
810
+
811
+ ## Custom agents
812
+
813
+ Both lower levels — `chat.createSession()` (managed turn iterator, your turn body) and `chat.customAgent()` with raw primitives (hand-rolled loop, full stream-conversion control) — are covered together on the Custom agents page, including the `ChatTurn` surface, the continuation-seeding pattern, and the hand-rolled-loop checklist:
814
+
815
+ <Card title="Custom agents" icon="screwdriver-wrench" href="/ai-chat/custom-agents">
816
+ Build agents without the managed lifecycle — createSession or raw primitives.
817
+ </Card>