A basic LLM can generate text, but an AI agent often needs to do more than write an answer. It may need to calculate a price, look up product information, check a database, call an API, or run a small business rule. LangChain tools make this possible by exposing Python functions to a chat model in a structured way.
In this tutorial, you will learn what LangChain tools are, why tool calling matters for AI agents, and how to define a simple custom tool in Python. Then we will connect that tool to a chat model so the model can decide when to call it.
🛠️
Tools are what let an AI agent take action. Instead of only producing text, the model can request a function call with structured arguments.
💡 What are LangChain tools?
A LangChain tool is a callable function with a name, description, and input schema. The description helps the model understand when the tool should be used. The input schema helps the model provide the right arguments.
You can think of a normal Python function as something your code calls directly. A LangChain tool is a Python function prepared so a model can request it during a conversation.
Concept
What it means
Beginner example
Python function
Code your program can call directly
calculate_discount(100, 20)
LangChain tool
A function described for model tool calling
A discount calculator tool
Tool calling
The model asks to use a tool with arguments
Call the calculator with price and discount
🧠 Why tools matter for AI agents
Tools make AI agents more useful because they connect language understanding with real operations. A model can understand the user request, choose the right tool, provide structured inputs, and use the tool result to continue the conversation.
User request -> Model decides tool call -> Python tool runs -> Tool result returns -> Model answers
✅
Tools are powerful, so keep them narrow and safe. A beginner tool should do one clear task with predictable inputs and outputs.
✅ Prerequisites
Before getting started, make sure you have:
☑️ Python installed ☑️ Basic Python knowledge ☑️ An OpenAI API key or another tool-calling chat model provider ☑️ A terminal or command prompt
⚙️1️⃣ Create a project folder
Create a new local project folder for this tutorial:
mkdir langchain-tools-agent
cd langchain-tools-agent
🐍2️⃣ Create a virtual environment
Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate
On Windows, activate it with:
.venv\Scripts\activate
📦3️⃣ Install libraries
Install the packages we need for tools and the chat model integration:
pip install -U langchain langchain-openai
🔐4️⃣ Set your API key
Set your OpenAI API key as an environment variable. Replace the placeholder with your own key and never commit real secrets to a repository.
export OPENAI_API_KEY="your_api_key_here"
On Windows PowerShell, use:
$env:OPENAI_API_KEY="your_api_key_here"
🔐
Use placeholders in tutorials and environment variables in real projects. Do not write private API keys directly into Python files.
🛠️5️⃣ Define your first LangChain tool
Create a file named define_tool.py. In this public example, we define a small discount calculator tool and call it directly so you can understand the tool object before adding a model.
from langchain.tools import tool
@tool
def calculate_discount(price: float, discount_percent: float) -> str:
"""Calculate the final price after a percentage discount."""
if price < 0:
return "Price must be greater than or equal to 0."
if discount_percent < 0 or discount_percent > 100:
return "Discount percent must be between 0 and 100."
final_price = price * (1 - discount_percent / 100)
return f"Final price: ${final_price:.2f}"
print(calculate_discount.name)
print(calculate_discount.description)
print(calculate_discount.invoke({"price": 120, "discount_percent": 25}))
define_tool.py
Run the script:
python define_tool.py
You should see the tool name, description, and the calculated result:
calculate_discount
Calculate the final price after a percentage discount.
Final price: $90.00
🎓
Want the model-connected workflow? In the Academy section, we bind this tool to a chat model and inspect the tool call the model requests.
You can view this post with the tier:
Academy Membership
Join academy now to read the post and get access to the full library of premium posts for academy members only.