Anthropic SDK Integration

Use the official Anthropic SDK with Martian to access 200+ models from multiple providers through a single, unified interface.

Ensure you have your Martian API key from the Martian Dashboard before continuing.

Installation

Install the Anthropic SDK for your language:

pip install anthropic

Configuration

Configure the Anthropic SDK to use Martian's base URL:

Python

import anthropic

client = anthropic.Anthropic(
    base_url="https://api.withmartian.com/v1",
    api_key="MARTIAN_API_KEY"
)

Node.js / TypeScript

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

const client = new Anthropic({
  baseURL: 'https://api.withmartian.com/v1',
  apiKey: process.env.MARTIAN_API_KEY,
});

Basic Usage

Messages

Make a simple message request:

message = client.messages.create(
    model="anthropic/claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "What is Olympus Mons?"}
    ]
)

print(message.content[0].text)

Using Models from Different Providers

Access models from any provider using the Anthropic SDK:

# Use Anthropic models
message = client.messages.create(
    model="anthropic/claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

# Use OpenAI models
message = client.messages.create(
    model="openai/gpt-4.1-nano",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

# Use Google models
message = client.messages.create(
    model="google/gemini-2.5-flash",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

See the Available Models page for the complete list of supported models.

Advanced Features

Streaming Responses

stream = client.messages.create(
    model="anthropic/claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a story about Mars"}],
    stream=True
)

for chunk in stream:
    if chunk.type == "content_block_delta" and chunk.delta.type == "text_delta":
        print(chunk.delta.text, end="")

Prompt Caching

Use Anthropic's prompt caching to reduce costs and latency:

system_instruction = [
    {
        "type": "text",
        "text": "You are a helpful assistant."
    },
    {
        "type": "text",
        "text": "<large context document here>",
        "cache_control": {"type": "ephemeral"}
    }
]

# First request - creates cache
message1 = client.messages.create(
    model="anthropic/claude-sonnet-4-20250514",
    max_tokens=100,
    system=system_instruction,
    messages=[{"role": "user", "content": "Question 1"}]
)

# Second request - uses cache
message2 = client.messages.create(
    model="anthropic/claude-sonnet-4-20250514",
    max_tokens=100,
    system=system_instruction,  # Must be identical
    messages=[{"role": "user", "content": "Question 2"}]
)

Tool Use

response = client.messages.create(
    model="anthropic/claude-sonnet-4-20250514",
    max_tokens=1024,
    tools=[
        {
            "name": "get_weather",
            "description": "Get the current weather in a location",
            "input_schema": {
                "type": "object",
                "properties": {
                    "location": {"type": "string"}
                }
            }
        }
    ],
    messages=[
        {"role": "user", "content": "What's the weather in San Francisco?"}
    ]
)

for item in response.content:
    if item.type == "tool_use":
        print(f"Tool: {item.name}")
        print(f"Args: {item.input}")

Cost Optimization

Use the :cheap suffix for automatic cost optimization:

message = client.messages.create(
    model="anthropic/claude-sonnet-4-20250514:cheap",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Summarize this article..."}]
)

Error Handling

import anthropic

try:
    message = client.messages.create(
        model="anthropic/claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello!"}]
    )
except anthropic.AuthenticationError:
    print("Invalid API key")
except anthropic.RateLimitError:
    print("Rate limit exceeded")
except anthropic.APIError as e:
    print(f"API error: {e}")

Next Steps

View Available Models

Browse 200+ AI models from leading providers with real-time pricing.

Read more

Explore Advanced Features

Learn about streaming, caching, tool use, and cost optimization.

Read more

View Other Integrations

Explore other ways to integrate Martian with your development workflow.

Read more