DurableAgent’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.
When to Use This
Message queueing is useful when:- Users send follow-up messages while the agent is still processing
- External systems need to inject context mid-turn (e.g., a 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 just 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. It receives the current state and can modify the messages sent to the model:
lineNumbers
Injecting Queued Messages
Combine a message queue withprepareStep to inject messages that arrive during processing:
workflows/chat/index.ts
chatMessageHook.resume() accumulate in the queue and get injected before the next step, whether that’s a tool call or another LLM request.
The
prepareStep callback receives messages in LanguageModelV2Prompt format (with content arrays), which is the internal format used by the AI SDK.Combining with Multi-Turn Sessions
You can combine message queueing with the standard multi-turn pattern:workflows/chat/index.ts
Advanced Use Cases
Context Switching
Switch the model or adjust parameters mid-conversation:lineNumbers
Dynamic Tool Activation
Enable or disable tools based on context:lineNumbers
Conversation Summarization
Summarize old messages to manage context window:lineNumbers
Rate Limit Handling
Adjust request rate based on API limits:lineNumbers
Related Documentation
- Chat Session Modeling - Single-turn vs multi-turn patterns
- Building Durable AI Agents - Complete guide to creating durable agents
DurableAgentAPI Reference - Full API documentationdefineHook()API Reference - Hook configuration options