Claude Code /claude-api 技能:API 参考自动加载
深入了解 Claude Code 的 /claude-api 技能,自动加载 Claude API 文档和 Agent SDK 参考
什么是 /claude-api 技能?
/claude-api 是 Claude Code 的 API 参考技能,专门为开发者提供 Claude API 和 Agent SDK 的完整文档。当你需要使用 Claude API 构建应用时,这个技能会自动加载对应语言的 API 参考资料。
核心特性
- 多语言支持:Python、TypeScript、Java、Go、Ruby、C#、PHP、cURL
- 自动加载:检测代码导入自动激活
- 完整参考:工具使用、流式输出、批量处理全覆盖
基本用法
手动调用
/claude-api
运行后会加载项目语言对应的 API 文档。
自动触发
当你导入以下包时自动激活:
anthropic@anthropic-ai/sdkclaude_agent_sdk
支持的语言
| 语言 | 安装包 | 文档范围 |
|---|---|---|
| Python | anthropic | 完整 |
| TypeScript | @anthropic-ai/sdk | 完整 |
| Java | anthropic-java | 完整 |
| Go | github.com/anthropics/claude-go | 完整 |
| Ruby | anthropic gem | 完整 |
| C# | Anthropic.SDK | 完整 |
| PHP | anthropic-php | 完整 |
| cURL | - | 完整 |
功能覆盖
1. 工具使用 (Tool Use)
学习如何在 API 调用中使用工具:
# Python 示例
from anthropic import Anthropic
client = Anthropic()
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
tools=[
{
"name": "weather",
"description": "Get weather for a location",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}
],
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)
2. 流式输出 (Streaming)
处理实时流式响应:
// TypeScript 示例
import { Anthropic } from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY
});
const stream = await client.messages.stream({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Write a story' }]
});
for await (const chunk of stream) {
if (chunk.type === 'content_block_delta') {
process.stdout.write(chunk.delta.text);
}
}
3. 批量处理 (Batches)
高效处理大量请求:
# Python - 批量创建
client.beta.messages.batches.create(
model="claude-3-5-sonnet-20241022",
messages=[
{"role": "user", "content": "Hello"},
{"role": "user", "content": "Hi there"},
# ... 更多消息
]
)
4. 结构化输出 (Structured Outputs)
生成 JSON 等结构化内容:
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Extract user info"}],
text={
"format": {
"type": "json_schema",
"name": "user_info",
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string"}
}
}
}
}
)
实际案例
案例 1:构建聊天机器人
> /claude-api
已加载 Python API 文档
> 帮我写一个简单的 CLI 聊天机器人
系统会使用最新 API 最佳实践帮你构建。
案例 2:集成到现有项目
当你在项目中导入 anthropic 包时:
import anthropic
# /claude-api 自动激活
# 提供即时 API 帮助
案例 3:使用 Agent SDK
# Agent SDK 使用
from claude_agent_sdk import ClaudeAgent
agent = ClaudeAgent(
instructions="你是一个代码审查助手",
tools=["Read", "Grep"]
)
result = agent.run("审查这个 PR")
常见陷阱
1. API Key 配置
# ❌ 错误
client = Anthropic() # 缺少 API Key
# ✅ 正确
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
2. 模型名称
# ❌ 过期模型
model="claude-3-opus-20240229"
# ✅ 当前模型
model="claude-3-5-sonnet-20241022"
3. 异步处理
# ✅ 正确使用 async/await
import asyncio
async def main():
client = Anthropic()
message = await client.messages.create(...)
最佳实践
1. 环境变量
使用环境变量存储 API Key:
# .env
ANTHROPIC_API_KEY=sk-...
from dotenv import load_dotenv
load_dotenv()
2. 错误处理
from anthropic import RateLimitError, APIError
try:
response = client.messages.create(...)
except RateLimitError:
# 处理速率限制
pass
except APIError as e:
# 处理 API 错误
pass
3. 重试机制
from tenacity import retry, stop_after_attempt, wait_exponential
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=1, max=10))
def call_api():
return client.messages.create(...)
常见问题
Q: 支持哪些 SDK 版本?
支持最新稳定版。旧版本 API 可能有差异。
Q: 可以自定义加载的文档吗?
可以,通过配置指定特定版本。
Q: Token 限制是多少?
取决于模型: Sonnet 200K,Haiku 100K。
总结
/claude-api 技能是开发者的必备工具:
- 即时参考:开发时随时获取 API 文档
- 多语言覆盖:主流语言全部支持
- 最佳实践:包含常见模式和陷阱避免
当你需要构建 Claude API 应用时,/claude-api 是你的最佳伙伴!
相关文章
评论
加载中...
评论
加载中...