Getting Started with Agents (Python)
Build your first AI agent with Python. Learn agent fundamentals, set up your environment, and write a working agent with tools.
Prerequisites
- 1Basic Python knowledge
- 2Python 3.10+ installed
- 3An API key from OpenAI or Anthropic
What you will learn
- What an AI agent is and how it differs from a chatbot
- The core agent loop: Perceive, Reason, Act
- How to set up a Python project for agent development
- How to build a hello-world agent and add tools
What Is an AI Agent?
An AI agent is a software system that uses a large language model (LLM) as its reasoning engine to autonomously perceive its environment, make decisions, and take actions to achieve a goal. Unlike a simple chatbot that responds to a single prompt, an agent operates in a loop — it observes, thinks, acts, and then observes again until the task is done.
Here is the simplest mental model:
while not done:
observation = perceive(environment)
thought = reason(observation, memory)
action = decide(thought, available_tools)
result = execute(action)
memory.update(result)
This loop is the beating heart of every agent framework. Whether you use LangGraph, CrewAI, or the OpenAI Agents SDK, the underlying principle is identical.
Agents vs Chatbots
It is important to understand the distinction between a chatbot and an agent:
| Feature | Chatbot | Agent |
|---|---|---|
| Interaction | Single turn or multi-turn conversation | Autonomous multi-step execution |
| Tool Use | Rarely uses external tools | Actively calls APIs, databases, code interpreters |
| Memory | Limited to context window | Can persist state across sessions |
| Goal-Directed | Responds to user messages | Works toward completing a defined objective |
| Autonomy | Low — waits for user input | High — decides its own next steps |
An agent can call tools, search the web, write and execute code, read files, and make decisions about what to do next without waiting for human input at every step.
Choosing a Framework
For your first Python agent, we recommend starting with one of these beginner-friendly options:
- OpenAI Agents SDK — Best if you are already using OpenAI models. Minimal setup, well-documented.
- Anthropic SDK — Deep MCP integration, strong safety features. Build agent loops with the Messages API.
- Smolagents (Hugging Face) — Ultra-minimal, perfect for learning. Your agent writes and executes Python code to achieve goals.
For a detailed comparison, see the Choosing Your Stack guide.
Installing Dependencies
Set up a Python project with the OpenAI Agents SDK. You will need Python 3.10 or later.
# Create a project directory
mkdir my-first-agent && cd my-first-agent
# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install the OpenAI Agents SDK
pip install openai-agents
# Set your API key
export OPENAI_API_KEY="sk-..."
Alternatively, if you prefer Anthropic:
pip install anthropic
export ANTHROPIC_API_KEY="sk-ant-..."
Hello World Agent
Here is a minimal agent using the OpenAI Agents SDK. It creates an agent with a system prompt and runs it with a single task:
from agents import Agent, Runner
agent = Agent(
name="Greeter",
instructions="You are a helpful assistant. Answer concisely.",
)
result = Runner.run_sync(
agent,
"What are the three laws of robotics?"
)
print(result.final_output)
Run it:
python agent.py
You should see the agent respond with Asimov's three laws. Congratulations — you have just built your first AI agent!
The key insight is that Runner.run_sync manages the agent loop for you. Under the hood it sends the prompt to the model, collects the response, checks if any tool calls are needed, executes them, and loops until the agent signals it is done.
Adding a Tool
Agents become powerful when they can use tools. Let us give our agent a simple calculator:
from agents import Agent, Runner, function_tool
@function_tool
def calculate(expression: str) -> str:
"""Evaluate a math expression and return the result."""
try:
return str(eval(expression))
except Exception as e:
return f"Error: {e}"
agent = Agent(
name="MathAgent",
instructions="You are a helpful math assistant. Use the calculate tool for arithmetic.",
tools=[calculate],
)
result = Runner.run_sync(agent, "What is 247 * 38 + 19?")
print(result.final_output)
When the agent encounters an arithmetic question, it will call the calculate tool rather than attempting mental math. This is the essence of tool-augmented generation.
Security note: The eval() function is used here for simplicity. Never use eval() with untrusted input in production — it can execute arbitrary code. For production math evaluation, use a safe library like simpleeval.
Next Steps
You now understand the agent loop and have a working Python agent with tool use. From here, you can:
- Speed-run a more complete agent in the Your First Agent in 5 Minutes guide
- Learn how to pick the right framework in Choosing Your Stack
- Deep dive into LangGraph, CrewAI, or PydanticAI
- Explore the Frameworks catalog to compare all Python options
Common Mistakes to Avoid
- !Forgetting to set the API key environment variable before running the agent
- !Giving the agent too broad of instructions — be specific about what it should and should not do
- !Not handling tool errors — always return meaningful error messages from tools
- !Using Python older than 3.10 which lacks required syntax features