tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > LangChain > LangChain Agents with Tools

LangChain Agents with Tools

Author: Venkata Sudhakar

A LangChain agent is an LLM that can decide which tools to use, in what order, to answer a question or complete a task. Unlike a simple chain where the sequence of steps is fixed in code, an agent uses the LLM as a reasoning engine: it looks at the available tools, decides which one to call, observes the result, and repeats until it has enough information to give a final answer. This ReAct (Reasoning + Acting) loop is what makes agents capable of handling open-ended tasks that chains cannot.

Tools are functions that the agent can choose to call. LangChain provides built-in tools for web search (Tavily, DuckDuckGo), Python code execution, Wikipedia, and more. You can also define custom tools using the @tool decorator - any Python function with a clear docstring becomes a tool the agent can discover and use. The docstring is critical: the LLM reads it to understand when and how to use the tool.

The below example shows a LangChain agent equipped with a web search tool and a custom data migration cost calculator tool, using the modern create_react_agent function.


It gives the following output,

Thought: I need to estimate the migration cost. I should use the estimate_migration_cost tool.
Action: estimate_migration_cost
Action Input: {"row_count": 5000000, "complexity": "high"}
Observation: Migration estimate for 5,000,000 rows (high complexity):
  Estimated cost: $100,000
  Estimated duration: 16 weeks
  Recommended approach: CDC-based incremental migration with Debezium
Thought: I now have the complete estimate.
Final Answer: For a 5 million row high complexity migration the estimated cost is $100,000 with a 16 week duration. The recommended approach is CDC-based incremental migration using Debezium.

The below example shows a multi-tool agent that uses both web search and the calculator tool together to answer a complex question.


It gives the following output,

Thought: I need two things: info about Debezium (use search) and a cost estimate (use calculator).
Action: duckduckgo_search
Action Input: what is Debezium CDC tool
Observation: Debezium is an open-source distributed platform for change data capture...

Thought: Now I need the migration cost estimate.
Action: estimate_migration_cost
Action Input: {"row_count": 2000000, "complexity": "medium"}
Observation: Migration estimate for 2,000,000 rows (medium complexity):
  Estimated cost: $10,000
  Estimated duration: 6 weeks

Final Answer: Debezium is an open-source CDC platform that reads database transaction logs
and streams changes to Apache Kafka. For 2 million rows at medium complexity the estimated
cost is $10,000 with a 6-week duration.

When to use agents vs chains:

Use a chain when the steps are known in advance and the workflow does not need to branch. Use an agent when the LLM needs to decide which tools to call, in what order, or how many times, based on the results it observes. Agents are more flexible but also less predictable - always set max_iterations and handle AgentFinish and AgentError exceptions in production to prevent runaway loops.


 
  


  
bl  br