Getting Started
Building AI agents requires a solid foundation in Python and working with APIs. Before you write your first prompt or call an LLM, you need to be comfortable with the tools that connect everything together.
Start by setting up a proper Python environment. Use Python 3.10 or later, as you will need modern features like structural pattern matching and improved type hints. Always work inside virtual environments to keep dependencies isolated:
python -m venv .venv
source .venv/bin/activate # macOS/Linux
pip install httpx python-dotenv
Environment variables are critical for managing API keys securely. Never hardcode secrets in your source files. Use a .env file locally and load values with python-dotenv:
import os
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv("OPENWEATHER_API_KEY")
Key Concepts
The two libraries you will use constantly in AI development are httpx and json. The httpx library supports both synchronous and asynchronous HTTP, which matters when your agent needs to call multiple APIs without blocking:
import httpx
import asyncio
async def fetch_weather(city: str) -> dict:
async with httpx.AsyncClient() as client:
response = await client.get(
"https://api.openweathermap.org/data/2.5/weather",
params={"q": city, "appid": os.getenv("API_KEY"), "units": "metric"}
)
response.raise_for_status()
return response.json()
async def main():
# Fetch multiple cities concurrently
cities = ["London", "Tokyo", "New York"]
tasks = [fetch_weather(city) for city in cities]
results = await asyncio.gather(*tasks)
for city, data in zip(cities, results):
print(f"{city}: {data['main']['temp']}C")
asyncio.run(main())
This pattern of concurrent API calls is exactly how agents interact with tools and services. Master it here and it will serve you throughout the entire roadmap.
Hands-On Practice
Focus your practice on these core skills before moving forward:
- JSON parsing: Every LLM API returns JSON. Get comfortable navigating nested dictionaries and handling missing keys with
.get()and default values. - Error handling: APIs fail. Wrap calls in try/except blocks and handle
httpx.HTTPStatusError, timeouts, and malformed responses gracefully. - File I/O: Agents often need to read configurations, write logs, and cache results. Practice reading and writing JSON and text files.
- Type hints: Use type annotations throughout your code. When you start working with tool schemas and function calling, precise types become essential.
The milestone project for this step is building a CLI tool that calls a public API. This exercise brings together every concept above: environment setup, HTTP requests, JSON parsing, error handling, and clean output formatting.