Skip to main content

Overview

When an AI agent is executing tool calls, users may want to send a message that steers the agent mid-execution — adding context, correcting course, or refining the request without waiting for the response to finish. By default (without pendingMessages), a message sent while the agent is responding never interrupts the in-flight response: it’s buffered and processed as its own turn once the current turn completes, with multiple messages running sequentially in arrival order. The pendingMessages option enables steering instead, injecting user messages between tool-call steps via the AI SDK’s prepareStep. Messages that arrive during streaming are queued and injected at the next step boundary. If there are no more step boundaries (single-step response or final text generation), the message becomes the next turn automatically.

How it works

  1. User sends a message while the agent is streaming
  2. The message is sent to the backend via input stream (transport.sendPendingMessage)
  3. The backend queues it in the steering queue
  4. At the next prepareStep boundary (between tool-call steps), shouldInject is called
  5. If it returns true, the message is injected into the LLM’s context
  6. A data-pending-message-injected stream chunk confirms injection to the frontend
  7. If prepareStep never fires (no tool calls), the message becomes the next turn

Backend: chat.agent

Add pendingMessages to your chat.agent configuration:
The prepareStep for injection is automatically included when you spread chat.toStreamTextOptions(). If you provide your own prepareStep after the spread, it overrides the auto-injected one.

Options

OptionTypeDescription
shouldInject(event: PendingMessagesBatchEvent) => booleanDecide whether to inject the batch. Called once per step boundary. If absent, no injection happens.
prepare(event: PendingMessagesBatchEvent) => ModelMessage[]Transform the batch before injection. Default: convert each message via convertToModelMessages.
onReceived(event) => voidCalled when a message arrives during streaming (per-message).
onInjected(event) => voidCalled after a batch is injected.

shouldInject

Called once per step boundary with the full batch of pending messages. Return true to inject all of them, false to skip (they’ll be available at the next boundary or become the next turn).
The event includes:
FieldTypeDescription
messagesUIMessage[]All pending messages (batch)
modelMessagesModelMessage[]Current conversation
stepsCompactionStep[]Completed steps
stepNumbernumberCurrent step (0-indexed)
chatIdstringChat session ID
turnnumberCurrent turn
clientDataunknownFrontend metadata

prepare

Transform the batch of pending messages before they’re injected into the LLM’s context. By default, each UIMessage is converted to ModelMessages individually. Use prepare to combine multiple messages or add context:

Stream chunk

When messages are injected, the SDK automatically writes a data-pending-message-injected stream chunk containing the message IDs and text. The frontend uses this to:
  • Confirm which messages were injected
  • Remove them from the pending overlay
  • Render them inline at the injection point in the assistant response
A “pending message injected” span also appears in the run trace.

Backend: chat.createSession

Pass pendingMessages to the session options:
Use turn.prepareStep() to get a prepareStep function that handles both injection and compaction. Users who spread chat.toStreamTextOptions() get it automatically.

Backend: MessageAccumulator (raw task)

Pass pendingMessages to the constructor and wire up the message listener manually:

MessageAccumulator methods

MethodDescription
steer(message, modelMessages?)Queue a UIMessage for injection (sync)
steerAsync(message)Queue a UIMessage, converting to model messages automatically
drainSteering()Get and clear unconsumed steering messages
prepareStep()Returns a prepareStep function handling injection + compaction

Frontend: usePendingMessages hook

The usePendingMessages hook manages all the frontend complexity — tracking pending messages, detecting injections, and handling the turn lifecycle.

Hook API

Property/MethodTypeDescription
pendingPendingMessage[]Current pending messages with id, text, mode, and injected status
steer(text)(text: string) => voidSend a steering message during streaming, or normal message when ready
queue(text)(text: string) => voidQueue for next turn during streaming, or send normally when ready
promoteToSteering(id)(id: string) => voidConvert a queued message to steering (sends via input stream immediately)
isInjectionPoint(part)(part: unknown) => booleanCheck if an assistant message part is an injection confirmation
getInjectedMessageIds(part)(part: unknown) => string[]Get message IDs from an injection point
getInjectedMessages(part)(part: unknown) => InjectedMessage[]Get messages (id + text) from an injection point

PendingMessage

FieldTypeDescription
idstringUnique message ID
textstringMessage text
mode"steering" | "queued"How the message is being handled
injectedbooleanWhether the backend confirmed injection

Message lifecycle

  • Steering messages are sent via transport.sendPendingMessage() immediately. They appear as purple pending bubbles. If injected, they disappear from the overlay and render inline at the injection point. If not injected (no more step boundaries), they auto-send as the next turn when the response finishes.
  • Queued messages stay client-side until the turn completes, then auto-send as the next turn via sendMessage(). They can be promoted to steering mid-stream by clicking “Steer instead”.
  • Promoted messages are queued messages that were converted to steering. They get sent via input stream immediately and follow the steering lifecycle from that point.

Transport: sendPendingMessage

The TriggerChatTransport exposes a sendPendingMessage method for sending messages via input stream without disrupting the active stream subscription:
Unlike sendMessage() from useChat, this does NOT:
  • Add the message to useChat’s local state
  • Cancel the active stream subscription
  • Start a new response stream
The usePendingMessages hook calls this internally — you typically don’t need to use it directly.

Coexistence with compaction

Pending message injection and compaction both use prepareStep. When both are configured, the auto-injected prepareStep handles them in order:
  1. Compaction runs first — checks threshold, generates summary if needed
  2. Injection runs second — pending messages are appended to either the compacted or original messages
This means injected messages are always included after compaction, ensuring the LLM sees both the compressed history and the new steering input.