Learn/LangChain Mastery/Part 8 · 7. Tools & Agents
Lesson 55 of 68
SyllabusLangChain Mastery · Part 8

Tool Selection & Validation

Choosing weapons wisely. Pre-flight checks for AI actions.

Course outline & contents
LangChain Mastery
Designing Tool SchemasFunction Calling in LangChainTool Selection & ValidationReAct AgentsPlan-and-Solve AgentsFallbacks & Error HandlingLangGraph: Stateful AgentsHuman-in-the-Loop

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."

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:

  1. Is the current user an Admin?
  2. Does user_id 5 exist?
  3. 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."

validation_error.py
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.

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."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonFunction Calling in LangChainNext lesson →ReAct Agents