Streaming Output
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://aisupermarket.work")
with client.messages.stream(
model="claude-opus-4-7",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a poem about winter"}],
) 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://aisupermarket.work" });
const stream = await client.messages.stream({
model: "claude-opus-4-7",
max_tokens: 1024,
messages: [{ role: "user", content: "Write a poem about winter" }],
});
for await (const text of stream.textStream) {
process.stdout.write(text);
}
curl https://aisupermarket.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": "Write a poem about winter"}]
}'
Get the Final Result
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://aisupermarket.work")
with client.messages.stream(
model="claude-opus-4-7",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
# Get the full message object after the stream ends
final_message = stream.get_final_message()
print(f"\nTotal token count: {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://aisupermarket.work" });
const stream = await client.messages.stream({
model: "claude-opus-4-7",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello" }],
});
for await (const text of stream.textStream) {
process.stdout.write(text);
}
const finalMessage = await stream.getFinalMessage();
console.log(`\nTotal token count: ${finalMessage.usage.input_tokens + finalMessage.usage.output_tokens}`);
# SSE streaming response. Each line starts with data:, and the final event includes usage information
curl https://aisupermarket.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": "Hello"}]
}'
Async Streaming
- Python
- JavaScript
- curl
import asyncio
import anthropic
client = anthropic.AsyncAnthropic(api_key="your-api-key", base_url="https://aisupermarket.work")
async def main():
async with client.messages.stream(
model="claude-opus-4-7",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a poem"}],
) 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://aisupermarket.work" });
// In Node.js, top-level await is enough; no extra wrapper is required
const stream = await client.messages.stream({
model: "claude-opus-4-7",
max_tokens: 1024,
messages: [{ role: "user", content: "Write a poem" }],
});
for await (const text of stream.textStream) {
process.stdout.write(text);
}
curl https://aisupermarket.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": "Write a poem"}]
}'