Appendix A — Python Setup

This appendix explains how you can set up Python and all required libraries so that you can run the code examples from this book on your own machine.

The goal is that you can read a listing, run it, change it, and see what changes — using the same environment the examples were written and executed in, so that the output you get matches the output printed in the text.

We assume basic familiarity with the command line (Terminal on macOS / Linux, PowerShell on Windows).

A.1 What You Need

To follow along, you should have:

  • A computer with Windows 10+, macOS, or a recent Linux distribution
  • Python 3.11 or 3.12
  • The uv tool for managing environments and dependencies
  • A text editor or IDE (VS Code, PyCharm, etc.)

A.1.1 Check Your Python Version

python --version

If the output shows a version below 3.11, please install a newer Python release from the official Python website or via your system’s package manager.

A.1.2 Install uv

Follow the instructions on the uv website for your platform to install the uv command. After installation, verify:

uv --version

If this prints a version number without an error, uv is ready.

A.2 Getting the Example Code

All code used in this book is available in a public Git repository at https://github.com/ai-agents-book/code.

git clone https://github.com/ai-agents-book/code
cd code

In the remainder of this appendix, we assume that your current working directory is this project folder.

A.3 Project Configuration with uv (pyproject.toml)

The dependencies for all examples are defined in a standard pyproject.toml file, which is understood by uv.

[project]
name = "agentic-ai"
version = "0.1.0"
description = "Python environment to execute code from 'AI Agents' by Michael Bücker and Michael Hewing"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
    "dotenv>=0.9.9",
    "fastapi>=0.139.0",
    "fastmcp>=2.13.0",
    "jupyter>=1.1.1",
    "langgraph>=1.2.7",
    "matplotlib>=3.10.7",
    "nltk>=3.9.2",
    "numpy>=2.3.5",
    "openai>=2.9.0",
    "pandas>=3.0.3",
    "pip>=25.3",
    "pydantic>=2.12.5",
    "requests>=2.32.5",
    "scikit-learn>=1.8.0",
    "spacy>=3.8.11",
    "sqlalchemy>=2.0.51",
    "textblob>=0.19.0",
    "torch>=2.12.1",
    "transformers>=4.57.3",
]

Most of these libraries support a single chapter’s examples: fastmcp implements the Model Context Protocol server and client used in the tools chapter, langgraph supports the orchestration examples, and pandas, numpy, and sqlalchemy back the data-handling code in later chapters. The important part is that you do not have to install packages manually, uv uses this file as the single source of truth, and any future addition to it is picked up the next time you run uv sync.

A.4 Creating and Using the Python Environment with uv

With pyproject.toml in place, the easiest way to get a matching Python environment is:

# Create (or update) a virtual environment based on pyproject.toml
uv sync

# Optionally: activate the virtual environment (recommended)
source .venv/bin/activate        # macOS / Linux
# .venv\Scripts\activate         # Windows (PowerShell)

What this does:

  • uv sync creates a virtual environment (by default in .venv/) and installs all required packages according to pyproject.toml.
  • Activating .venv ensures that when you run python, the correct interpreter and libraries are used.

From now on, you should see something like (.venv) at the beginning of your shell prompt.

You can confirm that you are using the right Python:

which python      # macOS / Linux
# or
where python      # Windows

The path should point to the .venv directory.

A.5 Optional: Jupyter for Interactive Exploration

jupyter is already listed in dependencies, so uv sync installs it along with everything else. Start it with:

uv run jupyter lab

or:

uv run jupyter notebook

This will run Jupyter using the same environment that uv sync created. You can then open the notebooks or create new ones to experiment with the code from the book.

A.6 API Keys and Environment Variables

The LLM examples throughout this book call a chat completions API through the standard openai Python package. This client interface is not tied to a single vendor: the same method calls work against OpenAI directly, against a self-hosted model server, and against any other provider implementing the OpenAI-compatible API standard, so you are not restricted to the exact setup used while writing the book. The code as printed constructs the client with no arguments and takes its configuration from the environment, so the same listings address whichever endpoint you point them at. The web-search example in the tools chapter additionally calls Serper.dev. You must provide your own credentials for whichever provider you use.

We never hard-code keys into the code. Instead, we read them from environment variables, typically loaded from a local .env file.

A.6.1 Step 1: Create a .env File

In the project root folder, create a .env file and insert your own credentials. To run the examples exactly as printed:

OPENAI_API_KEY="..."
OPENAI_BASE_URL="..."     # omit when calling OpenAI itself
CHAT_MODEL="..."
EMBED_MODEL="..."
SERPER_API_KEY="..."

The listings deliberately carry no default model, so the book never pins a choice that providers retire on their own schedule. Set CHAT_MODEL and EMBED_MODEL to identifiers your provider actually serves; naming schemes differ from one provider to the next even for the same underlying model. OPENAI_BASE_URL is needed only when you call something other than OpenAI itself, such as a self-hosted model server or a gateway placed in front of one.

NoteWhich model produced the printed outputs

The outputs shown throughout this book were generated with GPT-4o. A different model will word its answers differently and may make different tool-calling choices; what should carry over is the structure of each result, not its exact phrasing. Where an example depends on a specific capability rather than on wording, the text says so.

The .env file stays on your machine and is ignored by Git, so your credentials are not accidentally published.

A.6.2 Step 2: Loading Environment Variables in Python

The examples use the python-dotenv package to load environment variables. This is the client construction exactly as the chapters print it:

import os
from dotenv import load_dotenv
from openai import OpenAI

# Load variables from the .env file into the environment
load_dotenv()

client = OpenAI()

CHAT_MODEL = os.environ["CHAT_MODEL"]
EMBED_MODEL = os.environ["EMBED_MODEL"]

OpenAI() takes no arguments here because it reads OPENAI_API_KEY from the environment on its own, and OPENAI_BASE_URL whenever that is set. The same three lines therefore reach OpenAI itself, a self-hosted model server, or a gateway in front of either, without any change to the code. The core idea holds regardless of which provider a given example calls: configuration via environment variables, not via hard-coded strings.

A.6.3 Using Azure OpenAI

Organizations frequently permit Azure OpenAI and nothing else. Azure addresses deployments rather than models and pins an API version, so it needs a different client class — but everything past client construction (tool schemas, message handling, the tool-calling loop) stays identical, because it relies only on the standard chat completions interface.

Replace the client construction above with:

import os
from dotenv import load_dotenv
from openai import AzureOpenAI

load_dotenv()

client = AzureOpenAI(
    azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),
    api_version="<current-api-version>",
)

CHAT_MODEL = os.environ["CHAT_MODEL"]
EMBED_MODEL = os.environ["EMBED_MODEL"]

and add the corresponding variables to your .env:

AZURE_OPENAI_ENDPOINT="https://<your-resource>.openai.azure.com/"
AZURE_OPENAI_API_KEY="..."

Here CHAT_MODEL and EMBED_MODEL hold deployment names, which you choose when creating the deployment and which need not match the underlying model’s name. The API version is account-specific and changes on the provider’s schedule, which is why it appears as a placeholder rather than a fixed value.

A.6.4 Alternative: Setting Environment Variables in the Shell

If you prefer not to use a .env file, you can export the variables directly in your shell session before running a script:

export OPENAI_API_KEY="..."
export CHAT_MODEL="..."
export EMBED_MODEL="..."

python examples/01_first_agent.py

On Windows PowerShell, use:

$env:OPENAI_API_KEY = "..."
$env:CHAT_MODEL = "..."
$env:EMBED_MODEL = "..."
python examples/01_first_agent.py

A.7 Running an Example

Once your environment is set up (uv sync run, .venv activated, .env configured), you can run any example script, for instance:

python examples/01_first_agent.py

If the script runs without import errors and the API calls succeed, your setup matches the one used for this book and you are ready to explore the other chapters.