The Complete Guide to the Anthropic Cookbook: Building with Claude’s API
Learn how to set up, develop, and contribute to the Anthropic Cookbook — a collection of Jupyter notebooks and Python examples for building with the Claude API. Includes setup, code style, model best practices, and contribution workflow.
This guide walks you through setting up the Anthropic Cookbook repository, understanding its structure, following code style and model conventions, and contributing new cookbook notebooks using Claude Code and CI commands.
The Complete Guide to the Anthropic Cookbook: Building with Claude’s API
If you’re building applications with Claude, the Anthropic Cookbook is your single best resource for practical, hands-on examples. It’s a curated collection of Jupyter notebooks and Python scripts that demonstrate everything from basic API calls to advanced patterns like tool use, multimodal processing, and extended thinking.
This guide will walk you through everything you need to know to get started with the Cookbook: installation, development workflow, code style, model selection best practices, and how to contribute your own notebooks.
What Is the Anthropic Cookbook?
The Cookbook is an open-source repository maintained by Anthropic. It’s designed to be a living library of examples that grow alongside the Claude API. Each notebook focuses on one concept — classification, retrieval-augmented generation (RAG), vision, tool use, and more — so you can quickly find and adapt the pattern you need.
Whether you’re a beginner exploring Claude for the first time or an experienced developer integrating Claude into production, the Cookbook gives you battle-tested code you can build on.
Getting Started: Setup and Installation
Before you can run any notebooks, you need to set up your local environment. The Cookbook uses uv, a fast Python package manager, for dependency management.
Step 1: Clone the Repository
git clone https://github.com/anthropics/anthropic-cookbook.git
cd anthropic-cookbook
Step 2: Install Dependencies
The recommended way is to use uv with all extras:
uv sync --all-extras
This installs everything you need, including Jupyter, the Anthropic SDK, and third-party integrations like Pinecone and Voyage.
Step 3: Set Up 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-...
Never commit your .env file. The repository is configured to ignore it by default. In your notebooks, load the key safely using:
import os
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv("ANTHROPIC_API_KEY")
Step 4: Install Pre-commit Hooks
To automatically check formatting and structure before each commit:
uv run pre-commit install
Development Commands You’ll Use Every Day
The Cookbook provides a Makefile with common commands. Here’s what each does:
| Command | Description |
|---|---|
make format | Format all Python files with Ruff |
make lint | Run Ruff linting checks |
make check | Run format-check + lint together |
make fix | Auto-fix lint issues and format |
make test | Run pytest |
uv:
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
Pro tip: Run make check before every commit. It catches formatting issues and notebook structure problems early.
Code Style: The Rules of the Road
The Cookbook enforces a consistent code style to keep notebooks readable and maintainable:
- Line length: 100 characters
- Quotes: Double quotes (
") only - Formatter: Ruff (replaces Black and isort)
- Mid-file imports (E402) — common in educational notebooks
- Redefinitions (F811) — useful when iterating in cells
- Certain variable naming conventions (N803, N806) — relaxed for readability
Model Selection: Use the Right Claude Model
One of the most important rules in the Cookbook is never use dated model IDs. Always use the non-dated alias. Here are the current models:
| Model | API Model ID |
|---|---|
| Sonnet | claude-sonnet-4-6 |
| Haiku | claude-haiku-4-5 |
| Opus | claude-opus-4-6 |
model = "claude-sonnet-4-6-20250514" # Dated — don't use this
Correct:
model = "claude-sonnet-4-6" # Always use the alias
Bedrock Users
If you’re using Claude through AWS Bedrock, the model IDs follow a different format. Use the base Bedrock model ID from the docs:
# Opus 4.6
model = "global.anthropic.claude-opus-4-6-v1"
Sonnet 4.5
model = "global.anthropic.claude-sonnet-4-5-20250929-v1:0"
Haiku 4.5
model = "global.anthropic.claude-haiku-4-5-20251001-v1:0"
Prepend global. for global endpoints (recommended). Note that Bedrock models before Opus 4.6 require dated IDs.
Project Structure: Where to Find What You Need
The Cookbook is organized into clear directories:
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
If you’re looking for a specific pattern, start in the most relevant directory. For example, if you want to build a RAG system, check capabilities/. If you want to use Claude with images, go to multimodal/.
Slash Commands: Claude Code Integration
The Cookbook includes custom slash commands that work in Claude Code and CI:
/notebook-review— Reviews notebook quality and structure/model-check— Validates that all Claude model references use current, non-dated IDs/link-review— Checks links in changed files
.claude/ directory and are a great example of how to extend Claude Code for your own projects.
How to Contribute a New Cookbook
Got a great Claude pattern to share? Here’s the workflow:
1. Create Your Notebook
Place your notebook in the appropriate directory. Follow these rules:
- One concept per notebook — don’t cram multiple patterns into one file
- Keep outputs — notebooks should show results for demonstration
- Test top-to-bottom — ensure the notebook runs without errors from the first cell to the last
2. Register Your Notebook
Add an entry to registry.yaml with:
title: A clear, descriptive titledescription: What the notebook demonstratespath: Relative path to the notebook fileauthors: Your name (or GitHub username)categories: One or more categories from the project structure
3. Add Author Info
If you’re a new contributor, add your details to authors.yaml.
4. Run Quality Checks
Before submitting a PR:
make check
This runs formatting checks, linting, and notebook structure validation.
5. Submit Your PR
Use conventional commit format:
feat(capabilities): add notebook for multi-turn classification
Branch naming follows: <username>/<feature-description>
Practical Example: Your First Cookbook Notebook
Here’s a minimal example of what a Cookbook notebook cell might look like:
import anthropic
import os
from dotenv import load_dotenv
load_dotenv()
client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
response = client.messages.create(
model="claude-sonnet-4-6", # Use the alias, not a dated ID
max_tokens=1000,
messages=[
{"role": "user", "content": "Explain the concept of RAG in one paragraph."}
]
)
print(response.content[0].text)
This follows all the Cookbook conventions: double quotes, non-dated model ID, environment variable for the API key, and clear, self-contained code.
Key Takeaways
- Always use non-dated model aliases (e.g.,
claude-sonnet-4-6) in your API calls. Dated IDs can break when models are updated. - Run
make checkbefore every commit to catch formatting issues and notebook structure problems early. - Keep one concept per notebook and ensure it runs top-to-bottom without errors. Outputs should be preserved for demonstration.
- Use
uvfor dependency management — never editpyproject.tomldirectly. Add packages withuv addoruv add --dev. - Leverage the slash commands (
/notebook-review,/model-check,/link-review) in Claude Code to automate quality checks during development.