Anthropic SDK

Use the official Anthropic SDK with Ambient. Point base_url at api.ambient.xyz and use the Messages API, including thinking blocks and streaming.

Ambient implements the Anthropic Messages format at POST /v1/messages (plus POST /v1/messages/count_tokens), so the official Anthropic SDK works by swapping the base URL.

Prerequisites#

  1. An Ambient API key from app.ambient.xyz/keys
  2. The Anthropic SDK installed for your language

Python#

Install:

pip install anthropic

Usage#

ambient_anthropic.pypython
import anthropic
 
client = anthropic.Anthropic(
    base_url="https://api.ambient.xyz",
    api_key="your-ambient-api-key",
)
 
message = client.messages.create(
    model="ambient/large",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, how are you?"}
    ],
)
 
for block in message.content:
    if block.type == "thinking":
        print("Reasoning:", block.thinking)
    elif block.type == "text":
        print("Response:", block.text)

In the Messages format, a model's reasoning arrives as thinking content blocks alongside the text blocks, so there is only one place to look for it.

Streaming#

ambient_anthropic_stream.pypython
import anthropic
 
client = anthropic.Anthropic(
    base_url="https://api.ambient.xyz",
    api_key="your-ambient-api-key",
)
 
with client.messages.stream(
    model="ambient/large",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Write a short poem about coding."}
    ],
) as stream:
    for event in stream:
        if event.type == "content_block_delta":
            if event.delta.type == "thinking_delta":
                print(event.delta.thinking, end="", flush=True)
            elif event.delta.type == "text_delta":
                print(event.delta.text, end="", flush=True)

JavaScript / TypeScript#

Install:

npm install @anthropic-ai/sdk

Usage#

ambient-anthropic.jsjavascript
import Anthropic from "@anthropic-ai/sdk";
 
const client = new Anthropic({
  baseURL: "https://api.ambient.xyz",
  apiKey: "your-ambient-api-key",
});
 
const message = await client.messages.create({
  model: "ambient/large",
  max_tokens: 1024,
  messages: [
    { role: "user", content: "Hello, how are you?" }
  ],
});
 
for (const block of message.content) {
  if (block.type === "thinking") {
    console.log("Reasoning:", block.thinking);
  } else if (block.type === "text") {
    console.log("Response:", block.text);
  }
}

Streaming#

ambient-anthropic-stream.jsjavascript
import Anthropic from "@anthropic-ai/sdk";
 
const client = new Anthropic({
  baseURL: "https://api.ambient.xyz",
  apiKey: "your-ambient-api-key",
});
 
const stream = client.messages.stream({
  model: "ambient/large",
  max_tokens: 1024,
  messages: [
    { role: "user", content: "Write a short poem about coding." }
  ],
});
 
for await (const event of stream) {
  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);
    }
  }
}

Environment variables#

You can also configure the SDK through environment variables:

export ANTHROPIC_BASE_URL=https://api.ambient.xyz
export ANTHROPIC_API_KEY=your-ambient-api-key

Then instantiate the client without arguments:

# Python
client = anthropic.Anthropic()
// JavaScript
const client = new Anthropic();

Resources#