Claude api

From findscripter/everything-skills · View interactive version

Claude api is an AI agent skill from the GitHub repository findscripter/everything-skills. 当用基于 Anthropic 官方 SDK 构建、调试、优化 Claude API 应用,或在模型版本间迁移代码时使用;做出含提示缓存、自适应思考、工具调用与流式的可运行 Claude 调用代码(产物:SDK 调用代码与配置);不适用于 OpenAI/其他厂商 SDK、provider-neutral 代码或通用编程/ML 问题;触发词:Claude API、Anthropic SDK、anthropic、prompt caching、提示缓存、tool use、工具调用、Managed Agents、模型迁移、claude-opus-4-8、adaptive thinking

How to use this skill

Skill file

本条目改写自 Anthropic 官方 claude-api 技能(Apache-2.0),保留其关键约束与默认值,面向 AI Agent 直接消费。

何时使用

满足以下任一条件时使用:

不该用的边界(命中即停手并说明):

步骤

  1. 入场扫描:检查目标文件/项目是否含非 Anthropic 标记(import openai、gpt-4/5、langchain_openai 等)。命中则停手询问,不要继续。
  2. 语言识别:按项目文件推断语言并读取对应官方文档——*.py→Python,*.ts/.js→TypeScript(JS 共用 TS SDK),*.java/.kt/.scala→Java,*.go→Go,*.rb→Ruby,*.cs→C#,*.php→PHP。无法判断时询问用户,默认 Python。
  3. 选对调用面(surface):
  1. 写出 SDK 代码:函数名、类名、方法签名、import 路径必须来自明确文档,不得凭 cURL 形状或别的语言 SDK 臆测;缺失绑定时先 WebFetch 官方仓库再写。
  2. 默认就加提示缓存(本技能产出的应用应包含缓存)。
  3. 自检缓存与产物:用 usage.cache_read_input_tokens 验证缓存是否命中。

指令(默认值,除非用户另有要求)

示例

最小 Python 调用(注意 response.content 是内容块列表,需判 .type):

import anthropic
client = anthropic.Anthropic()  # 读 ANTHROPIC_API_KEY

response = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=16000,
    thinking={"type": "adaptive"},
    messages=[{"role": "user", "content": "法国的首都是哪里?"}],
)
for block in response.content:
    if block.type == "text":
        print(block.text)

提示缓存(前缀匹配,渲染序为 tools → system → messages,把稳定内容放前、易变内容放最后一个断点之后):

response = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=16000,
    system=[{"type": "text", "text": "<大段共享系统提示>",
             "cache_control": {"type": "ephemeral"}}],
    messages=[{"role": "user", "content": "<每次都变的问题>"}],
)
print(response.usage.cache_read_input_tokens)  # 跨请求若恒为 0 说明有静默失效源

注意事项

互见

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.