Module 10 Lesson 2: The Agent Loop and Tool Selection
How the Agent decides. Deep dive into the mechanics of tool selection and processing tool outputs.
Inside the Mind: Tool Selection
In Module 9, we defined our tools. In this lesson, we look at how the Agent actually picks them. This process is not a "Lookup table"—it is a Semantic Choice made by the model.
1. The Prompt Injection
When you initialize an agent, LangChain creates a giant "System Prompt" that includes:
- Your instructions.
- A list of all available tools.
- Their names and descriptions.
- Instructions on how to output the JSON for the tool call.
2. Dealing with Hallucinated Tools
Sometimes an agent gets confused and tries to call a tool that doesn't exist (e.g., call_mom()).
- LangChain's fix: The system catches the error and sends a message back to the LLM: "Error: Tool 'call_mom' does not exist. Available tools are: [multiply, search]."
- The LLM then "Realizes" its mistake and picks a valid tool.
3. Visualizing the Selection Flow
graph TD
M[Model] -- "Thinking" --> Choice{Which tool?}
Choice -- "Match Description" --> T1[Tool 1: Statistics]
Choice -- "Match Description" --> T2[Tool 2: Search]
T2 -- "Tool Call" --> Exec[Python Execution]
Exec -- "Data" --> M
4. Forced Tool Use
Sometimes you want the agent to Always use a specific tool (e.g., if you are building a specific DB-interface bot).
- You can set the
tool_choiceparameter to "Force" the agent to call your tool on every request.
5. Engineering Tip: Argument Quality
LLMs are prone to sending the wrong "Data Types" to tools (sending "Five" instead of "5").
- As we learned in Module 9, use Pydantic in your tool definitions to validate these arguments before the code runs. This prevents your server from crashing and gives the agent a clean "Validation Error" to fix.
Key Takeaways
- Prompt descriptions are the only way the agent knows which tool to use.
- The agent is re-invoked after every tool call to process the result.
- Tool hallucination is handled by automated error-correction messages.
- Validation is critical for maintaining the agent loop's stability.