Claude API 使用文档
Anthropic Claude 是由 Anthropic 开发的大语言模型,以安全性和有用性著称。
安装
- Python
- JavaScript
- curl
pip install anthropic
npm install @anthropic-ai/sdk
# curl 无需安装,系统自带
基础用法
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(
api_key="your-api-key",
base_url="https://www.cheapertoken.work",
)
message = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
{"role": "user", "content": "你好,请介绍一下你自己"}
]
)
print(message.content[0].text)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: "your-api-key",
baseURL: "https://www.cheapertoken.work",
});
const message = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
messages: [{ role: "user", content: "你好,请介绍一下你自己" }],
});
console.log(message.content[0].text);
curl https://www.cheapertoken.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "你好,请介绍一下你自己"}]
}'
常用模型
| 模型 | 说明 |
|---|---|
claude-opus-4-7 | 最强能力,适合复杂任务、代码、智能体 |
claude-opus-4-6 | 上一代旗舰模型 |
claude-sonnet-4-6 | 速度与能力平衡,适合大多数场景 |