Mastering the Anthropic Cookbook: A Practical Guide to Building with Claude API
Learn how to set up, develop, and contribute to the Anthropic Cookbook — the official collection of Jupyter notebooks and Python examples for building with the Claude API.
This guide walks you through setting up the Anthropic Cookbook environment, understanding the project structure, following code style and Git conventions, using current Claude models correctly, and contributing your own cookbook notebooks.
Mastering the Anthropic Cookbook: A Practical Guide to Building with Claude API
If you're serious about building applications with Claude, the Anthropic Cookbook is your most valuable resource. It's the official collection of Jupyter notebooks and Python examples that demonstrate how to use the Claude API effectively — from basic capabilities to advanced patterns like tool use, multimodal processing, and extended thinking.
But the cookbook isn't just a static reference. It's a living codebase with a well-defined development workflow, strict quality standards, and clear contribution guidelines. Whether you're using it to learn, to prototype, or to contribute your own examples, understanding how the cookbook works under the hood will save you time and frustration.
This guide covers everything you need to know: setup, project structure, code style, model selection best practices, and how to add your own cookbook.
Getting Started: Environment Setup
Before you can run any cookbook notebooks, you need to set up your development environment. The cookbook uses uv — a fast Python package manager — for dependency management.
Step 1: Install Dependencies
uv sync --all-extras
This command installs all required packages, including Jupyter, the Anthropic SDK, and any third-party integrations (like Pinecone or Voyage AI). The --all-extras flag ensures you get optional dependencies as well.
Step 2: Install Pre-commit Hooks
uv run pre-commit install
Pre-commit hooks automatically check your code for formatting issues, linting errors, and notebook structure problems before you commit. This keeps the repository clean and consistent.
Step 3: Configure Your API Key
cp .env.example .env
Edit .env and add your ANTHROPIC_API_KEY
Critical rule: Never commit your .env file. The cookbook uses python-dotenv to load environment variables. In your code, access the API key like this:
import os
from dotenv import load_dotenv
load_dotenv()
api_key = os.environ["ANTHROPIC_API_KEY"]
Development Commands You'll Use Daily
The cookbook provides make commands for common development tasks. Here's what each does:
| Command | Purpose |
|---|---|
make format | Format all Python files with Ruff |
make lint | Run Ruff linter to catch issues |
make check | Run format-check + lint together |
make fix | Auto-fix lint issues + format |
make test | Run pytest test suite |
uv directly:
uv run ruff format . # Format code
uv run ruff check . # Lint code
uv run ruff check --fix . # Auto-fix issues
uv run pre-commit run --all-files # Run all pre-commit hooks
Pro tip: Run make check before every commit. It catches formatting and linting issues early, saving you from CI failures.
Code Style: The Ruff Standard
The cookbook enforces a consistent code style using Ruff, a fast Python linter and formatter. Key rules:
- Line length: 100 characters (not the standard 88)
- Quotes: Double quotes (
"not') - Formatter: Ruff (no Black, no autopep8)
- Mid-file imports are allowed (E402 suppressed)
- Variable redefinitions are allowed (F811 suppressed)
- Some naming conventions are relaxed (N803, N806 suppressed)
Git Workflow: Conventional Commits
The cookbook follows the conventional commits standard for commit messages. This makes the git history readable and enables automated changelog generation.
Branch naming:<username>/<feature-description>
Commit format:
feat(scope): add new feature
fix(scope): fix bug
docs(scope): update documentation
style: lint/format
Examples:
feat(tool_use): add weather API tool examplefix(multimodal): correct image encoding in notebookdocs: update README with new cookbook structure
Model Selection: Use Current Models Correctly
One of the most important rules in the cookbook — and one that's easy to get wrong — is how to reference Claude models.
Use Non-dated Model Aliases
Always use the non-dated alias for Claude models in your API calls:
# ✅ CORRECT: Use non-dated aliases
model="claude-sonnet-4-6"
model="claude-haiku-4-5"
model="claude-opus-4-6"
❌ WRONG: Never use dated model IDs
model="claude-sonnet-4-6-20250514" # Don't do this
Non-dated aliases automatically point to the latest version of that model, ensuring your code stays current without manual updates.
Bedrock Model IDs
If you're using Claude through AWS Bedrock, the model IDs follow a different format:
# Opus 4.6
model="anthropic.claude-opus-4-6-v1"
Sonnet 4.5
model="anthropic.claude-sonnet-4-5-20250929-v1:0"
Haiku 4.5
model="anthropic.claude-haiku-4-5-20251001-v1:0"
For global endpoints (recommended), prepend global.:
model="global.anthropic.claude-opus-4-6-v1"
Note: Bedrock models before Opus 4.6 require dated IDs. Always check the Anthropic documentation for the latest model IDs.
Project Structure: Where Everything Lives
The cookbook is organized into clear directories, each serving a specific purpose:
capabilities/ # Core Claude capabilities (RAG, classification, etc.)
skills/ # Advanced skill-based notebooks
tool_use/ # Tool use and integration patterns
multimodal/ # Vision and image processing
misc/ # Batch processing, caching, utilities
third_party/ # Pinecone, Voyage, Wikipedia integrations
extended_thinking/ # Extended reasoning patterns
scripts/ # Validation scripts
.claude/ # Claude Code commands and skills
When exploring the cookbook, start with capabilities/ to understand the fundamentals, then move to tool_use/ and multimodal/ for more advanced patterns.
Slash Commands: Automate Quality Checks
The cookbook includes custom slash commands for Claude Code and CI pipelines:
/notebook-review— Reviews notebook quality/model-check— Validates Claude model references (catches dated model IDs)/link-review— Checks links in changed files
.claude/ directory and can be run directly in Claude Code or integrated into your CI workflow.
How to Add a New Cookbook Notebook
Contributing a new notebook is straightforward if you follow these steps:
Step 1: Create Your Notebook
Place your notebook in the appropriate directory based on its topic. Follow these guidelines:
- One concept per notebook — Don't cram multiple unrelated ideas into one file
- Keep outputs — Unlike production code, notebooks should retain their cell outputs for demonstration purposes
- Test top-to-bottom — Run all cells in order to ensure no errors
Step 2: Register Your Notebook
Add an entry to registry.yaml with the following fields:
- title: "Your Notebook Title"
description: "Brief description of what this notebook demonstrates"
path: capabilities/your-notebook.ipynb
authors:
- your-github-username
categories:
- capabilities
Step 3: Add Author Info
If you're a new contributor, add your details to authors.yaml:
your-github-username:
name: "Your Name"
url: "https://github.com/your-github-username"
Step 4: Run Quality Checks
Before submitting your pull request:
make check
This runs formatting, linting, and notebook structure validation. Fix any issues before pushing.
Step 5: Submit a PR
Create a branch following the naming convention (your-username/feature-description), commit with a conventional commit message, and open a pull request.
Practical Example: Your First Cookbook Notebook
Here's a minimal example of what a cookbook notebook might look like:
# cell 1: Setup
import os
from dotenv import load_dotenv
from anthropic import Anthropic
load_dotenv()
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
cell 2: Define the task
prompt = "Explain the concept of retrieval-augmented generation in one paragraph."
cell 3: Call Claude
response = client.messages.create(
model="claude-sonnet-4-6", # ✅ Non-dated alias
max_tokens=500,
messages=[{"role": "user", "content": prompt}]
)
cell 4: Display result
print(response.content[0].text)
This follows all the cookbook conventions: non-dated model alias, environment variable for API key, and clear cell structure.
Key Takeaways
- Use
uvfor dependency management — Runuv sync --all-extrasto set up your environment, and useuv addfor new packages instead of editingpyproject.tomldirectly. - Always use non-dated model aliases —
claude-sonnet-4-6instead ofclaude-sonnet-4-6-20250514. This keeps your code current and avoids deprecated model errors. - Run
make checkbefore every commit — This catches formatting, linting, and notebook structure issues early, preventing CI failures. - Follow conventional commits for git messages — Use the
feat(scope):,fix(scope):,docs(scope):format to maintain a clean, readable history. - Contribute one concept per notebook — Keep notebooks focused and test them top-to-bottom before submitting a PR with a
registry.yamlentry.