扩展思考(Extended Thinking)
扩展思考让模型在回答前深度推理,适合复杂数学、逻辑和分析任务。
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://www.cheapertoken.work")
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=16000,
thinking={
"type": "enabled",
"budget_tokens": 10000
},
messages=[{"role": "user", "content": "请证明根号2是无理数"}]
)
for block in response.content:
if block.type == "thinking":
print("思考过程:")
print(block.thinking)
elif block.type == "text":
print("回答:")
print(block.text)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work" });
const response = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 16000,
thinking: { type: "enabled", budget_tokens: 10000 },
messages: [{ role: "user", content: "请证明根号2是无理数" }],
});
for (const block of response.content) {
if (block.type === "thinking") {
console.log("思考过程:");
console.log(block.thinking);
} else if (block.type === "text") {
console.log("回答:");
console.log(block.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": 16000,
"thinking": {"type": "enabled", "budget_tokens": 10000},
"messages": [{"role": "user", "content": "请证明根号2是无理数"}]
}'
流式扩展思考
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://www.cheapertoken.work")
with client.messages.stream(
model="claude-opus-4-7",
max_tokens=16000,
thinking={"type": "enabled", "budget_tokens": 8000},
messages=[{"role": "user", "content": "分析这个商业计划的优缺点:..."}]
) as stream:
for event in stream:
if hasattr(event, "type"):
if event.type == "content_block_start" and hasattr(event.content_block, "type"):
if event.content_block.type == "thinking":
print("\n[思考中...]")
elif event.type == "content_block_delta":
if hasattr(event.delta, "thinking"):
print(event.delta.thinking, end="", flush=True)
elif hasattr(event.delta, "text"):
print(event.delta.text, end="", flush=True)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work" });
const stream = await client.messages.stream({
model: "claude-opus-4-7",
max_tokens: 16000,
thinking: { type: "enabled", budget_tokens: 8000 },
messages: [{ role: "user", content: "分析这个商业计划的优缺点:..." }],
});
for await (const event of stream) {
if (event.type === "content_block_start" && event.content_block?.type === "thinking") {
console.log("\n[思考中...]");
} else if (event.type === "content_block_delta") {
if (event.delta.type === "thinking_delta") {
process.stdout.write(event.delta.thinking);
} else if (event.delta.type === "text_delta") {
process.stdout.write(event.delta.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": 16000,
"stream": true,
"thinking": {"type": "enabled", "budget_tokens": 8000},
"messages": [{"role": "user", "content": "分析这个商业计划的优缺点:..."}]
}'
budget_tokens 最小 1024,max_tokens 必须大于 budget_tokens。