Skip to content

插件参考

Claude Code 插件 API 参考。本文提供完整的代码示例和终端操作演示。

快速开始

在终端中启动 Claude Code:

bash
claude

You: 帮我实现Claude Code 插件 API 参考

Claude: 好的,让我先看看项目结构...

Claude wants to:
 Read file: src/main.py
Allow? [y/n/always/never]

You: y

Claude: 让我为你实现这个功能...

Shift+Tab 切换权限模式:

bash
# Normal Mode → Auto-Accept Mode → Plan Mode

# Auto-Accept: 自动执行(底部显示 ⏵⏵ accept edits on)
You: 快速实现
Claude: [自动执行,无需确认]

# Plan Mode: 只读(底部显示 ⏸ plan mode on)
You: 先制定计划
Claude: [只制定计划,不执行]

基本概念

bash
You: 解释一下插件参考的核心概念

Claude: Claude Code 插件 API 参考的核心概念包括...
python
from anthropic import Anthropic

client = Anthropic()
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Claude Code 插件 API 参考"}]
)
print(response.content[0].text)

实现步骤

1. 环境准备

bash
# 安装依赖
pip install anthropic

# 配置 API Key
export ANTHROPIC_API_KEY="your-api-key"

2. 核心实现

bash
You: 帮我实现插件参考的核心功能

Claude: 好的,让我创建实现文件...

Claude wants to:
 Write file: src/插件参考.py
Allow? [y/n/always/never]

You: y

Claude: 文件已创建。
python
# 插件参考 核心实现
from anthropic import Anthropic

client = Anthropic()

def process(query: str) -> str:
    """处理用户请求"""
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=2048,
        messages=[{"role": "user", "content": query}]
    )
    return response.content[0].text

if __name__ == "__main__":
    result = process("Claude Code 插件 API 参考")
    print(result)

3. 测试验证

bash
You: 运行测试验证实现

Claude: 好的,让我运行测试...

Claude wants to:
 Run command: python -m pytest tests/
Allow? [y/n/always/never]

You: y

Claude: 所有测试通过

高级用法

python
# 使用流式输出
with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=2048,
    messages=[{"role": "user", "content": "Claude Code 插件 API 参考"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
bash
# 使用工具调用
tools = [{
    "name": "process",
    "description": "Claude Code 插件 API 参考",
    "input_schema": {
        "type": "object",
        "properties": {
            "query": {"type": "string"}
        },
        "required": ["query"]
    }
}]

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user", "content": "Claude Code 插件 API 参考"}]
)

性能优化

python
# 使用 Prompt Caching 降低成本
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system=[{
        "type": "text",
        "text": "你是一个Claude Code 插件 API 参考专家...",
        "cache_control": {"type": "ephemeral"}
    }],
    messages=[...]
)
bash
# 查看成本
/cost

# 切换到更快的模型
/model haiku

# 使用快速模式
/fast

错误处理

python
from anthropic import APIError, APITimeoutError

try:
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=[{"role": "user", "content": "请求"}]
    )
except APITimeoutError:
    print("请求超时,请重试")
except APIError as e:
    print(f"API 错误: {e.status_code} - {e.message}")

最佳实践

  1. 明确任务描述 - 告诉 Claude 具体要做什么
  2. 提供上下文 - 使用 CLAUDE.md 或直接提供相关代码
  3. 逐步验证 - 每完成一步就验证结果
  4. 使用 Plan Mode - 复杂任务先制定计划
  5. 监控成本 - 使用 /cost 查看 API 用量
  6. 合理选择模型 - 简单任务用 Haiku,复杂任务用 Opus

权限模式速查

模式快捷键底部显示行为
Normal默认无标识每次操作需确认
Auto-AcceptShift+Tab ×1⏵⏵ accept edits on自动执行编辑
PlanShift+Tab ×2⏸ plan mode on只制定计划

常用命令速查

bash
/help          # 查看帮助
/clear         # 清空历史
/model opus    # 切换到 Opus 4.6
/model sonnet  # 切换到 Sonnet 4.6
/model haiku   # 切换到 Haiku 4.5
/cost          # 查看成本
/fast          # 快速模式
/quit          # 退出

相关资源

基于 MIT 许可发布