0
0
LangChainframework~10 mins

Creating tools for agents in LangChain - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating tools for agents
Define Tool Function
Wrap Function as Tool
Create Agent with Tools
Agent Receives Input
Agent Selects Tool
Tool Executes Function
Return Tool Output to Agent
Agent Responds to User
This flow shows how you define a tool function, wrap it as a tool, create an agent with it, and how the agent uses the tool to answer user input.
Execution Sample
LangChain
from langchain.agents import Tool, initialize_agent
from langchain.llms import OpenAI

def add_numbers(a, b):
    return a + b

add_tool = Tool(name="Adder", func=add_numbers, description="Adds two numbers")
Defines a simple add function, wraps it as a LangChain tool named 'Adder'.
Execution Table
StepActionInputTool SelectedFunction CalledOutput
1Agent receives input"Add 3 and 5"Adderadd_numbers(3, 5)8
2Agent returns outputN/AN/AN/A"The sum is 8"
3Agent receives input"Add 10 and 20"Adderadd_numbers(10, 20)30
4Agent returns outputN/AN/AN/A"The sum is 30"
5Agent receives input"Multiply 2 and 3"No matching toolN/AN/A
6Agent returns outputN/AN/AN/A"Sorry, I can't do that."
💡 Agent stops when no matching tool is found or after responding to user input.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
input_textNone"Add 3 and 5""Add 10 and 20""Multiply 2 and 3""Multiply 2 and 3"
tool_selectedNoneAdderAdderNoneNone
function_outputNone830NoneNone
agent_responseNone"The sum is 8""The sum is 30""Sorry, I can't do that.""Sorry, I can't do that."
Key Moments - 2 Insights
Why does the agent say it can't do something when I ask to multiply?
Because the agent only has the 'Adder' tool registered, it cannot find a tool to handle multiplication. See execution_table row 5 where no matching tool is found.
How does the agent know which tool to use?
The agent matches the user input to tool descriptions or names. If the input mentions adding, it selects the 'Adder' tool as shown in execution_table rows 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the output of the tool function?
A30
B5
C8
DNone
💡 Hint
Check the 'Output' column in execution_table row 3.
At which step does the agent fail to find a matching tool?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Look for 'No matching tool' in the 'Tool Selected' column.
If you add a multiplication tool, how would the agent respond to 'Multiply 2 and 3'?
AIt would still say it can't do that.
BIt would call the add tool by mistake.
CIt would call the multiplication tool and return 6.
DIt would crash.
💡 Hint
Think about how the agent selects tools based on input matching.
Concept Snapshot
Creating tools for agents:
- Define a Python function for the tool's task.
- Wrap it with langchain's Tool(name, func, description).
- Create an agent with these tools.
- Agent matches user input to tool descriptions.
- Calls the tool function and returns output.
- If no tool matches, agent responds it can't help.
Full Transcript
This visual execution shows how to create tools for agents using LangChain. First, you define a function that does a task, like adding numbers. Then you wrap it as a Tool with a name and description. Next, you create an agent that knows about this tool. When the agent gets user input, it tries to find a tool that matches the request. If it finds one, it calls the tool's function with the right inputs and returns the result. If no tool matches, it tells the user it can't help. The execution table traces inputs, tool selection, function calls, and outputs step-by-step. The variable tracker shows how inputs and outputs change over time. Key moments clarify why the agent might say it can't do something and how it picks tools. The quiz tests understanding of these steps.