Title: bug: chat.agent drops tool results from step 3 on when prepareStep returns messages (AI SDK 7) · Issue #5077 · triggerdotdev/trigger.dev · GitHub
Open Graph Title: bug: chat.agent drops tool results from step 3 on when prepareStep returns messages (AI SDK 7) · Issue #5077 · triggerdotdev/trigger.dev
X Title: bug: chat.agent drops tool results from step 3 on when prepareStep returns messages (AI SDK 7) · Issue #5077 · triggerdotdev/trigger.dev
Description: Provide environment information System: OS: Linux 7.2 CachyOS Linux CPU: (16) x64 AMD Ryzen 7 4800H with Radeon Graphics Memory: 2.75 GB / 15.04 GB Container: Yes Shell: 5.9.2 - /usr/bin/zsh Binaries: Node: 24.18.0 - /home/senpai/.local/...
Open Graph Description: Provide environment information System: OS: Linux 7.2 CachyOS Linux CPU: (16) x64 AMD Ryzen 7 4800H with Radeon Graphics Memory: 2.75 GB / 15.04 GB Container: Yes Shell: 5.9.2 - /usr/bin/zsh Binari...
X Description: Provide environment information System: OS: Linux 7.2 CachyOS Linux CPU: (16) x64 AMD Ryzen 7 4800H with Radeon Graphics Memory: 2.75 GB / 15.04 GB Container: Yes Shell: 5.9.2 - /usr/bin/zsh Binari...
Opengraph URL: https://github.com/triggerdotdev/trigger.dev/issues/5077
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"bug: chat.agent drops tool results from step 3 on when prepareStep returns messages (AI SDK 7)","articleBody":"### Provide environment information\n\n```\n System:\n OS: Linux 7.2 CachyOS Linux\n CPU: (16) x64 AMD Ryzen 7 4800H with Radeon Graphics\n Memory: 2.75 GB / 15.04 GB\n Container: Yes\n Shell: 5.9.2 - /usr/bin/zsh\n Binaries:\n Node: 24.18.0 - /home/senpai/.local/share/nvm/v24.18.0/bin/node\n npm: 12.0.1 - /home/senpai/.local/share/nvm/v24.18.0/bin/npm\n pnpm: 11.13.1 - /home/senpai/.local/share/nvm/v24.18.0/bin/pnpm\n bun: 1.3.14 - /home/senpai/.bun/bin/bun\n```\n\nPackages: `@trigger.dev/sdk@4.8.1`, `ai@7.0.153`, `zod@4.6.7`.\n\n\n### Describe the bug\n\nWith AI SDK 7, if a `chat.agent`'s `streamText` has a `prepareStep` that returns `messages`, the model stops receiving new tool results after the first one. From step 3 on, every prompt contains only the first tool call and its result. The model never sees its later results, so it repeats the same tool call until `stopWhen` ends the turn.\n\nEven a no-op `prepareStep: ({ messages }) => ({ messages })` triggers it. In our production agent this showed up as the model running the same shell command dozens of times in one turn.\n\n**Expected:** each step's prompt includes every earlier tool call and result, the same as without `prepareStep`.\n\n**Cause:** `chat.agent` wraps `prepareStep` in `retainStepMessages` ([`packages/trigger-sdk/src/v3/steeringContext.ts`](https://github.com/triggerdotdev/trigger.dev/blob/fc33769a/packages/trigger-sdk/src/v3/steeringContext.ts#L37-L41)):\n\n```ts\nconst responses = input.steps.at(-1)?.response.messages ?? [];\nconst messages = retained ? [...retained, ...responses.slice(responseCount)] : input.messages;\n// ...\nresponseCount = responses.length;\n```\n\nThe doc comment above it states the assumption that no longer holds:\n\n> The same logic works when the installed AI SDK already retains overrides: its input is not concatenated again.\n\nThat code was written for AI SDK 5/6, where `step.response.messages` holds every message so far, so slicing off the first `responseCount` leaves only the new ones. AI SDK 7 changed this (migration guide, \"Step Response Messages Are No Longer Accumulated\"): each step's `response.messages` holds only that step's messages. A tool step always yields 2 messages (tool call + tool result), so `responseCount` stays at 2, and from step 3 on `responses.slice(2)` is empty. Nothing new is added to `retained`, and the stale copy replaces the correct `input.messages`.\n\n| Step | `responses` (AI SDK 7) | `responseCount` | Added to history |\n|---|---|---|---|\n| 2 | step 1's 2 messages | 0 | both ✓ |\n| 3 | step 2's 2 messages | 2 | nothing ✗ |\n| 4 | step 3's 2 messages | 2 | nothing ✗ |\n\nAll of these are needed to hit it:\n\n1. `ai` 7.x. With `ai@6.0.315`, the same code works.\n2. Something returns `messages` from `prepareStep`: either the user's `prepareStep`, or the SDK's own built-in one (steering via pending messages, compaction, `chat.inject`). Until the first override, the wrapper passes `input.messages` through.\n3. At least 3 tool steps in one turn. Step 2 is always correct.\n\nBecause of (2), this also affects agents **without** a user `prepareStep`: a single steering message injected mid-turn is enough. With no user `prepareStep` and one steering message on `ai@7`, steps 3 and 4 both see `[\"user:go\", \"result-1\", \"user:steer\"]` (result-2 and result-3 missing); on `ai@6` the same run is correct.\n\n\n### Reproduction repo\n\nhttps://github.com/iharsh02/BugsBunny/tree/02550d8/trigger-repro\n\n### To reproduce\n\n```bash\ngit clone https://github.com/iharsh02/BugsBunny && cd BugsBunny/trigger-repro\nnpm ci\nnode repro.mjs control # no prepareStep\nnode repro.mjs # no-op prepareStep\n```\n\n`repro.mjs` uses only public API and needs no network or API keys. It runs a `chat.agent` through `mockChatAgent` from `@trigger.dev/sdk/ai/test`, with a scripted `MockLanguageModelV3` that calls a `lookup` tool 3 times (its `execute` returns `result-N`) and then answers. Each model call logs the tool results in the `prompt` it actually received.\n\nThe core of it:\n\n```js\nconst agent = chat.agent({\n id: \"repro\",\n run: async ({ messages, signal, streamText }) =>\n streamText({\n model,\n messages,\n tools: { lookup },\n stopWhen: isStepCount(8),\n abortSignal: signal,\n ...(usePrepareStep && { prepareStep: ({ messages }) => ({ messages }) }),\n }),\n});\n```\n\nOutput:\n\n```\n$ node repro.mjs control\nmodel call 1 sees: []\nmodel call 2 sees: [ 'result-1' ]\nmodel call 3 sees: [ 'result-1', 'result-2' ]\nmodel call 4 sees: [ 'result-1', 'result-2', 'result-3' ]\n\n$ node repro.mjs\nmodel call 1 sees: []\nmodel call 2 sees: [ 'result-1' ]\nmodel call 3 sees: [ 'result-1' ] \u003c- result-2 missing\nmodel call 4 sees: [ 'result-1' ] \u003c- result-2 and result-3 missing\n```\n\nWith `npm i ai@6.0.315`, both runs match.\n\n\n### Suggested fix\n\nAI SDK 7 passes the accumulated history to `prepareStep` as `responseMessages`. AI SDK 5/6 don't have that field, so falling back keeps their behaviour unchanged:\n\n```diff\n type StepInput = {\n messages: ModelMessage[];\n steps: Array\u003c{ response: { messages: ModelMessage[] } }>;\n+ /** AI SDK 7+: response messages accumulated across all steps so far. */\n+ responseMessages?: ModelMessage[];\n };\n\n- const responses = input.steps.at(-1)?.response.messages ?? [];\n+ const responses = input.responseMessages ?? input.steps.at(-1)?.response.messages ?? [];\n```\n\nI applied this change to the built `dist` of 4.8.1 and the repro passes on `ai@5.0.281`, `ai@6.0.315` and `ai@7.0.153`, including the steering case above.\n\nThe obvious alternative, dropping the slice (`[...retained, ...responses]`), fixes AI SDK 7 but duplicates tool results on AI SDK 5/6: step 3 sees `['result-1', 'result-1', 'result-2']`. The SDK's peer range still includes `ai` 5 and 6, so that version isn't safe.\n\n**Why CI didn't catch it:** the repo's dev dependency is `ai ^6`, so the suite runs on v6. The existing \"SDK retains overrides\" test feeds v6-style accumulated step responses together with v7's carry-forward of overrides, a combination no real AI SDK version produces. Happy to open a PR with the fix plus a v7-shaped regression test.\n\n\n### Additional information\n\n**With a real model.** `real-agent.mjs` in the repro repo runs the same `chat.agent` with a real model (`minimax/minimax-m3.1-flash` via the AI Gateway). Its `getCode` tool returns a random code and takes the previous code as input, so the call's argument shows what the model thinks the last code was. The prompt is \"Call getCode exactly 3 times, one call at a time. Then reply with only the 3 codes joined by dashes.\"\n\nWithout `prepareStep`:\n\n```\nmodel call 1 sees: []\n getCode -> 5Y (previous: \"\")\nmodel call 2 sees: [ '5Y' ]\n getCode -> ZG (previous: \"5Y\")\nmodel call 3 sees: [ '5Y', 'ZG' ]\n getCode -> PE (previous: \"ZG\")\nmodel call 4 sees: [ '5Y', 'ZG', 'PE' ]\n answer: \"5Y-ZG-PE\" ✓\n```\n\nWith `prepareStep: ({ messages }) => ({ messages })`:\n\n```\nmodel call 1 sees: []\n getCode -> CP (previous: \"\")\nmodel call 2 sees: [ 'CP' ]\n getCode -> L2 (previous: \"CP\")\nmodel call 3 sees: [ 'CP' ]\n getCode -> KP (previous: \"CP\")\nmodel call 4 sees: [ 'CP' ]\n getCode -> 0B (previous: \"CP\")\n...\nmodel call 8 sees: [ 'CP' ]\n getCode -> 8I (previous: \"CP\")\ngetCode calls: 8 (asked for 3), no answer — stopped by isStepCount(8)\n```\n\nThe model keeps passing `previous: \"CP\"` because it never sees `L2`, `KP` and the later codes. That's the same loop we hit in production.\n\n**Affected versions.** I reproduced this on `@trigger.dev/sdk` 4.8.1 with `ai` 7.0.153. We first hit it on 4.7.5 and also saw it on 4.7.7 and with `ai` 7.0.146. The code is unchanged on `main` as of fc33769a (2026-10-26).\n\n**Workaround until a fix ships.** In `dist/esm/v3/steeringContext.js` and `dist/commonjs/v3/steeringContext.js`, change the `const responses = ...` line to `const responses = input.responseMessages ?? input.steps.at(-1)?.response.messages ?? [];` (the type addition only applies to the TS source). Or don't return `messages` from `prepareStep` (this doesn't help with SDK steering/compaction).\n","author":{"url":"https://github.com/iharsh02","@type":"Person","name":"iharsh02"},"datePublished":"2026-10-26T19:47:38Z","url":"https://github.com/triggerdotdev/trigger.dev/issues/5077"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:af1525da-85ca-07bb-5ccc-7e1df0751c57 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E6B0:38C411:5BCCB0:809DDF:6AFB6168 |
| html-safe-nonce | 7b3e6bb455b16a2a9ed5718d83de071f33e0ef7e890a16d40ba522cf739d95be |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNkIwOjM4QzQxMTo1QkNDQjA6ODA5RERGOjZBRkI2MTY4IiwidmlzaXRvcl9pZCI6IjY1MjQ1MTcxNjY1NDcxNjU1NDQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f556bc786b432d69993e9015c0ce646ddfff53903aa547642d5fcba62fea2ed9 |
| hovercard-subject-tag | issue:6046988784 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/triggerdotdev/trigger.dev/5077/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cb9c3af4fc710be9384d0a9d4f87c07ca506583cdf0454483a2fe22bc63e49a8/triggerdotdev/trigger.dev/issues/5077 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cb9c3af4fc710be9384d0a9d4f87c07ca506583cdf0454483a2fe22bc63e49a8/triggerdotdev/trigger.dev/issues/5077 |
| og:image:alt | Provide environment information System: OS: Linux 7.2 CachyOS Linux CPU: (16) x64 AMD Ryzen 7 4800H with Radeon Graphics Memory: 2.75 GB / 15.04 GB Container: Yes Shell: 5.9.2 - /usr/bin/zsh Binari... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | iharsh02 |
| hostname | github.com |
| expected-hostname | github.com |
| None | ea33d13c96d0ff66bd8fa906b193d2213536b78cb06b0c50a4632c58b1418f94 |
| turbo-cache-control | no-preview |
| go-import | github.com/triggerdotdev/trigger.dev git https://github.com/triggerdotdev/trigger.dev.git |
| octolytics-dimension-user_id | 95297378 |
| octolytics-dimension-user_login | triggerdotdev |
| octolytics-dimension-repository_id | 572570113 |
| octolytics-dimension-repository_nwo | triggerdotdev/trigger.dev |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 572570113 |
| octolytics-dimension-repository_network_root_nwo | triggerdotdev/trigger.dev |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 59ef97164c52cf59bd10558ddec9c4f482763988 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width