A comprehensive collection of Python examples, patterns, and implementations for building applications with the Anthropic Claude API. This repository serves as both a learning resource and a reference guide for developers working with Claude.
This cookbook covers the full spectrum of Claude API capabilities, from basic prompting to advanced agent architectures. Each file is a standalone example with detailed documentation, best practices, and production-ready patterns.
Prompting.py- Prompt evaluation framework with automated dataset generation, testing, and gradingTool_use.py- Tool integration patterns including datetime tools, reminders, and batch operationsTool_streaming.py- Real-time streaming with tool useTools_multi-turn.py- Multi-turn conversations with tool executionStructured_data.py- Extracting structured data from Claude responsesStructuredData_Exercise.py- Practical exercises for structured data extractionThinking.py- Extended thinking capabilities for deeper reasoning tasksImages.py- Image analysis, OCR, multi-image comparison, and vision capabilitiesPDF.py- PDF processing and analysisCitations.py- Grounding responses with citations and references
A new execution model: persistent, session-based agents running in sandboxed cloud environments. Each file is self-contained and demonstrates one capability of the Managed Agents API (research preview).
managed_agents/README.md- Concept map, decision guide, andclient.beta.*namespace referencemanaged_agents/01_multi_agent_coordinator.py- Coordinator + sub-agent roster, parallelization, specialization, and escalation patternsmanaged_agents/02_outcomes_with_rubric.py- Outcome-driven sessions: define a rubric, let the agent iterate until it satisfies itmanaged_agents/03_dreams_memory_consolidation.py- Async memory consolidation: deduplicate and resolve contradictions across sessionsmanaged_agents/04_webhooks.py- Flask webhook handler with signature verification and idempotencymanaged_agents/05_advisor_strategy.py- Tiered Opus / Sonnet / Haiku model routing for cost-efficient multi-agent systems
-
RAG_system.py- Complete RAG (Retrieval-Augmented Generation) implementation with:- Vector and BM25 hybrid search
- Contextual enrichment
- Reranking strategies
- Multiple chunking approaches
-
Web_search.py- Web search integration and information retrieval -
Caching.py- Prompt caching for cost optimization and performance -
Code_execution.py- Safe code execution capabilities -
MCP.py- Model Context Protocol implementation guide for building servers and clients -
Agents_and_Workflows.py- Architectural patterns:- Workflows vs Agents decision framework
- Parallelization patterns
- Chaining strategies
- Routing workflows
Running_Eval.py- Evaluation framework for testing promptsTest_Datasets.py- Dataset generation and managementPrompt_Dataset.py- Prompt testing datasetsCode_Grading.py- Automated code evaluationGrader_Exercise.py- Grading system exercisesModel_Grading.py- Model output evaluation
streaming_demo.py- Streaming response demonstrationsprefill_and_stop_demo.py- Prefill and stop sequence examplesstop_sequences_template.py- Stop sequence patternsstructured_data_template.py- Templates for structured outputtest_client.py- Client testing utilitiesText_editor_tool.py- Text editing tool implementationClaude_Code.py- Claude Code integration examples
pip install anthropic python-dotenv voyageaiFor Managed Agents webhook examples:
pip install flaskFor MCP examples:
pip install "mcp[cli]>=1.8.0"- Create a
.envfile in the project root:
ANTHROPIC_API_KEY=your_api_key_here
VOYAGE_API_KEY=your_voyage_key_here # For RAG examples- Each Python file is self-contained and can be run independently:
python Prompting.py
python Tool_use.py
python RAG_system.pyEach example includes error handling, best practices, and considerations for production deployment.
Inline documentation explains not just how to use features, but when and why to use them.
Helper functions and reusable components make it easy to integrate patterns into your own projects.
Examples progress from basic concepts to advanced architectures, building on earlier patterns.
- Parallelization - Split complex evaluations into focused, parallel tasks
- Chaining - Sequential processing for multi-step refinement
- Routing - Direct requests to specialized pipelines
- Tool Design - Abstract, composable tools for maximum flexibility
- Environment Inspection - Verify actions and adapt behavior
- Multi-Turn Planning - Complex task decomposition
This cookbook demonstrates implementations for:
- ๐ฌ Conversational AI applications
- ๐ Data extraction and analysis
- ๐ Semantic search and RAG systems
- ๐ค Autonomous agents and workflows
- ๐ Document processing and OCR
- ๐งฎ Complex reasoning and problem-solving
- ๐ง Tool integration and API orchestration
This repository was developed with significant contributions from Claude Code (Anthropic) in collaboration with ArchieCur.
Original cookbook (2025): powered by Claude Sonnet 4.5 (claude-sonnet-4-5-20250929). Claude Code assisted in:
- Architecture Design - Designing modular, extensible patterns for each capability
- Code Implementation - Writing production-ready Python code with proper error handling and type hints
- Documentation - Creating comprehensive inline documentation and pattern explanations
- Best Practices - Incorporating lessons learned from real-world Claude API usage
- Educational Structure - Organizing examples progressively from basic to advanced concepts
- Testing & Validation - Ensuring examples are practical and production-ready
Managed Agents update (May 2026): powered by Claude Sonnet 4.6 (claude-sonnet-4-6). Added the managed_agents/ folder covering the Managed Agents API research preview โ six self-contained files spanning multi-agent coordination, outcome-driven sessions, Dreams memory consolidation, webhooks, and the advisor strategy pattern.
The collaboration between human guidance and Claude's technical capabilities resulted in a resource that bridges theoretical understanding with practical implementation.
- Never commit API keys (use
.envfiles) - Validate and sanitize user inputs
- Implement rate limiting for production use
- Use prompt caching for repeated patterns
- Choose appropriate models (Haiku for simple tasks, Sonnet for balance, Opus for quality)
- Batch similar requests when possible
- Monitor token usage
- Implement streaming for better UX
- Use async/await for concurrent operations
- Cache embeddings and frequent computations
- Optimize image sizes before sending
- Start with workflows before resorting to agents
- Implement proper error handling and retries
- Test prompts systematically with evaluation frameworks
- Monitor and log for continuous improvement
from anthropic import Anthropic
client = Anthropic()
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
tools=[get_current_datetime_schema],
messages=[{"role": "user", "content": "What time is it?"}]
)from RAG_system import RAGSystem
rag = RAGSystem(
chunking_strategy="sentences",
use_contextual_enrichment=True,
use_reranking=True
)
rag.add_documents(documents)
answer = rag.query("Your question here", k=3)response = chat(
messages,
thinking=True,
thinking_budget=2048
)This repository is designed as a learning resource. Feel free to:
- Use these patterns in your own projects
- Adapt examples to your specific needs
- Share feedback or suggestions
- Report issues or inconsistencies
This is an educational resource. Use these examples as reference for your own projects.
Built with Claude - Demonstrating the power of human-AI collaboration in creating comprehensive technical resources.