Anthropic SDK

Overview

While ModelStack uses an OpenAI-compatible API format, you can also use the Anthropic SDK by pointing it to ModelStack's API endpoint. This lets you use Anthropic-specific features while routing through ModelStack's unified billing and gateway.

info

We recommend using the OpenAI SDK for the best compatibility across all providers. The Anthropic SDK only works with Claude models.

Python

Installation

bash
pip install anthropic

Setup

python
import anthropic

client = anthropic.Anthropic(
    api_key="your_api_key",
    base_url="https://api.modelstack.cc"
)

Chat Completion

python
message = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain quantum computing in simple terms."}
    ]
)

print(message.content[0].text)

Streaming

python
with client.messages.stream(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a poem about coding."}]
) as stream:
    for text in stream.text_stream:
        print(text, end="")

TypeScript / Node.js

Installation

bash
npm install @anthropic-ai/sdk

Setup

typescript
import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic({
  apiKey: 'your_api_key',
  baseURL: 'https://api.modelstack.cc',
})

Chat Completion

typescript
const message = await client.messages.create({
  model: 'claude-sonnet-4-5',
  max_tokens: 1024,
  messages: [
    { role: 'user', content: 'Explain quantum computing in simple terms.' },
  ],
})

console.log(message.content[0].text)

Streaming

typescript
const stream = client.messages.stream({
  model: 'claude-sonnet-4-5',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Write a poem about coding.' }],
})

stream.on('text', (text) => {
  process.stdout.write(text)
})

await stream.finalMessage()

When to Use the Anthropic SDK

Use the Anthropic SDK when you:

  • Need Anthropic-specific features (tool use with Anthropic's format, prompt caching)
  • Are only using Claude models
  • Have existing Anthropic SDK code and want minimal changes

Use the OpenAI SDK when you:

  • Want to switch between providers (Claude, GPT, Gemini) without changing SDKs
  • Need a single SDK for all models
  • Want maximum flexibility