
In this beginner-friendly tutorial, you’ll learn how to create an AI agent using Python’s LangChain
library.
This agent can process questions and return smart answers.
✨ What You’ll Learn
- How to install LangChain
- How to connect it to OpenAI
- How to build a simple agent with tools
🛠️ Step 1: Install LangChain
Open your terminal and run:
pip install langchain openai
🛠️ Step 2: Build the Agent Script
Here is an example Python script you can use:
from langchain_openai import ChatOpenAI
from langchain.agents import create_react_agent
from langchain.tools import Tool
# Initialize the language model
llm = ChatOpenAI(model="gpt-4o-mini")
# Example tool: a simple calculator
tools = [
Tool(
name="Calculator",
func=lambda x: str(eval(x)),
description="Performs basic math operations"
)
]
# Create the agent
agent_executor = create_react_agent(llm=llm, tools=tools)
# Example usage
result = agent_executor.invoke({"input": "What is 25 * 3?"})
print(result)
✅ How It Works
– ChatOpenAI()
connects to your GPT model.
– Tool()
defines an action the agent can take.
– create_react_agent()
combines the model and tools into an agent that processes input and produces output.
– You can expand this by adding more tools or customizing behavior.
🎉 Congratulations!
You’ve built your first AI agent with LangChain! Try changing the input prompt or adding more tools to explore different capabilities.
👉 Ready to Learn More?
Join our AI Agent course and learn to build more advanced assistants.