A hands-on implementation demonstrating how to create, bind, and execute custom tools using LangChain and OpenAI LLMs.
This repository walks through the complete lifecycle of LangChain tools—from creating your first tool to building an intelligent AI agent capable of automatically selecting and executing multiple tools.
Large Language Models (LLMs) are excellent at reasoning and generating text, but they cannot perform external operations such as:
- Mathematical calculations
- Calling APIs
- Accessing databases
- Fetching live information
- Executing Python functions
LangChain solves this problem using Tools.
A Tool is simply a Python function with metadata that an LLM can understand and invoke whenever required.
This project demonstrates:
- Creating custom tools
- Binding tools to an LLM
- Automatic tool calling
- Manual tool execution
- Passing outputs between tools
- Injecting hidden arguments
- Building an intelligent ReAct Agent
.
│── tools_in_langchain.ipynb
│── README.md
The notebook contains multiple examples arranged from beginner to advanced concepts.
- Create custom LangChain tools
- Tool metadata generation
- Bind tools with OpenAI models
- Automatic function calling
- Manual execution of tool calls
- Tool chaining
- Injected tool arguments
- Currency conversion API integration
- ReAct Agent implementation
- Beginner-friendly explanations
- Python
- LangChain
- LangChain OpenAI
- OpenAI GPT Models
- Requests
- ExchangeRate API
Clone the repository
git clone https://github.com/Swizknife/tools-in-langchain.git
cd tools-in-langchainInstall dependencies
pip install langchain-openai
pip install langchain-core
pip install langchain
pip install requestsor
pip install -r requirements.txtCreate an environment variable for your OpenAI API Key.
import os
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"The notebook is divided into several sections.
A simple multiplication function is converted into a LangChain Tool using the @tool decorator.
@tool
def multiply(a: int, b: int):
return a * bLangChain automatically generates:
- Tool name
- Description
- Input schema
- Validation
You also learn how to inspect
multiply.name
multiply.description
multiply.argsInstead of only chatting,
llm.invoke(...)the tool is attached to the model
llm.bind_tools([multiply])Now the LLM can decide whether a tool is needed to answer a user's question.
Example:
Multiply 3 by 1000
Instead of generating the answer directly,
the model returns a Tool Call.
AIMessage(
tool_calls=[
{
"name":"multiply",
"args":{"a":3,"b":1000}
}
]
)This is one of LangChain's core concepts.
The LLM does not execute the function.
It only decides which tool should be executed.
The notebook demonstrates how to manually execute the returned tool call.
tool_result = multiply.invoke(...)The result is then appended back into the conversation.
Human Message
↓
AI Tool Call
↓
Python Function
↓
Tool Result
↓
Final AI Response
This illustrates the complete tool execution loop.
The notebook introduces a more realistic use case.
A custom tool fetches live currency exchange rates from an external API.
get_conversion_factor()The tool makes an HTTP request and returns
- Base Currency
- Target Currency
- Conversion Rate
This demonstrates how LangChain tools can integrate with external services.
One of the most interesting concepts demonstrated is
InjectedToolArgThe conversion tool requires a conversion rate.
However,
the user should never provide this value.
Instead,
the value is automatically injected from another tool.
convert(
base_currency_value,
conversion_rate
)The conversion rate is hidden from the LLM and supplied programmatically.
This is useful for
- API keys
- Database connections
- Authentication tokens
- Hidden intermediate values
- Internal application state
The notebook manually chains two tools together.
Workflow:
User Question
↓
Get Exchange Rate
↓
Extract Conversion Rate
↓
Inject Rate
↓
Convert Currency
↓
Final Answer
This demonstrates how multiple tools can work together to solve a complex task.
Finally, the notebook creates a LangChain Agent.
initialize_agent(...)using
STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION
Unlike manual execution,
the Agent automatically
- decides which tools to use
- determines their order
- executes them
- returns the final response
without requiring manual orchestration.
User Query
│
▼
OpenAI Language Model
│
┌───────────┴────────────┐
│ │
Needs Tool? Normal Response
│
▼
Select Appropriate Tool
│
▼
Execute Python Function
│
▼
Tool Output
│
▼
Final AI Response
- LangChain Tools
- Tool Decorators
- Tool Metadata
- Tool Binding
- Function Calling
- AIMessage
- HumanMessage
- ToolMessage
- Tool Execution
- InjectedToolArg
- Tool Chaining
- External APIs
- ReAct Agent
- Structured Chat Agent
Multiply 5 and 20
Multiply 100 and 400
Convert 50 USD to INR
What is the conversion factor between INR and USD?
Convert 1000 INR to USD
After completing this notebook, you'll understand how to:
- Build custom LangChain tools
- Bind tools to LLMs
- Inspect tool metadata
- Handle tool calls manually
- Execute tools programmatically
- Pass outputs between tools
- Hide internal arguments using InjectedToolArg
- Integrate external REST APIs
- Create autonomous LangChain Agents
- Build AI applications capable of interacting with real-world systems
Some possible extensions include:
- Weather Tool
- Calculator Tool
- Wikipedia Search Tool
- SQL Database Tool
- Gmail Tool
- Google Calendar Tool
- File Reader Tool
- PDF Question Answering Tool
- Multi-tool AI Assistant
- LangGraph Agent implementation
- LangChain Documentation
- OpenAI API
- ExchangeRate API
Soumya Sharma
Electronics & Communication Engineering
Birla Institute of Technology, Mesra
Interested in:
- Generative AI
- Large Language Models
- AI Agents
- LangChain
- Machine Learning
- Deep Learning