Building Your First AI Agent: A Step-by-Step Introduction
A beginner-friendly guide to building your first AI agent with Python, covering core concepts like LLMs, tools, and memory, with a practical example using LangChain.

Building an AI agent sounds intimidating, but the core concepts are straightforward and the tools available in 2026 make it surprisingly accessible. If you can write basic Python, you can build an agent. This guide walks you through the fundamental concepts and builds a simple research agent step by step.
The Three Parts of Every Agent
Think of an AI agent like a person doing a task. Every agent has three essential components:
The Brain: A Large Language Model
The LLM is the thinking engine. It reads instructions, reasons about what to do next, and generates responses. When your agent needs to decide "should I search the web or write the summary now?" the LLM makes that decision.
You can use any LLM as the brain: Claude, GPT-5, Gemini, or even a local open-source model. More capable models make better decisions, but smaller models can work fine for simpler agents.
The Hands: Tools
Tools are what give the agent the ability to act. Without tools, an LLM can only generate text. With tools, it can search the web, read files, run code, send emails, query databases, and interact with APIs.
Each tool is a function with a clear description. The LLM reads the description and decides when and how to use each tool. For example, a "web_search" tool might have the description: "Search the web for current information. Input: a search query string. Output: a list of relevant results with titles and snippets."
The Memory: Context
Memory gives the agent awareness of what has happened so far. It includes the conversation history, results from previous tool calls, and any intermediate reasoning. Without memory, the agent would forget what it already searched for and get stuck in loops.
In practice, memory is managed by the agent framework - it automatically tracks the conversation and tool results, feeding them back to the LLM so it can make informed decisions.
Setting Up Your Environment
Before building, you need a few things:
Prerequisites
- Python 3.10 or higher installed on your system.
- An API key from an LLM provider. For this tutorial, we will use Anthropic's Claude (sign up at console.anthropic.com), but you can adapt it for OpenAI or others.
- Basic familiarity with Python - variables, functions, and installing packages.
Installation
Open your terminal and create a new project:
mkdir my-first-agent
cd my-first-agent
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install langchain langchain-anthropic tavily-python
We are installing three packages:
- langchain - the agent framework.
- langchain-anthropic - the connector for Claude models.
- tavily-python - a search API designed for AI agents (free tier available at tavily.com).
Set your API keys as environment variables:
export ANTHROPIC_API_KEY="your-anthropic-key-here"
export TAVILY_API_KEY="your-tavily-key-here"
Building a Research Agent
Let us build an agent that can search the web and summarize findings. You give it a research question, and it autonomously searches for information and produces a summary.
Step 1: Define the Tools
from langchain_community.tools.tavily_search import TavilySearchResults
# Create a search tool that returns up to 5 results
search_tool = TavilySearchResults(
max_results=5,
description="Search the web for current information on any topic. "
"Use this when you need up-to-date facts or data."
)
tools = [search_tool]
The TavilySearchResults tool wraps the Tavily search API. When the agent decides it needs to look something up, it calls this tool with a search query and gets back relevant results.
Step 2: Set Up the LLM
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(
model="claude-sonnet-4-20250514",
temperature=0,
max_tokens=4096
)
We use Claude Sonnet as the brain. Setting temperature=0 makes responses more deterministic and focused, which is good for research tasks.
Step 3: Create the Agent
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
# Define the agent's instructions
prompt = ChatPromptTemplate.from_messages([
("system",
"You are a helpful research assistant. When given a question, "
"search the web for relevant information, then provide a clear, "
"well-organized summary of your findings. Always cite your sources. "
"If the initial search does not provide enough information, "
"search again with different terms."),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])
# Create the agent
agent = create_tool_calling_agent(llm, tools, prompt)
# Wrap it in an executor that handles the agent loop
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True, # Print the agent's thought process
max_iterations=10
)
The AgentExecutor is what makes the agent loop work. It repeatedly asks the LLM what to do next, executes any tool calls, feeds the results back, and continues until the LLM decides it has enough information to give a final answer.
Step 4: Run the Agent
result = agent_executor.invoke({
"input": "What are the latest developments in nuclear fusion energy? "
"What breakthroughs happened in 2025-2026?"
})
print(result["output"])
The Complete Script
Here is the full script in one file:
# research_agent.py
from langchain_anthropic import ChatAnthropic
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
# Set up the search tool
search_tool = TavilySearchResults(
max_results=5,
description="Search the web for current information on any topic."
)
# Set up the LLM
llm = ChatAnthropic(
model="claude-sonnet-4-20250514",
temperature=0,
max_tokens=4096
)
# Define the prompt
prompt = ChatPromptTemplate.from_messages([
("system",
"You are a helpful research assistant. When given a question, "
"search the web for relevant information, then provide a clear, "
"well-organized summary of your findings. Always cite your sources. "
"If the initial search does not provide enough information, "
"search again with different terms."),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])
# Create and run the agent
agent = create_tool_calling_agent(llm, tools=[search_tool], prompt=prompt)
executor = AgentExecutor(agent=agent, tools=[search_tool], verbose=True)
# Ask a research question
result = executor.invoke({
"input": "What are the latest developments in nuclear fusion energy?"
})
print("\n--- Research Summary ---")
print(result["output"])
Run it with:
python research_agent.py
Because we set verbose=True, you will see the agent's reasoning process: it decides to search, reads the results, possibly searches again, and then writes a summary. This transparency is one of the great things about working with agents - you can see exactly how they think.
What Is Happening Under the Hood
When you run this agent, here is the sequence of events:
- Your question goes to the LLM.
- The LLM decides it needs to search the web and generates a tool call.
- The agent executor runs the search and returns the results.
- The LLM reads the search results and decides if it has enough information.
- If not, it searches again with a refined query (loop back to step 2).
- Once satisfied, the LLM generates a final summary.
This loop of "think, act, observe" is the core pattern of all AI agents. More sophisticated agents have more tools, more complex planning, and better memory, but the fundamental loop is the same.
Extending Your Agent
Once the basic agent works, here are natural next steps:
Add more tools. Give your agent a Wikipedia tool, a calculator, a file reader, or a Python code executor. Each new tool expands what the agent can do.
Improve the prompt. Add more specific instructions about output format, source quality, or depth of analysis. The system prompt is where you shape the agent's personality and priorities.
Add memory across sessions. Use LangChain's memory modules to let the agent remember previous conversations, so it builds up knowledge over time.
Try CrewAI for multi-agent systems. Once you are comfortable with a single agent, CrewAI lets you create teams of agents that collaborate - a researcher, a writer, and an editor working together.
Where to Go Next
This tutorial covered the fundamentals, but the agent ecosystem is vast. Here are paths worth exploring:
- LangChain documentation (python.langchain.com) - comprehensive guides for building more complex agents.
- CrewAI (crewai.com) - for multi-agent collaboration patterns.
- LangGraph - for building production-grade agent workflows with explicit state management.
- MCP (Model Context Protocol) - Anthropic's open standard for tool integration, which is becoming the universal way to give agents access to external services.
The most important thing is to start building. The concepts make much more sense once you see them in action. Start with the simple research agent above, then gradually add complexity as you learn what agents can do.