RichardAtCT/claude-code-openai-wrapper

OpenAI API-compatible wrapper for Claude Code

Claude Code OpenAI API Wrapper

What it is – A tiny web server that sits in front of Anthropic’s Claude Code model and speaks the same HTTP API that the OpenAI v1 chat‑completion endpoints use. It lets any existing OpenAI client library (Python, curl, etc.) talk to Claude Code without code changes.

Why it matters – Claude Code is a powerful code‑focused LLM, but most tools and libraries are built for the OpenAI API. This wrapper bridges that gap, handling authentication (API key, Anthropic CLI, AWS Bedrock, Google Vertex), model selection, token‑ and cost‑tracking, and optional tool usage while keeping the request/response shape identical to OpenAI.


Core capabilities

  • OpenAI‑compatible /v1/chat/completions (streaming and non‑streaming) – drop‑in replacement for OpenAI chat calls.
  • Session continuity – supply a session_id to keep conversation history across requests; includes management endpoints (GET/DELETE /v1/sessions).
  • Multi‑provider auth – automatically picks the right auth method (Anthropic CLI login, ANTHROPIC_API_KEY, AWS Bedrock, or Google Vertex AI).
  • Real‑time cost & token info – returned in the OpenAI‑style usage block, sourced from the official Claude Code Python SDK.
  • Optional tool usage – enable Claude Code’s built‑in tools (file read/write, bash, etc.) on a per‑request basis.
  • Fast default mode – tools are disabled unless explicitly turned on, giving a 5‑10× speed boost for plain chat.
  • Interactive API‑key protection – the server can generate a temporary bearer token for remote access.

Quick start (≈2 min)

# 1. Install Claude Code CLI (Node package)
npm install -g @anthropic-ai/claude-code

# 2. Authenticate (CLI login or set ANTHROPIC_API_KEY)
claude auth login   # or export ANTHROPIC_API_KEY=…

# 3. Clone and install the wrapper
git clone https://github.com/RichardAtCT/claude-code-openai-wrapper
cd claude-code-openai-wrapper
poetry install   # creates a venv and installs deps

# 4. Run the server (development mode with auto‑reload)
poetry run uvicorn main:app --reload --port 8000

The API is now reachable at http://localhost:8000/v1.


Example usage

Curl (no auth)

curl -X POST http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
        "model": "claude-3-5-sonnet-20241022",
        "messages": [{"role": "user", "content": "What is 2+2?"}]
      }'

Python (OpenAI SDK)

from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1")
resp = client.chat.completions.create(
    model="claude-3-5-sonnet-20241022",
    messages=[{"role": "user", "content": "List files in the current directory"}],
    extra_body={"enable_tools": True}   # turn on Claude's file‑read tool
)
print(resp.choices[0].message.content)
print("Cost:", resp.usage.total_tokens * 0.000003)

Session continuity

client = OpenAI(base_url="http://localhost:8000/v1")
first = client.chat.completions.create(
    model="claude-3-5-sonnet-20241022",
    messages=[{"role": "user", "content": "My name is Alice."}],
    extra_body={"session_id": "alice-demo"}
)
second = client.chat.completions.create(
    model="claude-3-5-sonnet-20241022",
    messages=[{"role": "user", "content": "What is my name?"}],
    extra_body={"session_id": "alice-demo"}
)
print(second.choices[0].message.content)   # → "Your name is Alice."

Installation & configuration details

Step Command / File Purpose
Prereqs npm install -g @anthropic-ai/claude-code Installs the Claude Code CLI used by the wrapper
Python Python 3.10+ + poetry Runs the FastAPI server
Env file cp .env.example .env then edit Set CLAUDE_CLI_PATH, optional API_KEY, PORT, CORS, timeout, etc.
Run poetry run uvicorn main:app --reload (dev) or poetry run python main.py (prod) Starts the HTTP service

Project health

  • Status: Production‑ready – core chat endpoint, streaming, model validation, multi‑auth, session handling, and health checks are all marked as working.
  • Tests: Simple endpoint test script (test_endpoints.py) and a more thorough suite (test_basic.py). Expected pass rate 100 % on a correctly configured system.
  • License: MIT – free to use, modify, and redistribute.

Who might use this

  • Developers who have existing OpenAI‑based tooling (e.g., LangChain, Copilot‑style assistants) and want to switch to Claude Code without rewriting client code.
  • Teams deploying Claude Code behind a firewall and needing a familiar OpenAI‑style endpoint for internal services.
  • Researchers experimenting with Claude Code’s tool‑use capabilities while keeping the same API surface as OpenAI.

Limitations (as of the README)

  • No support for image messages, function calling, or many OpenAI parameters (temperature, top_p, etc.).
  • Only one response per request (n > 1 not implemented).
  • Tools are disabled by default; they must be explicitly enabled per request.

Future roadmap (brief)

  • Add fine‑grained tool‑allow‑list endpoints.
  • Map additional OpenAI parameters (temperature, max_tokens, etc.).
  • Improve streaming chunk handling.
  • Support Model Context Protocol (MCP) server integration.

In a nutshell: This repository provides a ready‑to‑run FastAPI service that makes Claude Code look like an OpenAI chat API, handling auth, sessions, cost tracking, and optional tool usage, enabling seamless reuse of existing OpenAI client code with Anthropic’s code‑focused model.

Related

  • Project
  • Project
  • Project
  • Project