Skip to content

LangChain Agent Tool Error — How to Fix and Prevent This Common Issue

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about LangChain Agent Tool Error. We cover key concepts, practical examples, and best practices.

Your LangChain agent selects the wrong tool or fails to execute a tool call. The tool definition has missing input parameters or the agent cannot parse the tool description. Learn to define robust tools and debug agent routing.

The Problem

You define a tool for your agent but the agent cannot use it:

from langchain.agents import tool

@tool
def search(query: str) -> str:
    """Search the web for information."""
    return f"Results for: {query}"

agent.run("Search for AI news")

The agent either ignores the tool or raises:

AgentExecutionError: Tool search received unexpected input

Step-by-Step Fix

Step 1: Use StructuredTool for typed parameters

from langchain.tools import StructuredTool
from langchain.pydantic_v1 import BaseModel, Field

class SearchInput(BaseModel):
    query: str = Field(description="The search query")
    max_results: int = Field(default=5, description="Number of results")

def search(query: str, max_results: int = 5) -> str:
    return f"Found {max_results} results for: {query}"

tool = StructuredTool(
    name="search",
    description="Search the web for information",
    args_schema=SearchInput,
    func=search
)

Step 2: Create the agent with proper tools

from langchain.agents import create_openai_functions_agent, AgentExecutor

agent = create_openai_functions_agent(llm, [tool], prompt)
agent_executor = AgentExecutor(agent=agent, tools=[tool], verbose=True)
result = agent_executor.invoke({"input": "Search for AI news"})

Step 3: Debug tool selection

Set verbose=True to see which tools the agent selects and why.

Prevention Tips

  • Use StructuredTool for tools with multiple parameters
  • Write clear, detailed tool descriptions the agent can parse
  • Set return_direct for tools that return final answers
  • Test each tool in isolation before adding to an agent
  • Limit the number of tools per agent to avoid confusion

Advanced Troubleshooting

Check the Logs

Most LangChain errors are logged to stdout or a dedicated log file. Check your logs first:

# Check system logs
journalctl -u langchain --since "1 hour ago"

# Or check the application log
tail -50 ~/.langchain/logs/error.log

Test with a Minimal Example

Create the simplest possible langchain configuration to verify the base setup works:

langchain --version
langchain --help

If the minimal test passes, add configuration options one at a time until you find the breaking change.

Common Configuration Mistakes

  • Using the wrong file path or URL in configuration
  • Forgetting to restart LangChain after changing config files
  • Mixing tabs and spaces in YAML configuration files
  • Setting incorrect permissions on configuration directories

When to Reinstall

If none of the above resolves the issue, consider a clean reinstall:

# Backup your configuration
cp -r ~/.langchain ~/.langchain.bak

# Remove and reinstall
# Follow the official LangChain installation guide

This ensures you start from a known good state and can isolate the issue.

Common Mistakes with agent tool

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world LANGCHAIN code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does my agent ignore the tools I defined?

Tool descriptions must clearly describe what the tool does and when to use it. The agent uses descriptions to decide which tool to call.

How many tools should I give an agent?

Start with 2-3 tools. Too many tools confuse the agent and slow down execution. You can nest tools or use toolkits for complex scenarios.

What is the difference between tool and StructuredTool?

tool is a simple decorator for single-parameter tools. StructuredTool supports multiple typed parameters with Pydantic schemas.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro