Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/LangChain Mastery/Part 8 · 7. Tools & Agents
Lesson 55 of 68

Tool Selection & Validation

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

Derpx0610 Jan · 1 min read
LangChain Mastery
Designing Tool SchemasFunction Calling in LangChainTool Selection & ValidationReAct AgentsPlan-and-Solve AgentsFallbacks & Error HandlingLangGraph: Stateful AgentsHuman-in-the-Loop
Contents
1. The Paradox of Choice2. Validation: The Safety Net3. The Validation Loop4. Summary
0% read · 1 min leftBack to top ↑
Course outline & contents0%
LangChain Mastery
Designing Tool SchemasFunction Calling in LangChainTool Selection & ValidationReAct AgentsPlan-and-Solve AgentsFallbacks & Error HandlingLangGraph: Stateful AgentsHuman-in-the-Loop
Contents
1. The Paradox of Choice2. Validation: The Safety Net3. The Validation Loop4. Summary

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:

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

3. The Validation Loop

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.

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

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