LangChain 2.0 Tutorial: Build an AI Agent with Tools (2025 Edition)
LangChain 2.0 Tutorial: Build an AI Agent with Tools (2025 Edition)
AI applications are moving from simple chatbots to smart agents that can think, plan, and use external tools. In 2025, LangChain 2.0 is one of the most popular frameworks to build these intelligent agents in Python.
In this tutorial, you will learn how to create a real AI agent using LangChain 2.0 with tool calling support — step by step.
What’s New in LangChain 2.0?
LangChain 2.0 makes it easier and faster to build AI-powered workflows and agents. Some key improvements include:
- Better performance and more modular design.
- Improved tool calling with structured inputs and outputs.
- Support for modern LLMs like GPT-5, o1, Claude, Gemini, etc.
- Updated agent architecture (ReAct + function calling style).
- Stronger memory support for multi-turn conversations.
Agents built with LangChain 2.0 can now search the web, call APIs, run Python functions, read files, and combine all these tools to solve complex tasks.
1. Install LangChain 2.0 and Dependencies
First, create a virtual environment (recommended), then install the core packages:
pip install langchain langchain-openai python-dotenv
You will also need an OpenAI API key (or another LLM provider) set in your environment, for example using a .env file.
2. Load Your LLM (OpenAI Example)
Create a file named agent.py and start by loading the model:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-5.1") # use the model you have access to
This wraps the OpenAI chat model so that LangChain can talk to it easily.
3. Define Tools for the Agent
Tools are simple Python functions that the agent can call when it needs them. For example, let’s build two math tools:
from langchain.tools import tool
@tool
def add_numbers(data: dict) -> int:
"""Add two numbers a and b."""
return data["a"] + data["b"]
@tool
def multiply_numbers(data: dict) -> int:
"""Multiply two numbers x and y."""
return data["x"] * data["y"]
Each tool gets a dictionary as input, and returns a result. The docstring helps the LLM understand when to use which tool.
4. Create the AI Agent
Now we combine the LLM and the tools into a reactive agent:
from langchain.agents import create_react_agent
tools = [add_numbers, multiply_numbers]
agent = create_react_agent(
llm=llm,
tools=tools,
)
The agent will decide when to think, which tool to call, and how to combine their outputs to answer the user.
5. Run the Agent with a Natural Language Task
Let's give the agent a small task that requires multiple steps:
query = "Multiply 12 and 8, then add 50 to the result."
result = agent.invoke(query)
print(result)
Internally, the agent will call multiply_numbers, then add_numbers, and finally return the combined answer in natural language.
6. Adding Conversation Memory (Optional)
To make the agent remember previous messages, you can plug in a simple memory module:
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
agent_with_memory = create_react_agent(
llm=llm,
tools=tools,
memory=memory,
)
Now the agent can use past conversation context to answer follow-up questions more intelligently.
7. What Can You Build with LangChain 2.0 Agents?
- AI chatbots that call tools and APIs.
- Autonomous research agents that search, read, and summarize.
- Data analysis assistants that run Python code or query databases.
- Workflow automation bots that chain multiple actions.
- Personal AI assistants that remember context and preferences.
The main idea is simple: instead of a "dumb" chatbot that only replies with text, you get an agent that can actually do things.
Conclusion
LangChain 2.0 makes it much easier to build powerful AI agents in Python. By combining LLMs with tools, memory, and clear workflows, you can create apps that are far more useful than simple chatbots.
Start small: create one or two tools, wire them to an LLM, and experiment. Step by step, you'll be able to build AI agents that solve real problems in automation, data, and everyday work.
Comments
Post a Comment