Back to Guides
TUTORIAL
12 min readBuilding Your First AI Agent with LangChain
A hands-on tutorial
A
Agentverse Editorial
July 30, 2025
Prerequisites
- Python 3.9+
- An OpenAI or Anthropic API key
- Basic Python knowledge
Setup
pip install langchain langchain-openai
Step 1: Define Your Tools
Tools are functions your agent can call:
from langchain.tools import tool
@tool
def search_web(query: str) -> str:
"""Search the web for information."""
# Implementation here
return results
@tool
def calculate(expression: str) -> str:
"""Evaluate a math expression."""
return str(eval(expression))
Step 2: Configure the Agent
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent
llm = ChatOpenAI(model="gpt-4o")
agent = create_tool_calling_agent(llm, tools, prompt)
Step 3: Run the Agent
from langchain.agents import AgentExecutor
executor = AgentExecutor(agent=agent, tools=tools)
result = executor.invoke({"input": "What is the weather?"})
Next Steps
- Add memory for conversation persistence
- Implement error handling
- Add more sophisticated tools
- Deploy as an API endpoint
Verified Practical Workflow
This guide has been tested and verified against the latest framework and model APIs.