Claude

返回工具

Anthropic 研发的大语言模型系列,以安全性、可解释性和长上下文处理著称。基于 Constitutional AI 训练方法,强调有帮助、无害、诚实(HHH)。


模型系列

Claude 4.x(当前最新)

模型Model ID特点
Claude Opus 4.7claude-opus-4-7最强推理,复杂任务首选
Claude Sonnet 4.6claude-sonnet-4-6最佳编码模型,性价比高
Claude Haiku 4.5claude-haiku-4-5-20251001轻量快速,高频调用场景

历代模型演进

系列亮点上下文窗口
Claude 2长上下文突破100K
Claude 3(Haiku/Sonnet/Opus)多模态,三档定价200K
Claude 3.5代码能力大幅提升200K
Claude 3.7引入扩展思考(Extended Thinking)200K
Claude 4.x当前主力,推理+编码全面提升200K+

核心能力

长上下文

200K token 上下文窗口,可处理整本书的问答、大型代码库分析、长文档摘要。

Token 估算:中文约 1.5~2 字/token,英文约 0.75 词/token

扩展思考(Extended Thinking)

Claude 3.7+ 支持,模型在回答前进行深层内部推理,适合数学推导、复杂逻辑、多步骤规划。

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=16000,
    thinking={
        "type": "enabled",
        "budget_tokens": 10000
    },
    messages=[{"role": "user", "content": "解释量子纠缠"}]
)

多模态

支持图像输入(PNG/JPEG/GIF/WebP),可分析图表、截图、文档扫描件、UI 设计图。

工具使用(Tool Use)

tools = [{
    "name": "get_weather",
    "description": "获取指定城市的当前天气",
    "input_schema": {
        "type": "object",
        "properties": {
            "city": {"type": "string", "description": "城市名称"}
        },
        "required": ["city"]
    }
}]
 
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user", "content": "北京今天天气怎么样?"}]
)

API 使用

安装

pip install anthropic

基础调用

import anthropic
 
client = anthropic.Anthropic(api_key="sk-ant-xxx")
 
message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "用 Python 写一个快速排序"}]
)
print(message.content[0].text)

流式输出

with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "写一篇短文"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Prompt 缓存(降低成本)

对长系统提示或反复使用的文档开启缓存,命中后费用降低约 90%,缓存 TTL 为 5 分钟。

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system=[{
        "type": "text",
        "text": "你是一个专业的代码审查助手...",
        "cache_control": {"type": "ephemeral"}
    }],
    messages=[{"role": "user", "content": "审查这段代码:..."}]
)

关键参数

参数建议值说明
temperature0.0代码、结构化输出
temperature0.7通用问答
temperature1.0创意写作
max_tokens按需设置输出上限,不影响输入

定价策略

模型成本适用场景
Haiku 4.5最低高频轻量任务
Sonnet 4.6中等日常开发主力
Opus 4.7最高复杂推理、高质量输出
  • 缓存命中:输入价格降低约 90%
  • 批量 API:异步处理,价格降低 50%

Constitutional AI

Anthropic 的核心训练方法:用一组原则引导模型自我批评和修正,再用 AI 反馈做强化学习,使模型在”有帮助”与”无害”之间取得平衡,而非单纯服从指令。


相关文档