Overview
chat.inject() queues model messages for injection into the conversation. Messages are picked up at the start of the next turn or at the next prepareStep boundary (between tool-call steps).
This is the backend counterpart to pending messages. Pending messages come from the user via the frontend, while chat.inject() comes from your task code.
Basic usage
Common pattern: defer + inject
The most powerful pattern combineschat.defer() (background work) with chat.inject() (inject results). Background work runs in parallel with the idle wait between turns, and results are injected before the next response.
Timing
- Turn completes,
onTurnCompletefires chat.defer()registers the background work- The run immediately starts waiting for the next message (no blocking)
- Background work completes,
chat.inject()queues the messages - User sends next message, turn starts
- Injected messages are appended before
run()executes - The LLM sees the injected context alongside the new user message
prepareStep boundary instead.
Example: self-review
A cheap model reviews the agent’s response after each turn and injects coaching for the next one. Uses Prompts for the review prompt andgenerateObject for structured output.
claude-haiku-4-5 (fast, cheap) in the background. If the user sends another message before it completes, the coaching is still injected, because chat.inject() persists across the idle wait.
Other use cases
- RAG augmentation: After each turn, fetch relevant documents and inject them as context for the next response
- Safety checks: Run a moderation model on the response, inject warnings if issues are detected
- Fact-checking: Verify claims in the response using search tools, inject corrections
- Context enrichment: Look up user/account data based on what was discussed, inject it as system context
chat.defer standalone
chat.defer() is also useful on its own, without chat.inject(). Any work whose timing has no resume implication (analytics, audit logs, search-index writes, cache warming) can run in parallel with streaming instead of in the critical path. All deferred promises are awaited (with a 5s timeout) before onTurnComplete fires.
chat.defer() can be called from anywhere during a turn: hooks, run(), or nested helpers. All deferred promises are collected and awaited together before onTurnComplete.
How it differs from pending messages
Two lanes: trusted and untrusted
The role you inject with decides more than position. It decides whether the model treats the content as trustworthy.role: "system" goes to the instructions lane. The block is appended to the
system instructions for subsequent inference calls, so it carries the same standing
as your system prompt. This is the lane for context the agent should believe:
entitlements, plan changes, operational notices.
It has to work this way. On AI SDK 7 a system message inside messages is rejected
for every provider. standardizePrompt throws before any provider is called, and
its own advice is to use the instructions option, so the injected block goes there
rather than into the transcript.
- An injection applies to the next turn only. A block injected in
onTurnCompleteshapes the following turn and is cleared after it, so it is not repeated on every turn from then on. Within that turn it is consumed once rather than once per read, so arun()that builds options more than once sees the same instructions in every build. - The injected text is merged into a single instruction rather than added as a second block, because AI SDK 5 rejects an array of system blocks while accepting one structured block. Merging changes the cached prefix, so a cached system prompt gets no cache hit for as long as an injection is live. If you rely on prompt caching, inject sparingly and prefer facts that go stale, so the injection clears.
user is indistinguishable from something the user typed, and a
well-aligned model treats it accordingly, and may say so and re-derive the answer
from tools instead of taking it at face value:
“that text arrived embedded in your message, not from a tool I called, so I verified it myself rather than trusting it”That is correct behaviour, not a bug. So inject checkable facts in the conversational lane and put directives in the instructions lane. A conclusion injected as a user message is the worst of both: the model neither trusts it nor ignores it, and may contradict it in front of the user.
API reference
chat.inject()
Messages are drained (consumed) when:
- A new turn starts, before
run()executes - A
prepareStepboundary is reached, between tool-call steps during streaming
chat.inject() writes to an in-memory queue in the current process. It works from any code running in the same task: lifecycle hooks, deferred work, tool execute functions, etc. It does not work from subtasks or other runs.
