### Install Dependencies with UV Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Install project dependencies using the UV package manager. This is the recommended method for faster installation. ```bash uv sync ``` -------------------------------- ### Install Dependencies with UV or Pip Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/QUICKSTART.md Install project dependencies using either UV (recommended) or pip. Ensure you have Python 3.9+ installed. ```bash # Using UV (recommended) uv sync # Or using pip pip install -r requirements.txt ``` -------------------------------- ### Start Proxy Server with UV Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Start the proxy server using the UV command-line tool for efficient execution. ```bash uv run claude-code-proxy ``` -------------------------------- ### Basic Configuration and Custom Headers Example Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Example demonstrating basic configuration for OpenAI API key and base URL, along with enabling custom headers like Accept, Content-Type, User-Agent, and Authorization. ```bash # Basic configuration OPENAI_API_KEY="sk-your-openai-api-key-here" OPENAI_BASE_URL="https://api.openai.com/v1" # Enable custom headers (uncomment as needed) CUSTOM_HEADER_ACCEPT="application/jsonstream" CUSTOM_HEADER_CONTENT_TYPE="application/json" CUSTOM_HEADER_USER_AGENT="my-app/1.0.0" CUSTOM_HEADER_AUTHORIZATION="Bearer my-token" ``` -------------------------------- ### Install PyInstaller with uv Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Installs the PyInstaller tool for packaging. Ensure uv is set up in your development environment. ```bash # Install PyInstaller using uv uv add --dev pyinstaller ``` -------------------------------- ### Install Dependencies with Pip Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Install project dependencies using pip. Ensure you have a requirements.txt file in your project. ```bash pip install -r requirements.txt ``` -------------------------------- ### Starting and Using Claude Code with Proxy Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Instructions for starting the proxy and then using the Claude Code CLI with the proxy as the backend. The ANTHROPIC_BASE_URL environment variable directs Claude Code to the proxy. ```bash # Start the proxy python start_proxy.py # Use Claude Code with the proxy ANTHROPIC_BASE_URL=http://localhost:8082 claude # Or set permanently export ANTHROPIC_BASE_URL=http://localhost:8082 claude ``` -------------------------------- ### Start Proxy Server Directly Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Run the proxy server directly using the Python interpreter. ```bash python start_proxy.py ``` -------------------------------- ### Model Mapping Examples Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Examples illustrating how Claude model names are mapped to configured OpenAI-compatible models based on naming patterns and how OpenAI models pass through unchanged. ```python # Example mappings in requests "claude-3-5-haiku-20241022" -> SMALL_MODEL (gpt-4o-mini) "claude-3-5-sonnet-20241022" -> MIDDLE_MODEL (gpt-4o) "claude-3-opus-20240229" -> BIG_MODEL (gpt-4o) # OpenAI models pass through unchanged "gpt-4o" -> "gpt-4o" "o1-mini" -> "o1-mini" ``` -------------------------------- ### Configure OpenAI Provider Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/QUICKSTART.md Copy the example environment file and edit it to set your OpenAI API key and desired models. This configuration is for using OpenAI directly. ```bash cp .env.example .env # Edit .env: # OPENAI_API_KEY="sk-your-openai-key" # BIG_MODEL="gpt-4o" # SMALL_MODEL="gpt-4o-mini" ``` -------------------------------- ### Configure Environment Variables Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Copy the example environment file and edit it to add your API configuration. Environment variables are automatically loaded from the .env file. ```bash cp .env.example .env # Edit .env and add your API configuration # Note: Environment variables are automatically loaded from .env file ``` -------------------------------- ### Start and Enable Systemd Service Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Reloads systemd, starts the Claude Code Proxy service, and enables it to launch on boot. ```bash # 4. Start service systemctl daemon-reload systemctl start claude-proxy systemctl enable claude-proxy ``` -------------------------------- ### Configure Local Models (Ollama) Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/QUICKSTART.md Copy the example environment file and edit it to set a dummy API key, the Ollama base URL, and your desired local models. This configuration is for using Ollama for local model inference. ```bash cp .env.example .env # Edit .env: # OPENAI_API_KEY="dummy-key" # OPENAI_BASE_URL="http://localhost:11434/v1" # BIG_MODEL="llama3.1:70b" # SMALL_MODEL="llama3.1:8b" ``` -------------------------------- ### Test Claude to OpenAI Proxy Setup Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/QUICKSTART.md Run this command to perform a quick test of your Claude Code to OpenAI proxy setup. This verifies that the proxy is correctly routing requests. ```bash # Quick test python src/test_claude_to_openai.py ``` -------------------------------- ### Start Proxy Server with Docker Compose Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Deploy and run the proxy server in detached mode using Docker Compose. ```bash docker compose up -d ``` -------------------------------- ### Start Claude Code Proxy Server Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Launch the proxy server using the provided startup scripts or the UV package manager. Docker Compose is also an option for deployment. ```bash python start_proxy.py ``` ```bash uv run claude-code-proxy ``` ```bash docker compose up -d ``` -------------------------------- ### Configure Azure OpenAI Provider Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/QUICKSTART.md Copy the example environment file and edit it to set your Azure OpenAI API key, base URL, and desired models. This configuration is for using Azure OpenAI services. ```bash cp .env.example .env # Edit .env: # 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" ``` -------------------------------- ### Start Server in Foreground Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Runs the Claude Code Proxy in the foreground, requiring an API key to be set. ```bash # Run in foreground OPENAI_API_KEY="your-key" ./claude-code-proxy ``` -------------------------------- ### Start Proxy Server and Use Claude Code Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/QUICKSTART.md Start the proxy server by running the Python script. Then, in a separate terminal, set the ANTHROPIC_BASE_URL environment variable to the proxy's address to use Claude Code with the proxy. ```bash # Start the proxy server python start_proxy.py # In another terminal, use with Claude Code ANTHROPIC_BASE_URL=http://localhost:8082 claude ``` -------------------------------- ### GET / - Root Endpoint Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Get proxy server information and available endpoints. ```APIDOC ## GET / - Root Endpoint ### Description Get proxy server information and available endpoints. ### Method GET ### Endpoint / ### Response #### Success Response (200) - **message** (string) - A message indicating the proxy server status and version. - **status** (string) - The current status of the proxy server (e.g., "running"). - **config** (object) - Configuration details of the proxy server. - **openai_base_url** (string) - The base URL for the OpenAI API. - **max_tokens_limit** (integer) - The maximum token limit for requests. - **api_key_configured** (boolean) - Indicates if an API key is configured. - **client_api_key_validation** (boolean) - Indicates if client API key validation is enabled. - **big_model** (string) - The name of the big model configured. - **small_model** (string) - The name of the small model configured. - **endpoints** (object) - A map of available endpoints. - **messages** (string) - The endpoint for message-related operations. - **count_tokens** (string) - The endpoint for token counting. - **health** (string) - The endpoint for health checks. - **test_connection** (string) - The endpoint for testing API connectivity. #### Response Example ```json { "message": "Claude-to-OpenAI API Proxy v1.0.0", "status": "running", "config": { "openai_base_url": "https://api.openai.com/v1", "max_tokens_limit": 4096, "api_key_configured": true, "client_api_key_validation": true, "big_model": "gpt-4o", "small_model": "gpt-4o-mini" }, "endpoints": { "messages": "/v1/messages", "count_tokens": "/v1/messages/count_tokens", "health": "/health", "test_connection": "/test-connection" } } ``` ``` -------------------------------- ### Build with UPX Compression Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md For Windows users, this command uses PyInstaller with UPX compression to reduce the size of the single-file executable. Ensure UPX is installed and its path is correctly specified. ```bash pyinstaller --onefile --upx-dir=/path/to/upx src/main.py ``` -------------------------------- ### GET /test-connection - Test API Connectivity Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Verify the proxy can successfully connect to the configured OpenAI-compatible API. ```APIDOC ## GET /test-connection - Test API Connectivity ### Description Verify the proxy can successfully connect to the configured OpenAI-compatible API. ### Method GET ### Endpoint /test-connection ### Response #### Success Response (200) - **status** (string) - The status of the connection (e.g., "success"). - **message** (string) - A message indicating the connection status. - **model_used** (string) - The model used for testing the connection. - **timestamp** (string) - The timestamp of the test. - **response_id** (string) - The ID of the response from the API. #### Failure Response (503) - **status** (string) - The status of the connection (e.g., "failed"). - **error_type** (string) - The type of error encountered. - **message** (string) - A detailed error message. - **suggestions** (array) - A list of suggestions to resolve the issue. #### Success Response Example ```json { "status": "success", "message": "Successfully connected to OpenAI API", "model_used": "gpt-4o-mini", "timestamp": "2024-01-15T10:30:00.123456", "response_id": "chatcmpl-abc123" } ``` #### Failure Response Example ```json { "status": "failed", "error_type": "API Error", "message": "Invalid API key...", "suggestions": [ "Check your OPENAI_API_KEY is valid", "Verify your API key has the necessary permissions" ] } ``` ``` -------------------------------- ### Basic Chat Request with httpx Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Example of making a basic chat request to the proxy using the httpx library in Python. The 'model' field maps to the configured MIDDLE_MODEL. ```python import httpx response = httpx.post( "http://localhost:8082/v1/messages", json={ "model": "claude-3-5-sonnet-20241022", # Maps to MIDDLE_MODEL "max_tokens": 100, "messages": [ {"role": "user", "content": "Hello!"} ] } ) ``` -------------------------------- ### Run with Different Port Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md If the default port is in use, you can specify a different port by setting the `PORT` environment variable before running the proxy. This example uses port 8083. ```bash PORT=8083 ./claude-code-proxy ``` -------------------------------- ### Start Server in Background Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Runs the Claude Code Proxy in the background using nohup, ensuring it continues to run after the terminal session ends. ```bash # Run in background OPENAI_API_KEY="your-key" nohup ./claude-code-proxy & ``` -------------------------------- ### GET /health - Health Check Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Check proxy server status and configuration. ```APIDOC ## GET /health - Health Check ### Description Check proxy server status and configuration. ### Method GET ### Endpoint /health ### Response #### Success Response (200) - **status** (string) - The status of the proxy server (e.g., "healthy"). - **timestamp** (string) - The timestamp of the health check. - **openai_api_configured** (boolean) - Indicates if the OpenAI API is configured. - **api_key_valid** (boolean) - Indicates if the API key is valid. - **client_api_key_validation** (boolean) - Indicates if client API key validation is enabled. #### Response Example ```json { "status": "healthy", "timestamp": "2024-01-15T10:30:00.123456", "openai_api_configured": true, "api_key_valid": true, "client_api_key_validation": true } ``` ``` -------------------------------- ### GET / - Root Endpoint Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Retrieve information about the proxy server, including its version, status, configuration details, and available API endpoints. This is the primary endpoint for understanding the proxy's capabilities. ```bash curl http://localhost:8082/ # Output: # { # "message": "Claude-to-OpenAI API Proxy v1.0.0", # "status": "running", # "config": { # "openai_base_url": "https://api.openai.com/v1", # "max_tokens_limit": 4096, # "api_key_configured": true, # "client_api_key_validation": true, # "big_model": "gpt-4o", # "small_model": "gpt-4o-mini" # }, # "endpoints": { # "messages": "/v1/messages", # "count_tokens": "/v1/messages/count_tokens", # "health": "/health", # "test_connection": "/test-connection" # } # } ``` -------------------------------- ### GET /test-connection - Test API Connectivity Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Verify that the proxy server can successfully connect to the configured OpenAI-compatible API. This endpoint provides success or failure messages, including details about the model used and potential errors. ```bash curl http://localhost:8082/test-connection # Success Output: # { # "status": "success", # "message": "Successfully connected to OpenAI API", # "model_used": "gpt-4o-mini", # "timestamp": "2024-01-15T10:30:00.123456", # "response_id": "chatcmpl-abc123" # } # Failure Output (503): # { # "status": "failed", # "error_type": "API Error", # "message": "Invalid API key...", # "suggestions": [ # "Check your OPENAI_API_KEY is valid", # "Verify your API key has the necessary permissions" # ] # } ``` -------------------------------- ### Development Workflow with UV Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Commands for managing dependencies, running the server, formatting code, and performing type checking using the 'uv' tool. ```bash # Install dependencies uv sync # Run server uv run claude-code-proxy # Format code uv run black src/ uv run isort src/ # Type checking uv run mypy src/ ``` -------------------------------- ### Cross-platform Packaging: Windows Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Builds a single-file executable for Windows systems. The output is a Windows executable file. ```bash # Build on Windows system pyinstaller --onefile src/main.py # Output: claude-code-proxy-single.exe ``` -------------------------------- ### Deploy Binary via SCP Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Copies the single-file executable to the target server using SCP for deployment. ```bash # 1. Upload binary file to server scp claude-code-proxy-single user@server:/opt/claude-proxy/ ``` -------------------------------- ### Single-file Version Packaging Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Packages the application into a single standalone executable. Recommended for deployment. ```bash # Quick single-file packaging uv run pyinstaller --onefile --name claude-code-proxy-single src/main.py ``` -------------------------------- ### Directory Version Packaging Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Packages the application into a directory structure with an executable and dependencies. Recommended for development and testing. ```bash # Use custom spec file uv run pyinstaller claude-proxy.spec ``` -------------------------------- ### Configure Proxy Environment Variables Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Set up the proxy's behavior by defining environment variables in a .env file. This includes API keys, base URLs, model mappings, and server settings. ```bash # Required: Your OpenAI API key OPENAI_API_KEY="sk-your-openai-api-key-here" # Optional: Client validation (if set, clients must provide this exact key) ANTHROPIC_API_KEY="your-expected-anthropic-api-key" # Optional: API endpoint (default: https://api.openai.com/v1) OPENAI_BASE_URL="https://api.openai.com/v1" # Model mappings for Claude model names BIG_MODEL="gpt-4o" # Used for Claude opus requests MIDDLE_MODEL="gpt-4o" # Used for Claude sonnet requests SMALL_MODEL="gpt-4o-mini" # Used for Claude haiku requests # Server settings HOST="0.0.0.0" PORT="8082" LOG_LEVEL="INFO" # Performance settings MAX_TOKENS_LIMIT="4096" MIN_TOKENS_LIMIT="4096" REQUEST_TIMEOUT="90" ``` -------------------------------- ### GET /health - Health Check Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Check the status of the proxy server and its configuration. This endpoint returns a JSON object indicating if the server is healthy and details about its connection to the OpenAI API. ```bash curl http://localhost:8082/health # Output: # { # "status": "healthy", # "timestamp": "2024-01-15T10:30:00.123456", # "openai_api_configured": true, # "api_key_valid": true, # "client_api_key_validation": true # } ``` -------------------------------- ### OpenAI Provider Configuration Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Configure environment variables for using OpenAI as the backend. Ensure your API key and base URL are correctly set. ```bash OPENAI_API_KEY="sk-your-openai-key" OPENAI_BASE_URL="https://api.openai.com/v1" BIG_MODEL="gpt-4o" MIDDLE_MODEL="gpt-4o" SMALL_MODEL="gpt-4o-mini" ``` -------------------------------- ### View Help Information Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Displays the help message and available command-line options for the Claude Code Proxy executable. ```bash ./claude-code-proxy --help ``` -------------------------------- ### Cross-platform Packaging: Linux Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Builds a single-file executable for Linux systems. The output is a Linux ELF executable. ```bash # Build on Linux system uv run pyinstaller --onefile src/main.py # Output: claude-code-proxy-single (Linux ELF) ``` -------------------------------- ### Custom Packaging: Include Data Files Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Packages the application as a single file, ensuring that data files from the 'src' directory are included. ```bash # Include data files uv run pyinstaller --onefile --add-data="src:src" src/main.py ``` -------------------------------- ### Cross-platform Packaging: macOS Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Builds a single-file executable for macOS systems. The output is a macOS executable. ```bash # Build on macOS system pyinstaller --onefile src/main.py # Output: claude-code-proxy-single (macOS executable) ``` -------------------------------- ### Create Systemd Service File Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Configures a systemd service unit file to manage the Claude Code Proxy as a background service. ```bash # 3. Create system service (systemd) cat > /etc/systemd/system/claude-proxy.service << EOF [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 EOF ``` -------------------------------- ### Running Comprehensive Tests Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Execute all tests for the proxy functionality, specifically focusing on Claude to OpenAI request transformations. ```bash # Run comprehensive tests python src/test_claude_to_openai.py ``` -------------------------------- ### Local Ollama Provider Configuration Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Configure environment variables for using local models via Ollama. A dummy API key is sufficient for local use. ```bash OPENAI_API_KEY="dummy-key" # Required but can be dummy OPENAI_BASE_URL="http://localhost:11434/v1" BIG_MODEL="llama3.1:70b" MIDDLE_MODEL="llama3.1:70b" SMALL_MODEL="llama3.1:8b" ``` -------------------------------- ### Set Python Optimization Flags Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md These environment variables can optimize Python startup and reduce memory usage. `PYTHONOPTIMIZE=2` enables compile-time optimizations, and `PYTHONDONTWRITEBYTECODE=1` prevents the creation of `.pyc` files. ```bash export PYTHONOPTIMIZE=2 ``` ```bash export PYTHONDONTWRITEBYTECODE=1 ``` -------------------------------- ### Custom Packaging: Include Modules Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Packages the application as a single file, explicitly including specific modules that PyInstaller might not detect automatically. ```bash # Include specific modules uv run pyinstaller --onefile --hidden-import=src.api.endpoints src/main.py ``` -------------------------------- ### Create Environment File for Service Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Defines environment variables for the Claude Code Proxy service by creating a .env file. ```bash # 2. Set environment variables cat > /opt/claude-proxy/.env << EOF OPENAI_API_KEY=sk-your-actual-key ANTHROPIC_API_KEY=your-anthropic-key HOST=0.0.0.0 PORT=8082 LOG_LEVEL=WARNING EOF ``` -------------------------------- ### Set Environment Variables for Execution Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Configures necessary environment variables, including API keys and server settings, before running the proxy. ```bash # Set required environment variables export OPENAI_API_KEY="your-api-key-here" # Optional environment variables export ANTHROPIC_API_KEY="anthropic-key" export HOST="0.0.0.0" export PORT="8082" export LOG_LEVEL="INFO" ``` -------------------------------- ### Execute Directory Version Binary Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Runs the Claude Code Proxy application from the directory-packaged version. ```bash # Directory version ./dist/claude-code-proxy/claude-code-proxy ``` -------------------------------- ### Ollama Local Models Configuration Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Configure the proxy to use locally running models via Ollama. The API key is required but can be any dummy value. ```env # .env file for Ollama OPENAI_API_KEY="dummy-key" # Required but can be any value OPENAI_BASE_URL="http://localhost:11434/v1" BIG_MODEL="llama3.1:70b" MIDDLE_MODEL="llama3.1:70b" SMALL_MODEL="llama3.1:8b" ``` -------------------------------- ### Preview Sed Replacements Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/CLAUDE.md Utilize the sed command with the -n option and the 's' command for substitution, along with the 'p' flag to print, to preview potential text replacements without modifying the file. The 'g' flag ensures all occurrences on a line are considered. ```bash sed -n "s/oldPattern/newPattern/gp" filename.js ``` -------------------------------- ### Azure OpenAI Provider Configuration Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Configure environment variables for using Azure OpenAI. Replace placeholders with your specific Azure resource details. ```bash OPENAI_API_KEY="your-azure-key" OPENAI_BASE_URL="https://your-resource.openai.azure.com/openai/deployments/your-deployment" BIG_MODEL="gpt-4" MIDDLE_MODEL="gpt-4" SMALL_MODEL="gpt-35-turbo" ``` -------------------------------- ### Use Claude Code with Proxy (API Key Set) Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Configure the ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY environment variables to use the Claude Code CLI with the proxy when the proxy expects an exact API key match. ```bash ANTHROPIC_BASE_URL=http://localhost:8082 ANTHROPIC_API_KEY="exact-matching-key" claude ``` -------------------------------- ### Rollback Service Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Commands to back up the current executable and then restore a previous version. After rollback, the service needs to be restarted. ```bash cp claude-code-proxy claude-code-proxy.bak ``` ```bash mv claude-code-proxy.bak claude-code-proxy ``` ```bash systemctl restart claude-proxy ``` -------------------------------- ### Execute Single-file Version Binary Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Runs the Claude Code Proxy application from the single-file packaged executable. ```bash # Single-file version ./dist/claude-code-proxy-single ``` -------------------------------- ### Use Claude Code with Proxy (API Key Not Set) Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Configure the ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY environment variables to use the Claude Code CLI with the proxy when the proxy does not have a specific API key set. ```bash ANTHROPIC_BASE_URL=http://localhost:8082 ANTHROPIC_API_KEY="any-value" claude ``` -------------------------------- ### Add Execution Permissions Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Use this command to add execution permissions to the `claude-code-proxy` binary. This is often required on Linux/macOS systems. ```bash chmod +x claude-code-proxy ``` -------------------------------- ### Locate Function Implementations with Grep Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/CLAUDE.md Employ Grep with the -A flag to display lines following a match, aiding in the identification of function or method implementations. Specify the filename for targeted searches. ```bash grep -n "function findTagBoundaries" -A 20 filename.js ``` -------------------------------- ### MultiEdit: Insert New Methods into Class Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/CLAUDE.md Use this strategy when inserting new methods into an existing class. The `old_string` should be the class's closing brace `}`. The `new_string` includes the new methods followed by the original closing brace. ```text old_string = "}" new_string = " // New method 1\n function method1() { ... }\n\n // New method 2\n function method2() { ... }\n}" ``` -------------------------------- ### View System Logs Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Monitor system logs for the `claude-proxy` service using `journalctl`. The `-f` flag follows the log output in real-time. ```bash journalctl -u claude-proxy -f ``` -------------------------------- ### Azure OpenAI Configuration Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Configure the proxy to use Azure OpenAI services by setting the API key, base URL, API version, and model names in the .env file. ```env OPENAI_API_KEY="your-azure-api-key" OPENAI_BASE_URL="https://your-resource.openai.azure.com/openai/deployments/your-deployment" AZURE_API_VERSION="2024-03-01-preview" BIG_MODEL="gpt-4" MIDDLE_MODEL="gpt-4" SMALL_MODEL="gpt-35-turbo" ``` -------------------------------- ### PyInstaller Analysis Configuration Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Defines the main configuration for PyInstaller's Analysis object, specifying entry points, search paths, and data files. ```python # Main configuration items explanation a = Analysis( ['src/main.py'], # Entry file pathex=['.'], # Search path binaries=[], # Binary files datas=[('src', 'src')], # Data files hiddenimports=[...], # Hidden import modules ) # Key hidden imports included hiddenimports=[ 'src.api.endpoints', 'src.core.config', 'src.core.client', 'uvicorn.logging', 'uvicorn.loops.auto', 'fastapi.openapi.utils', 'pydantic.v1', ] ``` -------------------------------- ### POST /v1/messages - Tool/Function Calling Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Use Claude's tool calling feature, which is converted to OpenAI function calling format. Ensure the 'tools' parameter is correctly defined with schema and 'tool_choice' is set to 'auto' or a specific tool. ```python import httpx response = httpx.post( "http://localhost:8082/v1/messages", headers={"Authorization": "Bearer your-anthropic-key"}, json={ "model": "claude-3-5-sonnet-20241022", "max_tokens": 1024, "messages": [ {"role": "user", "content": "What's the weather in San Francisco?"} ], "tools": [ { "name": "get_weather", "description": "Get current weather for a location", "input_schema": { "type": "object", "properties": { "location": {"type": "string", "description": "City name"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} }, "required": ["location"] } } ], "tool_choice": {"type": "auto"} } ) result = response.json() print(result) # Output: # { # "id": "msg_...", # "content": [ # {"type": "text", "text": "I'll check the weather for you."}, # { # "type": "tool_use", # "id": "tool_abc123", # "name": "get_weather", # "input": {"location": "San Francisco", "unit": "fahrenheit"} # } # ], # "stop_reason": "tool_use" # } ``` -------------------------------- ### Enable Detailed Logging Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Set the `LOG_LEVEL` environment variable to `DEBUG` to enable detailed logging for the Claude Code Proxy. This is useful for diagnosing issues. ```bash LOG_LEVEL=DEBUG ./claude-code-proxy ``` -------------------------------- ### Check Environment Variables Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Verify that necessary environment variables, such as those for OpenAI or Anthropic, are set correctly. This command filters environment variables containing 'OPENAI' or 'ANTHROPIC'. ```bash env | grep -E "(OPENAI|ANTHROPIC)" ``` -------------------------------- ### Enable Custom Headers for API Requests Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/README.md Configure custom headers for API requests by uncommenting and setting environment variables with the CUSTOM_HEADER_ prefix in your .env file. These headers are automatically converted to HTTP headers. ```bash # Uncomment to enable custom headers # CUSTOM_HEADER_ACCEPT="application/jsonstream" # CUSTOM_HEADER_CONTENT_TYPE="application/json" # CUSTOM_HEADER_USER_AGENT="your-app/1.0.0" # CUSTOM_HEADER_AUTHORIZATION="Bearer your-token" # CUSTOM_HEADER_X_API_KEY="your-api-key" # CUSTOM_HEADER_X_CLIENT_ID="your-client-id" # CUSTOM_HEADER_X_CLIENT_VERSION="1.0.0" # CUSTOM_HEADER_X_REQUEST_ID="unique-request-id" # CUSTOM_HEADER_X_TRACE_ID="trace-123" # CUSTOM_HEADER_X_SESSION_ID="session-456" ``` -------------------------------- ### POST /v1/messages - Create Message Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Send chat completion requests using the Claude API format. The proxy converts these requests to the OpenAI format and returns responses in the Claude format. ```APIDOC ## POST /v1/messages - Create Message ### Description Send chat completion requests using Claude API format. The proxy converts requests to OpenAI format and returns Claude-formatted responses. ### Method POST ### Endpoint /v1/messages ### Parameters #### Request Body - **model** (string) - Required - The Claude model name to use (e.g., `claude-3-5-sonnet-20241022`). This will be mapped to an OpenAI-compatible model. - **max_tokens** (integer) - Required - The maximum number of tokens to generate. - **messages** (array) - Required - An array of message objects representing the conversation history. - **role** (string) - Required - The role of the message sender (`user` or `assistant`). - **content** (string) - Required - The content of the message. - **system** (string) - Optional - A system prompt to guide the assistant's behavior. - **temperature** (number) - Optional - Controls randomness. Lower values make output more deterministic. ### Request Example ```json { "model": "claude-3-5-sonnet-20241022", "max_tokens": 1024, "messages": [ {"role": "user", "content": "Explain quantum computing in simple terms."} ], "system": "You are a helpful assistant that explains complex topics simply.", "temperature": 0.7 } ``` ### Response #### Success Response (200) - **id** (string) - Unique identifier for the message. - **type** (string) - Type of the response, typically `message`. - **role** (string) - The role of the sender, usually `assistant`. - **model** (string) - The model used for the response. - **content** (array) - An array of content blocks. - **type** (string) - Type of content block (e.g., `text`). - **text** (string) - The text content. - **stop_reason** (string) - The reason the generation stopped (e.g., `end_turn`). - **usage** (object) - Token usage statistics. - **input_tokens** (integer) - Number of input tokens. - **output_tokens** (integer) - Number of output tokens. #### Response Example ```json { "id": "msg_abc123...", "type": "message", "role": "assistant", "model": "claude-3-5-sonnet-20241022", "content": [{"type": "text", "text": "Quantum computing uses..."}], "stop_reason": "end_turn", "usage": {"input_tokens": 45, "output_tokens": 230} } ``` ``` -------------------------------- ### Find Process Using Port Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md Use `lsof` to identify which process is currently using a specific port, such as 8082. This helps in resolving 'Port Already in Use' errors. ```bash lsof -i :8082 ``` -------------------------------- ### Connect Claude Code CLI to Proxy Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Configure the Claude Code CLI to use the proxy by setting the ANTHROPIC_BASE_URL environment variable. This can be done temporarily for a single command or permanently in your shell profile. ```bash # If ANTHROPIC_API_KEY is not set in the proxy (no validation): ANTHROPIC_BASE_URL=http://localhost:8082 ANTHROPIC_API_KEY="any-value" claude # If ANTHROPIC_API_KEY is set in the proxy (validation enabled): ANTHROPIC_BASE_URL=http://localhost:8082 ANTHROPIC_API_KEY="exact-matching-key" claude # Or set permanently in your shell profile: export ANTHROPIC_BASE_URL=http://localhost:8082 export ANTHROPIC_API_KEY="your-key" claude ``` -------------------------------- ### POST /v1/messages - Tool/Function Calling Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Utilizes Claude's tool calling feature, which is converted to OpenAI function calling format. ```APIDOC ## POST /v1/messages - Tool/Function Calling ### Description Use Claude's tool calling feature which is converted to OpenAI function calling format. ### Method POST ### Endpoint /v1/messages ### Request Body - **model** (string) - Required - The model to use for generation. - **max_tokens** (integer) - Required - The maximum number of tokens to generate. - **messages** (array) - Required - The messages to send to the model. - **role** (string) - Required - The role of the message sender (e.g., "user", "assistant"). - **content** (string) - Required - The content of the message. - **tools** (array) - Optional - A list of tools the model can use. - **name** (string) - Required - The name of the tool. - **description** (string) - Optional - A description of the tool. - **input_schema** (object) - Optional - The JSON schema for the tool's input. - **tool_choice** (object) - Optional - Controls how the model chooses tools. - **type** (string) - Required - The type of tool choice (e.g., "auto"). ### Request Example ```json { "model": "claude-3-5-sonnet-20241022", "max_tokens": 1024, "messages": [ {"role": "user", "content": "What's the weather in San Francisco?"} ], "tools": [ { "name": "get_weather", "description": "Get current weather for a location", "input_schema": { "type": "object", "properties": { "location": {"type": "string", "description": "City name"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} }, "required": ["location"] } } ], "tool_choice": {"type": "auto"} } ``` ### Response #### Success Response (200) - **id** (string) - The ID of the message. - **content** (array) - The content of the message. - **type** (string) - The type of content (e.g., "text", "tool_use"). - **text** (string) - The text content of the message (if type is "text"). - **id** (string) - The ID of the tool use (if type is "tool_use"). - **name** (string) - The name of the tool used (if type is "tool_use"). - **input** (object) - The input provided to the tool (if type is "tool_use"). - **stop_reason** (string) - The reason the generation stopped (e.g., "tool_use"). #### Response Example ```json { "id": "msg_...", "content": [ {"type": "text", "text": "I'll check the weather for you."}, { "type": "tool_use", "id": "tool_abc123", "name": "get_weather", "input": {"location": "San Francisco", "unit": "fahrenheit"} } ], "stop_reason": "tool_use" } ``` ``` -------------------------------- ### POST /v1/messages - Streaming Response Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Enable real-time streaming responses using Server-Sent Events (SSE). This is useful for interactive applications where immediate feedback is desired. ```APIDOC ## POST /v1/messages - Streaming Response ### Description Enable real-time streaming responses using Server-Sent Events (SSE). This is useful for interactive applications where immediate feedback is desired. ### Method POST ### Endpoint /v1/messages ### Parameters #### Request Body - **model** (string) - Required - The Claude model name to use (e.g., `claude-3-5-haiku-20241022`). This will be mapped to an OpenAI-compatible model. - **max_tokens** (integer) - Required - The maximum number of tokens to generate. - **messages** (array) - Required - An array of message objects representing the conversation history. - **role** (string) - Required - The role of the message sender (`user` or `assistant`). - **content** (string) - Required - The content of the message. - **stream** (boolean) - Required - Set to `true` to enable streaming responses. ### Request Example ```python import httpx with httpx.stream( "POST", "http://localhost:8082/v1/messages", headers={"Authorization": "Bearer your-anthropic-key"}, json={ "model": "claude-3-5-haiku-20241022", "max_tokens": 512, "messages": [{"role": "user", "content": "Write a haiku about coding."}], "stream": True } ) as response: for line in response.iter_lines(): if line.startswith("data: "): print(line[6:]) ``` ### Response #### Success Response (200) Responses are streamed as Server-Sent Events (SSE). Each event contains a JSON payload. - **event**: `message_start` - **data**: `{"type": "message_start", "message": {"id": "msg_...", "role": "assistant", ...}}` - **event**: `content_block_start` - **data**: `{"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}` - **event**: `content_block_delta` - **data**: `{"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Lines of "}}` - **event**: `message_stop` - **data**: `{"type": "message_stop"}` #### Response Example (SSE events) ``` event: message_start data: {"type": "message_start", "message": {"id": "msg_...", "role": "assistant", ...}} event: content_block_start data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}} event: content_block_delta data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Lines of "}} event: content_block_delta data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "code..."}} event: message_stop data: {"type": "message_stop"} ``` ``` -------------------------------- ### POST /v1/messages - Multimodal Image Input Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Send images encoded in base64 format for vision-capable models. Ensure the image is read in binary mode and encoded to UTF-8. The 'content' in messages should be a list containing both the image object and text. ```python import httpx import base64 # Read and encode image with open("diagram.png", "rb") as f: image_data = base64.b64encode(f.read()).decode("utf-8") response = httpx.post( "http://localhost:8082/v1/messages", headers={"Authorization": "Bearer your-anthropic-key"}, json={ "model": "claude-3-5-sonnet-20241022", "max_tokens": 1024, "messages": [ { "role": "user", "content": [ { "type": "image", "source": { "type": "base64", "media_type": "image/png", "data": image_data } }, {"type": "text", "text": "Describe what you see in this image."} ] } ] } ) print(response.json()["content"][0]["text"]) ``` -------------------------------- ### POST /v1/messages - Non-streaming Request Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Send a chat completion request using the Claude API format to the proxy. The proxy converts it to OpenAI format and returns a Claude-formatted response. Ensure the correct model name is used for mapping. ```python import httpx # Non-streaming request response = httpx.post( "http://localhost:8082/v1/messages", headers={"Authorization": "Bearer your-anthropic-key"}, json={ "model": "claude-3-5-sonnet-20241022", # Maps to MIDDLE_MODEL "max_tokens": 1024, "messages": [ {"role": "user", "content": "Explain quantum computing in simple terms."} ], "system": "You are a helpful assistant that explains complex topics simply.", "temperature": 0.7 } ) print(response.json()) # Output: # { # "id": "msg_abc123...", # "type": "message", # "role": "assistant", # "model": "claude-3-5-sonnet-20241022", # "content": [{"type": "text", "text": "Quantum computing uses..."}], # "stop_reason": "end_turn", # "usage": {"input_tokens": 45, "output_tokens": 230} # } ``` -------------------------------- ### Dockerfile for Claude Code Proxy Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/BINARY_PACKAGING.md A Dockerfile to create a container image for the Claude Code Proxy, based on Alpine Linux. ```dockerfile FROM alpine:latest RUN apk add --no-cache libstdc++ COPY claude-code-proxy-single /usr/local/bin/claude-code-proxy EXPOSE 8082 CMD ["claude-code-proxy"] ``` -------------------------------- ### MultiEdit: Insert Function Call Source: https://github.com/fuergaosi233/claude-code-proxy/blob/main/CLAUDE.md Use this for inserting a function call within an existing method. The `old_string` is a stable line before the call site, and the `new_string` adds the call after that line. ```text old_string = "// existing line before call" new_string = "// existing line before call\n this.newMethodCall();" ``` -------------------------------- ### POST /v1/messages - Streaming Response Source: https://context7.com/fuergaosi233/claude-code-proxy/llms.txt Enable real-time streaming responses from the proxy using Server-Sent Events (SSE). Set `stream: True` in the JSON payload to receive events as they are generated. ```python import httpx # Streaming request with httpx.stream( "POST", "http://localhost:8082/v1/messages", headers={"Authorization": "Bearer your-anthropic-key"}, json={ "model": "claude-3-5-haiku-20241022", # Maps to SMALL_MODEL "max_tokens": 512, "messages": [{"role": "user", "content": "Write a haiku about coding."}], "stream": True } ) as response: for line in response.iter_lines(): if line.startswith("data: "): print(line[6:]) # Output (SSE events): event: message_start data: {"type": "message_start", "message": {"id": "msg_...", "role": "assistant", ...}} event: content_block_start data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}} event: content_block_delta data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Lines of "}} event: content_block_delta data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "code..."}} event: message_stop data: {"type": "message_stop"} ```