Queueing User Messages
When using multi-turn workflows, messages typically arrive between agent turns. The workflow waits at a hook, receives a message, then starts a new turn. But sometimes you need to inject messages during an agent's turn, before tool calls complete or while the model is reasoning.
WorkflowAgent's prepareStep callback enables this by running before each step in the agent loop, giving you a chance to inject queued messages into the conversation. prepareStep also allows you to modify the model choice and existing messages mid-turn, see AI SDK's prepareStep callback for more details.
When to use this
Message queueing is useful when:
- Users send follow-up messages while the agent is still searching for flights or processing bookings
- External systems need to inject context mid-turn (e.g., a flight status webhook fires during processing)
- You want messages to influence the agent's next step rather than waiting for the current turn to complete
If you need basic multi-turn conversations where messages arrive between turns, see Chat Session Modeling. This guide covers the more advanced case of injecting messages during turns.
The prepareStep callback
The prepareStep callback runs before each step in the agent loop. Use WorkflowAgent's exported types rather than redeclaring its normalized provider-prompt contract:
PrepareStepInfo.messages is a normalized LanguageModelV4Prompt, not the application-level ModelMessage[] accepted by WorkflowAgent.stream().
Queueing messages during and between turns
Use one async Hook consumer and one FIFO. prepareStep atomically drains messages that arrived during a model turn; messages that arrive after the final model step become input to the next turn. Each Hook payload therefore has exactly one ownership path.
Messages sent via chatMessageHook.resume() accumulate until either prepareStep or the between-turn branch drains the FIFO. Send /done to stop the consumer and let the workflow return.
Related documentation
- Chat Session Modeling - Single-turn vs multi-turn patterns
- Building Durable AI Agents - Complete guide to creating durable agents
WorkflowAgent- AI SDK API for durable, resumable agentsdefineHook()API Reference - Hook configuration options