Overview

Bifrost provides complete Anthropic API compatibility through protocol adaptation. The integration handles request transformation, response normalization, and error mapping between Anthropic’s Messages API specification and Bifrost’s internal processing pipeline. This integration enables you to utilize Bifrost’s features like governance, load balancing, semantic caching, multi-provider support, and more, all while preserving your existing Anthropic SDK-based architecture. Endpoint: /anthropic

Setup

import anthropic

# Configure client to use Bifrost
client = anthropic.Anthropic(
    base_url="http://localhost:8080/anthropic",
    api_key="dummy-key"  # Keys handled by Bifrost
)

# Make requests as usual
response = client.messages.create(
    model="claude-3-sonnet-20240229",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello!"}]
)

print(response.content[0].text)

Provider/Model Usage Examples

Use multiple providers through the same Anthropic SDK format by prefixing model names with the provider:
import anthropic

client = anthropic.Anthropic(
    base_url="http://localhost:8080/anthropic",
    api_key="dummy-key"
)

# Anthropic models (default)
anthropic_response = client.messages.create(
    model="claude-3-sonnet-20240229",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello from Claude!"}]
)

# OpenAI models via Anthropic SDK format
openai_response = client.messages.create(
    model="openai/gpt-4o-mini",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello from OpenAI!"}]
)

# Google Vertex models via Anthropic SDK format
vertex_response = client.messages.create(
    model="vertex/gemini-pro",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello from Gemini!"}]
)

# Azure OpenAI models
azure_response = client.messages.create(
    model="azure/gpt-4o",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello from Azure!"}]
)

# Local Ollama models
ollama_response = client.messages.create(
    model="ollama/llama3.1:8b",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello from Ollama!"}]
)

Adding Custom Headers

Pass custom headers required by Bifrost plugins (like governance, telemetry, etc.):
import anthropic

client = anthropic.Anthropic(
    base_url="http://localhost:8080/anthropic",
    api_key="dummy-key",
    default_headers={
        "x-bf-vk": "vk_12345",  # Virtual key for governance
        "x-bf-user-id": "user_789",  # User identification
        "x-bf-team-id": "team_456",  # Team identification
        "x-bf-trace-id": "trace_abc123",  # Request tracing
    }
)

response = client.messages.create(
    model="claude-3-sonnet-20240229",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello with custom headers!"}]
)

Using Direct Keys

Pass API keys directly in requests to bypass Bifrost’s load balancing. You can pass any provider’s API key (OpenAI, Anthropic, Mistral, etc.) since Bifrost only looks for Authorization or x-api-key headers. This requires the Allow Direct API keys option to be enabled in Bifrost configuration.
Learn more: See Quickstart Configuration for enabling direct API key usage.
import anthropic

# Using Anthropic's API key directly
client_with_direct_key = anthropic.Anthropic(
    base_url="http://localhost:8080/anthropic",
    api_key="sk-your-anthropic-key"  # Anthropic's API key works
)

anthropic_response = client_with_direct_key.messages.create(
    model="claude-3-sonnet-20240229",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello from Claude!"}]
)

# or pass different provider keys per request using headers
client = anthropic.Anthropic(
    base_url="http://localhost:8080/anthropic",
    api_key="dummy-key"
)

# Use Anthropic key for Claude
anthropic_response = client.messages.create(
    model="claude-3-sonnet-20240229",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello Claude!"}],
    extra_headers={
        "x-api-key": "sk-ant-your-anthropic-key"
    }
)

# Use OpenAI key for GPT models
openai_response = client.messages.create(
    model="openai/gpt-4o-mini",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello GPT!"}],
    extra_headers={
        "Authorization": "Bearer sk-your-openai-key"
    }
)

Supported Features

The Anthropic integration supports all features that are available in both the Anthropic SDK and Bifrost core functionality. If the Anthropic SDK supports a feature and Bifrost supports it, the integration will work seamlessly. 😄

Next Steps