Interleaving User Input
You'll learn how to handle situations where users provide new information or instructions while your AI agent is in the middle of executing tools or waiting for results.
The Challenge of Mid-Flow User Input
When your AI agent executes a series of tool calls across multiple conversation turns, users don't always wait patiently for completion. They might:
- Provide missing information the agent asked for
- Clarify their original request
- Change their mind mid-execution
- Add constraints or new requirements
Think of it like ordering a custom pizza: You ask for pepperoni, the chef starts preparing, but before they finish you shout "Actually, add mushrooms too!" The chef needs to incorporate your update without starting over.
How It Works
Interleaving user input means inserting user messages between tool execution steps in your conversation history. When the LLM sees these new messages, it can:
- Adjust its plan based on new information
- Skip unnecessary tool calls
- Use provided data instead of calling tools
- Pivot to a different approach entirely
The key is maintaining proper conversation state (lesson 566) and ensuring your continuation logic (lesson 569) checks for user messages before blindly executing the next planned tool.
Pattern in Practice
After each tool execution, check if the user has responded. If they have, send the updated conversation history (including their new input) back to the LLM rather than continuing with the original plan. The LLM will re-evaluate and either use the new information or call different tools.
Key Takeaway: Interleaving user input lets your AI agent remain responsive and adaptive by incorporating new user messages between tool executions, rather than rigidly following a predetermined plan.