Mastering the Anthropic Cookbook: A Practical Guide to Building with Claude API
Learn how to set up, develop, and contribute to the official Anthropic Cookbook. Includes environment setup, code style, model best practices, and workflow tips for Claude AI developers.
This guide walks you through using the Anthropic Cookbook repository: setting up your environment with uv, following code style rules, using correct Claude model IDs, running quality checks, and contributing new notebooks. Perfect for developers building applications with the Claude API.
Mastering the Anthropic Cookbook: A Practical Guide to Building with Claude API
If you're building applications with Claude AI, the Anthropic Cookbook is your most valuable resource. This official repository contains Jupyter notebooks and Python examples that demonstrate everything from basic API calls to advanced patterns like tool use, multimodal processing, and extended thinking.
But a cookbook is only as good as your ability to use it. In this guide, I'll walk you through setting up the development environment, understanding the code conventions, using the correct Claude model IDs, and even contributing your own notebooks. Let's get cooking.
Setting Up Your Development Environment
The Cookbook uses uv, a fast Python package manager. Here's how to get started:
Step 1: Install Dependencies
uv sync --all-extras
This command installs all required packages, including Jupyter, the Anthropic SDK, and development tools like Ruff (linter/formatter) and pre-commit hooks.
Step 2: Install Pre-commit Hooks
uv run pre-commit install
Pre-commit hooks automatically check your code for issues before each commit. They enforce formatting rules and validate notebook structure.
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 repository's .gitignore already excludes it, but always double-check. In your notebooks, load the key like this:
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("ANTHROPIC_API_KEY")
Code Style and Quality Standards
The Cookbook enforces consistent code style to keep notebooks readable and maintainable. Here's what you need to know:
Formatting Rules
- Line length: 100 characters maximum
- Quotes: Always use double quotes (
"not') - Formatter: Ruff (configured in
pyproject.toml)
Running Quality Checks
Before submitting any changes, run the full quality check suite:
make check
This runs both formatting checks and linting. You can also run individual commands:
make format # Format code with ruff
make lint # Run linting
make fix # Auto-fix issues + format
make test # Run pytest
If you prefer using 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
Notebook-Specific Rules
Notebooks get some leniency because they're meant for demonstration:
- Mid-file imports are allowed (E402 warning suppressed)
- Variable redefinitions are allowed (F811 suppressed)
- Some naming conventions are relaxed (N803, N806 suppressed)
- Run top-to-bottom without errors
- Keep cell outputs (they're intentional for demonstration)
- Focus on one concept per notebook
Using the Correct Claude Model IDs
One of the most common mistakes developers make is using outdated or incorrect model IDs. The Cookbook enforces a strict policy: always use non-dated model aliases.
Correct API Model IDs
| Model | ID to Use |
|---|---|
| Claude Opus 4.6 | claude-opus-4-6 |
| Claude Sonnet 4.6 | claude-sonnet-4-6 |
| Claude Haiku 4.5 | claude-haiku-4-5 |
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 Amazon Bedrock, the format is different. Always prepend global. for global endpoints (recommended):
| Model | Bedrock ID |
|---|---|
| Opus 4.6 | global.anthropic.claude-opus-4-6-v1 |
| Sonnet 4.5 | global.anthropic.claude-sonnet-4-5-20250929-v1:0 |
| Haiku 4.5 | global.anthropic.claude-haiku-4-5-20251001-v1:0 |
Practical Example
Here's how to use the correct model ID in your code:
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-6", # Correct: non-dated alias
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain quantum computing in simple terms."}
]
)
print(response.content[0].text)
Git Workflow and Contribution Guidelines
If you plan to contribute notebooks or fixes, follow these conventions:
Branch Naming
<username>/<feature-description>
Example: johndoe/add-rag-tutorial
Commit Messages
Use conventional commits:
feat(scope): add new feature
fix(scope): fix bug
docs(scope): update documentation
style: lint/format
Adding a New Cookbook
- Create your notebook in the appropriate directory (see project structure below)
- Register it by adding an entry to
registry.yamlwith:
- Add author info to
authors.yamlif you're a new contributor - Run quality checks (
make check) - Submit a PR
Project Structure Overview
The Cookbook is organized into focused 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
Choose the directory that matches your notebook's focus.
Slash Commands for Claude Code
The Cookbook includes custom slash commands that work in Claude Code and CI:
/notebook-review- Reviews notebook quality/model-check- Validates Claude model references/link-review- Checks links in changed files
Dependency Management
Never editpyproject.toml directly. Instead, use uv commands:
uv add <package> # Add a production dependency
uv add --dev <package> # Add a development dependency
This ensures dependency versions are properly tracked and conflicts are avoided.
Key Takeaways
- Use
uvfor environment management – it's faster and more reliable than pip. Always runuv sync --all-extrasto install everything. - Always use non-dated Claude model aliases (e.g.,
claude-sonnet-4-6) in API calls. Never use dated IDs likeclaude-sonnet-4-6-20250514. - Run
make checkbefore committing – this enforces formatting, linting, and notebook structure validation. - Keep notebooks focused and runnable – one concept per notebook, with outputs preserved for demonstration.
- Follow the contribution workflow – create a feature branch, use conventional commits, register your notebook in
registry.yaml, and submit a PR.