Skip to main content

A production-ready Claude API proxy server

Project description

Claude API Proxy

A production-ready Claude API proxy server that enables seamless integration with OpenAI-compatible API providers.

Features

  • 🔄 Protocol Conversion: Complete Claude API to OpenAI API format conversion
  • 🚀 High Performance: Async FastAPI with connection pooling
  • 📡 Streaming Support: Real-time Server-Sent Events streaming
  • 🎯 Smart Model Mapping: Configurable model routing (Haiku→Small, Sonnet/Opus→Big)
  • 🔒 Secure: Optional API key validation and request authentication
  • 🐳 Docker Ready: Easy deployment with Docker/Docker Compose
  • 🔍 Observable: Health checks, logging, and error handling
  • 🛠️ Developer Friendly: Type-safe with Pydantic models

Quick Start

1. Installation

# Clone the repository
git clone <repository-url>
cd claude-proxy

# Install dependencies with uv (recommended)
uv sync

# Or install directly
uv pip install -e .

# Or with pip (traditional method)
pip install -e .

2. Configuration

Create a .env file:

# Required: Your OpenAI API key
OPENAI_API_KEY=sk-your-openai-key

# Optional: API configuration
OPENAI_BASE_URL=https://api.openai.com/v1

# Model mapping
BIG_MODEL=gpt-4o          # For Claude Sonnet/Opus requests
SMALL_MODEL=gpt-4o-mini   # For Claude Haiku requests

# Server settings
HOST=0.0.0.0
PORT=8082
LOG_LEVEL=INFO

# Optional: API key validation
ANTHROPIC_API_KEY=your-anthropic-key-for-validation

3. Run the Server

# With uv
uv run python app.py

# Or directly (after installing dependencies)
python app.py

# With Docker Compose
docker-compose up -d

4. Use with Claude Code

# Set the proxy URL and API key
export ANTHROPIC_BASE_URL=http://localhost:8082
export ANTHROPIC_API_KEY=any-value  # Or your validation key

# Use Claude Code as normal
claude

API Endpoints

Main Endpoints

  • POST /v1/messages - Chat completions (compatible with Claude API)
  • POST /v1/messages/count_tokens - Token counting
  • GET /health - Health check
  • GET /test-connection - Test connectivity to target API
  • GET / - API information

Model Mapping

The proxy automatically maps Claude models to your configured models:

Claude Model Maps To Environment Variable
claude-3-haiku* SMALL_MODEL Default: gpt-4o-mini
claude-3-sonnet* BIG_MODEL Default: gpt-4o
claude-3-opus* BIG_MODEL Default: gpt-4o
claude-sonnet-4* BIG_MODEL Default: gpt-4o

Usage Examples

Basic Chat Completion

import httpx

response = httpx.post(
    "http://localhost:8082/v1/messages",
    headers={"x-api-key": "your-api-key"},  # Optional if validation disabled
    json={
        "model": "claude-3-5-sonnet-20241022",
        "max_tokens": 100,
        "messages": [
            {"role": "user", "content": "Hello, world!"}
        ]
    }
)
print(response.json())

Streaming Chat

import httpx

with httpx.stream(
    "POST",
    "http://localhost:8082/v1/messages",
    headers={"x-api-key": "your-api-key"},
    json={
        "model": "claude-3-haiku",
        "max_tokens": 100,
        "stream": True,
        "messages": [
            {"role": "user", "content": "Tell me a story"}
        ]
    }
) as response:
    for line in response.iter_lines():
        if line.startswith("data: "):
            print(line[6:])  # Remove "data: " prefix

With System Prompt and Tools

response = httpx.post(
    "http://localhost:8082/v1/messages",
    headers={"x-api-key": "your-api-key"},
    json={
        "model": "claude-3-sonnet",
        "max_tokens": 200,
        "system": "You are a helpful assistant.",
        "messages": [
            {"role": "user", "content": "What's the weather like?"}
        ],
        "tools": [
            {
                "type": "function",
                "function": {
                    "name": "get_weather",
                    "description": "Get current weather",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "location": {"type": "string"}
                        }
                    }
                }
            }
        ]
    }
)

Provider Configuration

OpenAI

OPENAI_API_KEY=sk-your-openai-key
OPENAI_BASE_URL=https://api.openai.com/v1
BIG_MODEL=gpt-4o
SMALL_MODEL=gpt-4o-mini

Azure OpenAI

OPENAI_API_KEY=your-azure-key
OPENAI_BASE_URL=https://your-resource.openai.azure.com/openai/deployments/your-deployment
BIG_MODEL=gpt-4
SMALL_MODEL=gpt-35-turbo

Local Models (Ollama)

OPENAI_API_KEY=dummy-key  # Required but can be dummy
OPENAI_BASE_URL=http://localhost:11434/v1
BIG_MODEL=llama3.1:70b
SMALL_MODEL=llama3.1:8b

Development

Running Tests

# Run all tests
uv run pytest

# Run with coverage
uv run pytest --cov=. --cov-report=html

# Run specific test file
uv run pytest tests/test_app.py -v

Code Quality

# Format code
uv run black .

# Lint code
uv run ruff check .

# Type checking
uv run mypy .

Project Structure

claude-proxy/
├── app.py                 # FastAPI application
├── config.py             # Configuration management
├── utils.py              # Utility functions
├── models/               # Data models
│   ├── claude.py         # Claude API models
│   └── openai.py         # OpenAI API models
├── providers/            # LLM provider implementations
│   ├── base.py          # Base provider class
│   ├── openai.py        # OpenAI provider
│   └── anthropic.py     # Anthropic provider (pass-through)
└── tests/               # Test suite

Deployment

Docker

# Build image
docker build -t claude-proxy .

# Run container
docker run -p 8082:8082 --env-file .env claude-proxy

Docker Compose

# Create .env file with your configuration
cp .env.example .env

# Start services
docker-compose up -d

# View logs
docker-compose logs -f

Production Considerations

  • Use a reverse proxy (Nginx/Traefik) for HTTPS termination
  • Set up monitoring and logging
  • Configure resource limits and health checks
  • Use secrets management for API keys
  • Enable API key validation in production

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Run the test suite
  6. Submit a pull request

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

claude_proxy-0.1.0.tar.gz (75.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

claude_proxy-0.1.0-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file claude_proxy-0.1.0.tar.gz.

File metadata

  • Download URL: claude_proxy-0.1.0.tar.gz
  • Upload date:
  • Size: 75.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for claude_proxy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 495557d48e976899303e9d911280291095379224b9192ef68c3bfb2f563d48e3
MD5 cfe3d408a8a20e0b07e3892bc09b35c5
BLAKE2b-256 9208e2a8b5e1369ca4df85de5f3a47091899df776cdc5cf9e7690fc888650e4f

See more details on using hashes here.

File details

Details for the file claude_proxy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: claude_proxy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for claude_proxy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 866915e1ef02bfc8473be7eb0a04b7d8eb8d3c7a269825433f268ab5e2003200
MD5 a0318092c35c143f8087f4ea581476b1
BLAKE2b-256 91ab594defa26238b13020280319ae6d3e6937f73e28d8b7c5c827753a93e031

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page