Learn to build powerful AI agents for specific tasks
A step-by-step guide to preparing your development environment for AI agent creation
Setting up a proper development environment is crucial for efficient AI agent development. This guide will walk you through installing the necessary tools, setting up API access, and configuring your workspace for building AI agents with popular frameworks.
This guide assumes you have basic familiarity with:
Most AI agent frameworks require Python 3.9 or newer. Here's how to install Python on different operating systems:
python --version
Alternatively, you can install Python using Windows Package Manager:
winget install Python.Python.3.11
Option 1: Using Homebrew (recommended):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install python
Option 2: Using the installer:
Verify installation by opening Terminal and typing:
python3 --version
For Ubuntu/Debian:
sudo apt update
sudo apt install python3 python3-pip python3-venv
For Fedora:
sudo dnf install python3 python3-pip
For Arch Linux:
sudo pacman -S python python-pip
Verify installation:
python3 --version
Virtual environments keep your project dependencies isolated, preventing conflicts with other Python projects.
mkdir ai-agent-project
cd ai-agent-project
python -m venv venv
source venv\Scripts\activate
You should see (venv)
at the beginning of your command prompt, indicating
that the virtual environment is active.
mkdir ai-agent-project
cd ai-agent-project
python3 -m venv venv
source venv/bin/activate
You should see (venv)
at the beginning of your command prompt, indicating
that the virtual environment is active.
To deactivate the virtual environment when you're done working on your project, simply run the
deactivate
command.
Different AI agent frameworks have their own dependencies. Here's how to install the most popular ones:
Make sure your virtual environment is activated before installing packages.
pip install langchain langchain-openai
For additional functionality:
pip install langchain[all]
Common integrations:
pip install langchain-anthropic langchain-community langchain-experimental chroma chromadb
pip install pyautogen
For enhanced functionality:
pip install pyautogen[retrievers,blendsearch]
For OpenAI integrations:
pip install openai
pip install llama-index llama-index-llms-openai
For additional integrations:
pip install llama-index-llms-anthropic llama-index-embeddings-openai llama-index-vector-stores-chroma
pip install crewai crewai_tools
For LLM providers:
pip install openai anthropic
If you want to experiment with multiple frameworks, you can create a requirements.txt
file with the following content:
# Core frameworks
langchain>=0.1.0
langchain-openai>=0.0.2
langchain-anthropic>=0.1.1
langchain-community>=0.0.16
pyautogen>=0.2.0
llama-index>=0.9.0
crewai>=0.28.0
# LLM providers
openai>=1.0.0
anthropic>=0.8.0
# Vector stores and embeddings
chromadb>=0.4.18
sentence-transformers>=2.2.2
# Utilities
python-dotenv>=1.0.0
pydantic>=2.5.0
tiktoken>=0.5.0
Then install all dependencies at once:
pip install -r requirements.txt
Keep your dependencies up-to-date, especially for rapidly evolving frameworks:
pip install --upgrade -r requirements.txt
Most AI agent frameworks require access to LLM APIs. Here's how to set up the most common ones:
Similar processes apply for other providers like:
Never hardcode API keys in your source code or commit them to version control. Always use environment variables or secure key management solutions.
Using environment variables is a secure way to manage API keys and configuration.
In your project root, create a file named .env
with your API keys:
# API Keys
OPENAI_API_KEY=sk-your-openai-key-here
ANTHROPIC_API_KEY=sk-ant-your-anthropic-key-here
# Configuration
DEFAULT_LLM_MODEL=gpt-4-turbo
VECTOR_DB_PATH=./data/vectorstore
Use the python-dotenv
package to load environment variables:
# Import at the top of your main script
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
# Access variables in your code
openai_api_key = os.getenv("OPENAI_API_KEY")
anthropic_api_key = os.getenv("ANTHROPIC_API_KEY")
Create a .gitignore
file in your project root to prevent committing sensitive
information:
# Environment variables
.env
# Virtual environment
venv/
# Python cache
__pycache__/
*.py[cod]
*$py.class
# Distribution / packaging
dist/
build/
*.egg-info/
# Vector stores and local data
data/
A well-organized project structure makes development easier. Here's a recommended structure for AI agent projects:
ai-agent-project/
├── .env # Environment variables
├── .gitignore # Git ignore file
├── README.md # Project documentation
├── requirements.txt # Dependencies
├── main.py # Entry point
├── agents/ # Agent definitions
│ ├── __init__.py
│ ├── base_agent.py
│ └── specialized_agents.py
├── tools/ # Custom tools
│ ├── __init__.py
│ └── tool_definitions.py
├── prompts/ # Prompt templates
│ ├── __init__.py
│ └── templates.py
├── memory/ # Agent memory components
│ ├── __init__.py
│ └── memory_handlers.py
├── data/ # Data files and vector stores
│ ├── raw/
│ ├── processed/
│ └── vectorstore/
└── tests/ # Test files
├── __init__.py
└── test_agents.py
Create this structure using the following commands:
mkdir agents tools prompts memory data data\raw data\processed data\vectorstore tests
type nul > .env
type nul > .gitignore
type nul > README.md
type nul > requirements.txt
type nul > main.py
type nul > agents\__init__.py
type nul > agents\base_agent.py
type nul > agents\specialized_agents.py
type nul > tools\__init__.py
type nul > tools\tool_definitions.py
type nul > prompts\__init__.py
type nul > prompts\templates.py
type nul > memory\__init__.py
type nul > memory\memory_handlers.py
type nul > tests\__init__.py
type nul > tests\test_agents.py
mkdir -p agents tools prompts memory data/raw data/processed data/vectorstore tests
touch .env .gitignore README.md requirements.txt main.py
touch agents/__init__.py agents/base_agent.py agents/specialized_agents.py
touch tools/__init__.py tools/tool_definitions.py
touch prompts/__init__.py prompts/templates.py
touch memory/__init__.py memory/memory_handlers.py
touch tests/__init__.py tests/test_agents.py
To verify your setup, create a simple agent template. Here's an example using LangChain:
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_react_agent
from langchain.prompts import ChatPromptTemplate
from langchain.tools import tool
# Load environment variables
load_dotenv()
# Initialize LLM
llm = ChatOpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
model="gpt-4-turbo"
)
# Define a simple tool
@tool
def calculator(expression: str) -> str:
"""Evaluate a mathematical expression"""
try:
return str(eval(expression))
except Exception as e:
return f"Error: {str(e)}"
# Define tools list
tools = [calculator]
# Create prompt template
prompt = ChatPromptTemplate.from_template(
"""You are a helpful AI assistant that can use tools to answer questions.
{chat_history}
Human: {input}
{agent_scratchpad}
"""
)
# Create agent
agent = create_react_agent(llm, tools, prompt)
# Create agent executor
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
def main():
print("AI Agent initialized! Type 'exit' to quit.")
while True:
user_input = input("\nYou: ")
if user_input.lower() == 'exit':
print("Goodbye!")
break
try:
response = agent_executor.invoke({"input": user_input})
print(f"\nAI: {response['output']}")
except Exception as e:
print(f"\nError: {str(e)}")
if __name__ == "__main__":
main()
Run your agent to verify everything is working correctly:
python main.py
Try asking a question that uses the calculator tool, such as:
You: What is 42 * 3.14?
Setting up version control is recommended for tracking changes and collaborating with others:
git init
git add .gitignore README.md requirements.txt main.py agents/ tools/ prompts/ memory/ tests/
git commit -m "Initial project setup"
Make sure your .env
file is listed in .gitignore
to prevent accidentally committing your API
keys.