claude-cookbooks

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Claude Cookbooks Skill

Claude Cookbooks 技能

Comprehensive code examples and guides for building with Claude AI, sourced from the official Anthropic cookbooks repository.
本技能提供构建Claude AI应用的全面代码示例和指南,内容源自Anthropic官方烹饪手册仓库。

When to Use This Skill

何时使用此技能

This skill should be triggered when:
  • Learning how to use Claude API
  • Implementing Claude integrations
  • Building applications with Claude
  • Working with tool use and function calling
  • Implementing multimodal features (vision, image analysis)
  • Setting up RAG (Retrieval Augmented Generation)
  • Integrating Claude with third-party services
  • Building AI agents with Claude
  • Optimizing prompts for Claude
  • Implementing advanced patterns (caching, sub-agents, etc.)
在以下场景中应触发此技能:
  • 学习如何使用Claude API
  • 实现Claude集成
  • 构建基于Claude的应用程序
  • 处理工具调用和函数调用
  • 实现多模态功能(视觉、图像分析)
  • 搭建RAG(检索增强生成)系统
  • 将Claude与第三方服务集成
  • 使用Claude构建AI Agent
  • 优化Claude的提示词
  • 实现高级模式(缓存、子Agent等)

Quick Reference

快速参考

Basic API Usage

基础API使用

python
import anthropic

client = anthropic.Anthropic(api_key="your-api-key")
python
import anthropic

client = anthropic.Anthropic(api_key="your-api-key")

Simple message

Simple message

response = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1024, messages=[{ "role": "user", "content": "Hello, Claude!" }] )
undefined
response = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1024, messages=[{ "role": "user", "content": "Hello, Claude!" }] )
undefined

Tool Use (Function Calling)

工具调用(函数调用)

python
undefined
python
undefined

Define a tool

Define a tool

tools = [{ "name": "get_weather", "description": "Get current weather for a location", "input_schema": { "type": "object", "properties": { "location": {"type": "string", "description": "City name"} }, "required": ["location"] } }]
tools = [{ "name": "get_weather", "description": "Get current weather for a location", "input_schema": { "type": "object", "properties": { "location": {"type": "string", "description": "City name"} }, "required": ["location"] } }]

Use the tool

Use the tool

response = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1024, tools=tools, messages=[{"role": "user", "content": "What's the weather in San Francisco?"}] )
undefined
response = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1024, tools=tools, messages=[{"role": "user", "content": "What's the weather in San Francisco?"}] )
undefined

Vision (Image Analysis)

视觉(图像分析)

python
undefined
python
undefined

Analyze an image

Analyze an image

response = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1024, messages=[{ "role": "user", "content": [ { "type": "image", "source": { "type": "base64", "media_type": "image/jpeg", "data": base64_image } }, {"type": "text", "text": "Describe this image"} ] }] )
undefined
response = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1024, messages=[{ "role": "user", "content": [ { "type": "image", "source": { "type": "base64", "media_type": "image/jpeg", "data": base64_image } }, {"type": "text", "text": "Describe this image"} ] }] )
undefined

Prompt Caching

提示词缓存

python
undefined
python
undefined

Use prompt caching for efficiency

Use prompt caching for efficiency

response = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1024, system=[{ "type": "text", "text": "Large system prompt here...", "cache_control": {"type": "ephemeral"} }], messages=[{"role": "user", "content": "Your question"}] )
undefined
response = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1024, system=[{ "type": "text", "text": "Large system prompt here...", "cache_control": {"type": "ephemeral"} }], messages=[{"role": "user", "content": "Your question"}] )
undefined

Key Capabilities Covered

涵盖的核心功能

1. Classification

1. 分类

  • Text classification techniques
  • Sentiment analysis
  • Content categorization
  • Multi-label classification
  • 文本分类技术
  • 情感分析
  • 内容分类
  • 多标签分类

2. Retrieval Augmented Generation (RAG)

2. 检索增强生成(RAG)

  • Vector database integration
  • Semantic search
  • Context retrieval
  • Knowledge base queries
  • 向量数据库集成
  • 语义搜索
  • 上下文检索
  • 知识库查询

3. Summarization

3. 摘要生成

  • Document summarization
  • Meeting notes
  • Article condensing
  • Multi-document synthesis
  • 文档摘要
  • 会议记录整理
  • 文章浓缩
  • 多文档合成

4. Text-to-SQL

4. 文本转SQL

  • Natural language to SQL queries
  • Database schema understanding
  • Query optimization
  • Result interpretation
  • 自然语言转SQL查询
  • 数据库架构理解
  • 查询优化
  • 结果解读

5. Tool Use & Function Calling

5. 工具使用与函数调用

  • Tool definition and schema
  • Parameter validation
  • Multi-tool workflows
  • Error handling
  • 工具定义与 schema
  • 参数验证
  • 多工具工作流
  • 错误处理

6. Multimodal

6. 多模态

  • Image analysis and OCR
  • Chart/graph interpretation
  • Visual question answering
  • Image generation integration
  • 图像分析与OCR
  • 图表解读
  • 视觉问答
  • 图像生成集成

7. Advanced Patterns

7. 高级模式

  • Agent architectures
  • Sub-agent delegation
  • Prompt optimization
  • Cost optimization with caching
  • Agent架构
  • 子Agent委托
  • 提示词优化
  • 缓存成本优化

Repository Structure

仓库结构

The cookbooks are organized into these main categories:
  • capabilities/ - Core AI capabilities (classification, RAG, summarization, text-to-SQL)
  • tool_use/ - Function calling and tool integration examples
  • multimodal/ - Vision and image-related examples
  • patterns/ - Advanced patterns like agents and workflows
  • third_party/ - Integrations with external services (Pinecone, LlamaIndex, etc.)
  • claude_agent_sdk/ - Agent SDK examples and templates
  • misc/ - Additional utilities (PDF upload, JSON mode, evaluations, etc.)
烹饪手册按以下主要类别组织:
  • capabilities/ - 核心AI功能(分类、RAG、摘要生成、文本转SQL)
  • tool_use/ - 函数调用与工具集成示例
  • multimodal/ - 视觉与图像相关示例
  • patterns/ - Agent与工作流等高级模式
  • third_party/ - 与外部服务的集成(Pinecone、LlamaIndex等)
  • claude_agent_sdk/ - Agent SDK示例与模板
  • misc/ - 附加工具(PDF上传、JSON模式、评估等)

Reference Files

参考文档

This skill includes comprehensive documentation in
references/
:
  • main_readme.md - Main repository overview
  • capabilities.md - Core capabilities documentation
  • tool_use.md - Tool use and function calling guides
  • multimodal.md - Vision and multimodal capabilities
  • third_party.md - Third-party integrations
  • patterns.md - Advanced patterns and agents
  • index.md - Complete reference index
本技能在
references/
目录中包含全面的文档:
  • main_readme.md - 仓库总览
  • capabilities.md - 核心功能文档
  • tool_use.md - 工具使用与函数调用指南
  • multimodal.md - 视觉与多模态功能文档
  • third_party.md - 第三方集成文档
  • patterns.md - 高级模式与Agent文档
  • index.md - 完整参考索引

Common Use Cases

常见用例

Building a Customer Service Agent

构建客服Agent

  1. Define tools for CRM access, ticket creation, knowledge base search
  2. Use tool use API to handle function calls
  3. Implement conversation memory
  4. Add fallback mechanisms
See:
references/tool_use.md#customer-service
  1. 定义用于CRM访问、工单创建、知识库搜索的工具
  2. 使用工具调用API处理函数调用
  3. 实现对话记忆
  4. 添加回退机制
参考:
references/tool_use.md#customer-service

Implementing RAG

实现RAG

  1. Create embeddings of your documents
  2. Store in vector database (Pinecone, etc.)
  3. Retrieve relevant context on query
  4. Augment Claude's response with context
See:
references/capabilities.md#rag
  1. 为文档创建嵌入向量
  2. 存储到向量数据库(如Pinecone)
  3. 查询时检索相关上下文
  4. 用上下文增强Claude的响应
参考:
references/capabilities.md#rag

Processing Documents with Vision

用视觉处理文档

  1. Convert document to images or PDF
  2. Use vision API to extract content
  3. Structure the extracted data
  4. Validate and post-process
See:
references/multimodal.md#vision
  1. 将文档转换为图像或PDF
  2. 使用视觉API提取内容
  3. 结构化提取的数据
  4. 验证与后处理
参考:
references/multimodal.md#vision

Building Multi-Agent Systems

构建多Agent系统

  1. Define specialized agents for different tasks
  2. Implement routing logic
  3. Use sub-agents for delegation
  4. Aggregate results
See:
references/patterns.md#agents
  1. 为不同任务定义专用Agent
  2. 实现路由逻辑
  3. 使用子Agent进行委托
  4. 聚合结果
参考:
references/patterns.md#agents

Best Practices

最佳实践

API Usage

API使用

  • Use appropriate model for task (Sonnet for balance, Haiku for speed, Opus for complex tasks)
  • Implement retry logic with exponential backoff
  • Handle rate limits gracefully
  • Monitor token usage for cost optimization
  • 根据任务选择合适的模型(Sonnet兼顾平衡,Haiku追求速度,Opus处理复杂任务)
  • 实现带指数退避的重试逻辑
  • 优雅处理速率限制
  • 监控令牌使用以优化成本

Prompt Engineering

提示词工程

  • Be specific and clear in instructions
  • Provide examples when needed
  • Use system prompts for consistent behavior
  • Structure outputs with JSON mode when needed
  • 指令要具体清晰
  • 必要时提供示例
  • 使用系统提示词保证行为一致性
  • 需要时用JSON模式结构化输出

Tool Use

工具使用

  • Define clear, specific tool schemas
  • Validate inputs and outputs
  • Handle errors gracefully
  • Keep tool descriptions concise but informative
  • 定义清晰、具体的工具schema
  • 验证输入与输出
  • 优雅处理错误
  • 工具描述要简洁且信息丰富

Multimodal

多模态

  • Use high-quality images (higher resolution = better results)
  • Be specific about what to extract/analyze
  • Respect size limits (5MB per image)
  • Use appropriate image formats (JPEG, PNG, GIF, WebP)
  • 使用高质量图像(分辨率越高结果越好)
  • 明确说明要提取/分析的内容
  • 遵守大小限制(单张图片不超过5MB)
  • 使用合适的图像格式(JPEG、PNG、GIF、WebP)

Performance Optimization

性能优化

Prompt Caching

提示词缓存

  • Cache large system prompts
  • Cache frequently used context
  • Monitor cache hit rates
  • Balance caching vs. fresh content
  • 缓存大型系统提示词
  • 缓存频繁使用的上下文
  • 监控缓存命中率
  • 平衡缓存与新鲜内容

Cost Optimization

成本优化

  • Use Haiku for simple tasks
  • Implement prompt caching for repeated context
  • Set appropriate max_tokens
  • Batch similar requests
  • 简单任务使用Haiku模型
  • 对重复上下文使用提示词缓存
  • 设置合适的max_tokens值
  • 批量处理相似请求

Latency Optimization

延迟优化

  • Use streaming for long responses
  • Minimize message history
  • Optimize image sizes
  • Use appropriate timeout values
  • 长响应使用流式输出
  • 最小化消息历史
  • 优化图像大小
  • 设置合适的超时值

Resources

资源

Official Documentation

官方文档

Community

社区

Learning Resources

学习资源

Working with This Skill

如何使用此技能

For Beginners

面向初学者

Start with
references/main_readme.md
and explore basic examples in
references/capabilities.md
references/main_readme.md
开始,探索
references/capabilities.md
中的基础示例

For Specific Features

面向特定功能

  • Tool use →
    references/tool_use.md
  • Vision →
    references/multimodal.md
  • RAG →
    references/capabilities.md#rag
  • Agents →
    references/patterns.md#agents
  • 工具调用 →
    references/tool_use.md
  • 视觉功能 →
    references/multimodal.md
  • RAG →
    references/capabilities.md#rag
  • Agent →
    references/patterns.md#agents

For Code Examples

面向代码示例

Each reference file contains practical, copy-pasteable code examples
每个参考文档都包含可直接复制使用的实用代码示例

Examples Available

可用示例

The cookbook includes 50+ practical examples including:
  • Customer service chatbot with tool use
  • RAG with Pinecone vector database
  • Document summarization
  • Image analysis and OCR
  • Chart/graph interpretation
  • Natural language to SQL
  • Content moderation filter
  • Automated evaluations
  • Multi-agent systems
  • Prompt caching optimization
烹饪手册包含50多个实用示例,包括:
  • 带工具调用的客服聊天机器人
  • 基于Pinecone向量数据库的RAG
  • 文档摘要生成
  • 图像分析与OCR
  • 图表解读
  • 自然语言转SQL
  • 内容审核过滤器
  • 自动化评估
  • 多Agent系统
  • 提示词缓存优化

Notes

注意事项

  • All examples use official Anthropic Python SDK
  • Code is production-ready with error handling
  • Examples follow current API best practices
  • Regular updates from Anthropic team
  • Community contributions welcome
  • 所有示例使用官方Anthropic Python SDK
  • 代码包含错误处理,可用于生产环境
  • 示例遵循当前API最佳实践
  • 由Anthropic团队定期更新
  • 欢迎社区贡献

Skill Source

技能来源

This skill was created from the official Anthropic Claude Cookbooks repository: https://github.com/anthropics/claude-cookbooks
Repository cloned and processed on: 2025-10-29
本技能源自Anthropic官方Claude烹饪手册仓库: https://github.com/anthropics/claude-cookbooks
仓库克隆与处理时间:2025-10-29