import os
from anthropic import Anthropic
client = Anthropic(base_url="https://api.naga.ac", api_key=os.environ["NAGAAI_API_KEY"])
with client.messages.stream(
model="claude-sonnet-5",
max_tokens=512,
messages=[{"role": "user", "content": "Explain backpressure in three sentences."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print("\n", stream.get_final_message().usage)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ baseURL: "https://api.naga.ac", apiKey: process.env.NAGAAI_API_KEY });
const stream = client.messages.stream({
model: "claude-sonnet-5",
max_tokens: 512,
messages: [{ role: "user", content: "Explain backpressure in three sentences." }],
});
stream.on("text", (text) => process.stdout.write(text));
const final = await stream.finalMessage();
console.log("\n", final.usage);
curl -N https://api.naga.ac/v1/messages \
-H "x-api-key: $NAGAAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-5",
"max_tokens": 512,
"messages": [{"role": "user", "content": "Explain backpressure in three sentences."}],
"stream": true
}'
Events
| Event | Carries |
|---|---|
message_start | The message shell with id and model |
content_block_start | A new block: text, thinking, tool_use and others |
content_block_delta | text_delta, thinking_delta, signature_delta or input_json_delta for tool input |
content_block_stop | The end of a block |
message_delta | stop_reason and the final usage |
message_stop | The end of the stream |
event: error arrives with the Anthropic error shape, and the connection closes.