流式输出
- 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=1024,
messages=[{"role": "user", "content": "写一首关于冬天的诗"}],
) as stream:
for text in stream.text_stream:
print(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: 1024,
messages: [{ role: "user", content: "写一首关于冬天的诗" }],
});
for await (const text of stream.textStream) {
process.stdout.write(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,
"stream": true,
"messages": [{"role": "user", "content": "写一首关于冬天的诗"}]
}'
获取最终结果
- 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=1024,
messages=[{"role": "user", "content": "你好"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
# 流结束后获取完整消息对象
final_message = stream.get_final_message()
print(f"\n总 Token 数: {final_message.usage.input_tokens + final_message.usage.output_tokens}")
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: 1024,
messages: [{ role: "user", content: "你好" }],
});
for await (const text of stream.textStream) {
process.stdout.write(text);
}
const finalMessage = await stream.getFinalMessage();
console.log(`\n总 Token 数: ${finalMessage.usage.input_tokens + finalMessage.usage.output_tokens}`);
# SSE 流式响应,每行以 data: 开头,最后一个事件包含 usage 信息
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,
"stream": true,
"messages": [{"role": "user", "content": "你好"}]
}'
异步流式输出
- Python
- JavaScript
- curl
import asyncio
import anthropic
client = anthropic.AsyncAnthropic(api_key="your-api-key", base_url="https://www.cheapertoken.work")
async def main():
async with client.messages.stream(
model="claude-opus-4-7",
max_tokens=1024,
messages=[{"role": "user", "content": "写一首诗"}],
) as stream:
async for text in stream.text_stream:
print(text, end="", flush=True)
asyncio.run(main())
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work" });
// Node.js 中 top-level await 即可,无需额外封装
const stream = await client.messages.stream({
model: "claude-opus-4-7",
max_tokens: 1024,
messages: [{ role: "user", content: "写一首诗" }],
});
for await (const text of stream.textStream) {
process.stdout.write(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,
"stream": true,
"messages": [{"role": "user", "content": "写一首诗"}]
}'