Claude Code Proxy

repository·main·Indexed 25 days ago

https://github.com/fuergaosi233/claude-code-proxy

A proxy server that converts Claude API requests into OpenAI-compatible API calls, enabling the Claude Code CLI to work with providers such as OpenAI, Azure OpenAI, and local models via Ollama. It features model mapping rules, custom HTTP header injection, and support for both streaming and non-streaming responses via the /v1/messages endpoint.

Tokens
5.2K
Snippets
24
Records
45
Agent score
83%

What's inside claude-code-proxy

  1. Deploy Claude Code Proxy as a systemd service

    main

    To run the proxy in a production environment on Linux, follow these steps to create a systemd service.

    1. Upload the binary to your server (e.g., /opt/claude-proxy/).
    2. Create a .env file at /opt/claude-proxy/.env containing your API keys and configuration.
    3. Create the service file at /etc/systemd/system/claude-proxy.service:
    [Unit]
    Description=Claude Code Proxy
    After=network.target
    
    [Service]
    Type=simple
    User=www-data
    WorkingDirectory=/opt/claude-proxy
    EnvironmentFile=/opt/claude-proxy/.env
    ExecStart=/opt/claude-proxy/claude-code-proxy-single
    Restart=always
    RestartSec=5
    
    [Install]
    WantedBy=multi-user.target
    1. Start the service:
    systemctl daemon-reload
    systemctl start claude-proxy
    systemctl enable claude-proxy
  2. Run the Claude Code Proxy binary

    main

    Once packaged, you can execute the binary using the following methods.

    Basic Execution:

    # Directory version
    ./dist/claude-code-proxy/claude-code-proxy
    
    # Single-file version
    ./dist/claude-code-proxy-single

    With Environment Variables: Required: OPENAI_API_KEY. Optional: ANTHROPIC_API_KEY, HOST, PORT, LOG_LEVEL.

    # Run in foreground
    OPENAI_API_KEY="your-key" ./claude-code-proxy
    
    # Run in background
    OPENAI_API_KEY="your-key" nohup ./claude-code-proxy &
    # Run in foreground
    OPENAI_API_KEY="your-key" ./claude-code-proxy
  3. Run the proxy server and use with Claude Code

    main
    1. Start the proxy server using python start_proxy.py.
    2. In a separate terminal, run the claude CLI while overriding the ANTHROPIC_BASE_URL to point to the proxy (defaulting to http://localhost:8082).
    # Start the proxy server
    python start_proxy.py
    
    # In another terminal, use with Claude Code
    ANTHROPIC_BASE_URL=http://localhost:8082 claude
  4. Use Claude Code with the Proxy

    main

    To route the Claude Code CLI through the proxy, set the ANTHROPIC_BASE_URL environment variable to the proxy's address (default is http://localhost:8082).

    If ANTHROPIC_API_KEY is NOT set in the proxy: You must provide any value for the key in your shell.

    If ANTHROPIC_API_KEY IS set in the proxy: You must provide the exact matching key in your shell for validation.

    # If ANTHROPIC_API_KEY is not set in the proxy:
    ANTHROPIC_BASE_URL=http://localhost:8082 ANTHROPIC_API_KEY="any-value" claude
    
    # If ANTHROPIC_API_KEY is set in the proxy:
    ANTHROPIC_BASE_URL=http://localhost:8082 ANTHROPIC_API_KEY="exact-matching-key" claude
  5. Start the Claude Code Proxy server

    main

    You can start the server using several methods:

    Directly with Python:

    python start_proxy.py

    Using UV:

    uv run claude-code-proxy

    Using Docker Compose:

    docker compose up -d
    # Direct run
    python start_proxy.py
    
    # Or with UV
    uv run claude-code-proxy
    
    # Or with docker compose
    docker compose up -d
  6. Configure Claude Code Proxy via .env

    main

    The application uses python-dotenv to automatically load environment variables from a .env file in the project root.

    1. Copy the template: cp .env.example .env
    2. Edit the .env file with your provider credentials and model settings.
    cp .env.example .env