Mastering the Anthropic Cookbook: A Practical Guide to Building with Claude API
Learn how to set up, develop, and contribute to the Anthropic Cookbook—a collection of Jupyter notebooks and Python examples for building powerful applications 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 the correct Claude model IDs, and contributing your own cookbook notebooks.
Mastering the Anthropic Cookbook: A Practical Guide to Building with Claude API
The Anthropic Cookbook is an essential resource for anyone building applications with the Claude API. It contains a curated collection of Jupyter notebooks and Python examples covering everything from basic capabilities to advanced patterns like tool use, multimodal processing, and extended reasoning.
Whether you're a seasoned Claude developer or just getting started, understanding how to navigate, set up, and contribute to this cookbook will accelerate your development workflow and help you build better AI-powered applications.
In this guide, you'll learn:
- How to set up the cookbook environment on your local machine
- The project structure and where to find specific examples
- Code style conventions and quality checks
- How to use the correct Claude model IDs (and avoid common pitfalls)
- How to contribute your own cookbook notebooks
Setting Up the Cookbook Environment
Before you can run any of the cookbook examples, you need to set up your local environment. The cookbook uses uv as its package manager and dependency resolver.
Prerequisites
- Python 3.10 or higher
uvinstalled (see uv documentation)- An Anthropic API key
Step 1: Clone the Repository
git clone https://github.com/anthropics/anthropic-cookbook.git
cd anthropic-cookbook
Step 2: Install Dependencies
The cookbook uses uv sync to install all dependencies, including development extras:
uv sync --all-extras
This command reads the pyproject.toml file and installs everything you need, including Jupyter, Ruff (the linter/formatter), and all notebook-specific libraries.
Step 3: Set Up Pre-commit Hooks
Pre-commit hooks automatically check your code quality before each commit. Install them with:
uv run pre-commit install
Step 4: Configure Your API Key
Copy the example environment file and add your Anthropic API key:
cp .env.example .env
Then edit .env and add your key:
ANTHROPIC_API_KEY=sk-ant-...
Important: Never commit your .env file. It's already in .gitignore, but double-check before pushing.
Understanding the Project Structure
The cookbook is organized into thematic directories, each focusing on a specific Claude capability or integration pattern:
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 looking for a specific example, start by identifying the category that matches your use case. For example:
- Building a RAG system? Check
capabilities/ - Integrating with external tools? Check
tool_use/ - Working with images? Check
multimodal/
Code Style and Quality Checks
Consistency matters in a collaborative project. The cookbook enforces a strict code style using Ruff:
- Line length: 100 characters
- Quotes: Double quotes (
"not') - Formatter: Ruff (replaces Black and isort)
Running Quality Checks
Before committing, always run the full quality check suite:
make check
This runs both formatting checks and linting. If you want to auto-fix issues:
make fix
Or use uv directly:
uv run ruff format . # Format all files
uv run ruff check . # Lint all files
uv run ruff check --fix . # Auto-fix lint issues
uv run pre-commit run --all-files # Run all pre-commit hooks
Notebook-Specific Rules
Jupyter notebooks have slightly relaxed rules to accommodate the interactive nature of notebooks:
- Mid-file imports are allowed (E402 suppressed)
- Variable redefinitions are allowed (F811 suppressed)
- Some naming conventions are relaxed (N803, N806)
Using the Correct Claude Model IDs
One of the most common mistakes when working with the Claude API is using outdated or incorrect model IDs. The cookbook enforces a strict policy: always use non-dated model aliases.
Correct API Model IDs
| Model | API ID |
|---|---|
| Sonnet | claude-sonnet-4-6 |
| Haiku | claude-haiku-4-5 |
| Opus | claude-opus-4-6 |
claude-sonnet-4-6-20250514. The non-dated aliases always point to the latest stable version of that model.
Correct Bedrock Model IDs
If you're using Claude through Amazon Bedrock, the model IDs follow a different format:
| Model | Bedrock ID |
|---|---|
| Opus 4.6 | anthropic.claude-opus-4-6-v1 |
| Sonnet 4.5 | anthropic.claude-sonnet-4-5-20250929-v1:0 |
| Haiku 4.5 | anthropic.claude-haiku-4-5-20251001-v1:0 |
global.:
model_id = "global.anthropic.claude-opus-4-6-v1"
Note: Bedrock models released before Opus 4.6 require dated IDs. Always check the official documentation for the latest model IDs.
Example: Using the Correct Model in Python
import anthropic
from dotenv import load_dotenv
import os
load_dotenv()
client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
response = client.messages.create(
model="claude-sonnet-4-6", # Correct: non-dated alias
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain the concept of recursion in simple terms."}
]
)
print(response.content[0].text)
Git Workflow and Commit Conventions
The cookbook follows a structured git workflow to maintain a clean history:
Branch Naming
Use the format <username>/<feature-description>:
johndoe/add-rag-example
janedoe/fix-tool-use-bug
Commit Messages
Use conventional commits:
feat(rag): add vector store integration example
fix(tool_use): correct function call syntax in notebook
docs(readme): update setup instructions
style: format notebooks with ruff
Before Committing
- Run
make checkto ensure all quality checks pass - Verify notebooks run top-to-bottom without errors
- Check that no
.envfiles or API keys are included
Slash Commands for Claude Code
The cookbook includes custom slash commands that work with Claude Code and CI pipelines:
/notebook-review- Reviews notebook quality and structure/model-check- Validates Claude model references in your code/link-review- Checks links in changed files
Adding a New Cookbook
Contributing your own cookbook notebook is straightforward. Here's the process:
Step 1: Create Your Notebook
Place your notebook in the appropriate directory based on its topic:
# Example: adding a RAG notebook
capabilities/rag_with_pinecone.ipynb
Step 2: Register the Notebook
Add an entry to registry.yaml with the following fields:
- title: "RAG with Pinecone"
description: "Build a retrieval-augmented generation system using Pinecone vector database"
path: capabilities/rag_with_pinecone.ipynb
authors:
- johndoe
categories:
- capabilities
- rag
Step 3: Add Author Information
If you're a new contributor, add your details to authors.yaml:
johndoe:
name: John Doe
github: johndoe
twitter: "@johndoe"
Step 4: Run Quality Checks
make check
Step 5: Submit a Pull Request
Create a PR with a descriptive title and description. The CI pipeline will automatically run quality checks and validate your notebook.
Best Practices for Cookbook Development
- One concept per notebook - Keep notebooks focused on a single capability or pattern. This makes them easier to understand and reuse.
- Keep outputs intact - Unlike traditional code repositories, cookbook notebooks intentionally keep their outputs. These serve as demonstrations of expected behavior.
- Test from top to bottom - Before submitting, run the entire notebook to ensure all cells execute without errors.
- Use environment variables for secrets - Never hardcode API keys. Use
dotenv.load_dotenv()and access keys viaos.environoros.getenv().
- Stay current with model IDs - Check docs.anthropic.com regularly for the latest model versions and IDs.
Conclusion
The Anthropic Cookbook is more than just a collection of examples—it's a development framework that enforces best practices for building with the Claude API. By following the setup instructions, code style guidelines, and contribution workflow outlined in this guide, you'll be able to:
- Quickly set up a development environment for Claude API projects
- Find and run relevant examples for your use case
- Contribute high-quality notebooks that help the community
- Avoid common pitfalls like outdated model IDs or hardcoded secrets
Key Takeaways
- Set up with
uv sync --all-extrasand always runmake checkbefore committing to ensure code quality. - Use non-dated model aliases (e.g.,
claude-sonnet-4-6) instead of dated IDs to always target the latest stable version. - Never commit API keys—use
.envfiles anddotenv.load_dotenv()to manage secrets securely. - Follow conventional commits (
feat:,fix:,docs:) and branch naming (<username>/<feature>) for a clean git history. - Contribute one concept per notebook and register it in
registry.yamlwith proper author attribution.