Tool Selection & Validation
Choosing weapons wisely. Pre-flight checks for AI actions.
1. The Paradox of Choice
If you give an agent 50 tools, it gets confused.
It might pick search_wikipedia when it should have picked search_internal_docs.
Tool Selection is primarily solved by naming and descriptions.
The description is not just documentation; it is the Selection Vector.
"Use this tool for factual questions about history." vs "Use this tool for company policy."
2. Validation: The Safety Net
Just because the LLM selected the tool and generated the arguments doesn't mean you should run it. You need a "Pre-Execution Middleware."
Example: delete_user(user_id=5)
Validation Logic:
- Is the current user an Admin?
- Does user_id 5 exist?
- Is this a protected root account?
If validation fails, do not crash.
Throw a ToolException with a clear message:
"Error: You do not have permission to delete User 5. Please ask the user for authorization first."
The Agent receives this error, understands the constraint, and pivots: "I need your permission to proceed."
3. The Validation Loop
from langchain_core.tools import ToolException
def _delete_user(user_id: int):
if user_id == 1:
raise ToolException("Cannot delete Root user.")
# ... proceed
delete_tool = Tool(
name="delete_user",
func=_delete_user,
handle_tool_error=True # Vital!
)handle_tool_error=True means the crash is caught and fed back to the LLM as observation.
If False, the entire chain crashes.
4. Summary
Tools are dangerous. Validation converts "Dangerous Action" into "Managed Exception." Never trust the Model's output until you verify it.
Key Intuition: "Trust but Verify. Then Verify again."