|
1 | 1 | # Claude Code API Gateway |
2 | 2 |
|
3 | | -A simple, focused OpenAI-compatible API gateway for Claude Code with streaming support. |
4 | | -Leverage the Claude Code SDK use mode. Don't hack the token credentials. |
| 3 | +OpenAI-compatible API gateway for Claude Code CLI. |
5 | 4 |
|
6 | | -## Getting Started |
| 5 | +## What You Get |
7 | 6 |
|
8 | | -Use the Makefile to install the project or pip/uv. |
| 7 | +- OpenAI-style endpoints (`/v1/chat/completions`, `/v1/models`, sessions/projects APIs) |
| 8 | +- Streaming and non-streaming chat completions |
| 9 | +- Claude model aliases and fallback behavior |
| 10 | +- Optional `model` field: if omitted, CLI default model is used |
9 | 11 |
|
10 | | - |
| 12 | +## Quick Start (Linux/macOS) |
11 | 13 |
|
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | -### Python Implementation |
23 | 14 | ```bash |
24 | | -# Clone and setup |
25 | 15 | git clone https://github.com/codingworkflow/claude-code-api |
26 | 16 | cd claude-code-api |
27 | | - |
28 | | -# Install dependencies & module |
29 | | -make install |
30 | | - |
31 | | -# Start the API server |
| 17 | +make install |
32 | 18 | make start |
33 | 19 | ``` |
34 | 20 |
|
35 | | -## Limitations |
36 | | - |
37 | | -- There might be a limit on maximum input below normal "Sonnet 4" input as Claude Code usually doesn't ingest more than 25k tokens (despite the context being 100k). |
38 | | -- Claude Code auto-compacts context beyond 100k. |
39 | | -- Currently runs with bypass mode to avoid tool errors. |
40 | | -- Claude Code tools may need to be disabled to avoid overlap and background usage. |
41 | | -- Runs only on Linux/Mac as Claude Code doesn't run on Windows (you can use WSL). |
42 | | -- Note that Claude Code will default to accessing the current workspace environment/folder and is set to use bypass mode. |
43 | | - |
44 | | - |
45 | | -## Features |
46 | | - |
47 | | -- **Claude-Only Models**: Supports exactly the 4 Claude models that Claude Code CLI offers |
48 | | -- **OpenAI Compatible**: Drop-in replacement for OpenAI API endpoints |
49 | | -- **Streaming Support**: Real-time streaming responses |
50 | | -- **Simple & Clean**: No over-engineering, focused implementation |
51 | | -- **Claude Code Integration**: Leverages Claude Code CLI with streaming output |
52 | | - |
53 | | -## Supported Models |
54 | | - |
55 | | -Model definitions live in `claude_code_api/config/models.json`. |
56 | | -Override with `CLAUDE_CODE_API_MODELS_PATH` to point at a custom JSON file. |
57 | | - |
58 | | -- `claude-opus-4-5-20250929` - Claude Opus 4.5 (Most powerful) |
59 | | -- `claude-sonnet-4-5-20250929` - Claude Sonnet 4.5 (Latest Sonnet) |
60 | | -- `claude-haiku-4-5-20250929` - Claude Haiku 4.5 (Fast & cost-effective) |
61 | | - |
62 | | -## Quick Start |
| 21 | +Server URLs: |
| 22 | +- API: `http://localhost:8000` |
| 23 | +- OpenAPI docs: `http://localhost:8000/docs` |
| 24 | +- Health: `http://localhost:8000/health` |
63 | 25 |
|
64 | | -### Prerequisites |
65 | | -- Python 3.10+ |
66 | | -- Claude Code CLI installed and accessible |
67 | | -- Valid Anthropic API key configured in Claude Code (ensure it works in current directory src/) |
| 26 | +## Quick Start (Windows) |
68 | 27 |
|
69 | | -### Installation & Setup |
| 28 | +Use the provided wrappers: |
70 | 29 |
|
71 | | -```bash |
72 | | -# Clone and setup |
73 | | -git clone https://github.com/codingworkflow/claude-code-api |
74 | | -cd claude-code-api |
75 | | - |
76 | | -# Install dependencies |
77 | | -make install |
78 | | - |
79 | | -# Run tests to verify setup |
80 | | -make test |
81 | | - |
82 | | -# Start the API server |
83 | | -make start-dev |
| 30 | +```bat |
| 31 | +make.bat install |
| 32 | +start.bat |
84 | 33 | ``` |
85 | 34 |
|
86 | | -The API will be available at: |
87 | | -- **API**: http://localhost:8000 |
88 | | -- **Docs**: http://localhost:8000/docs |
89 | | -- **Health**: http://localhost:8000/health |
| 35 | +Notes: |
| 36 | +- `start.bat` starts the API in dev mode. |
| 37 | +- `make.bat` provides common project commands. |
| 38 | +- Claude Code CLI support on Windows may require WSL depending on your local setup. |
90 | 39 |
|
91 | | -## Makefile Commands |
92 | | - |
93 | | -### Core Commands |
94 | | -```bash |
95 | | -make install # Install production dependencies |
96 | | -make install-dev # Install development dependencies |
97 | | -make test # Run all tests |
98 | | -make start # Start API server (production) |
99 | | -make start-dev # Start API server (development with reload) |
100 | | -``` |
| 40 | +## Supported Models |
101 | 41 |
|
102 | | -### Testing |
103 | | -```bash |
104 | | -make test # Run all tests |
105 | | -make test-fast # Run tests (skip slow ones) |
106 | | -make test-hello # Test hello world with Haiku |
107 | | -make test-health # Test health check only |
108 | | -make test-models # Test models API only |
109 | | -make test-chat # Test chat completions only |
110 | | -make quick-test # Quick validation of core functionality |
111 | | -``` |
| 42 | +Model config is in `claude_code_api/config/models.json`. |
| 43 | +Override with `CLAUDE_CODE_API_MODELS_PATH`. |
112 | 44 |
|
113 | | -### Development |
114 | | -```bash |
115 | | -make dev-setup # Complete development setup |
116 | | -make lint # Run linting checks |
117 | | -make format # Format code with black/isort |
118 | | -make type-check # Run type checking |
119 | | -make clean # Clean up cache files |
120 | | -``` |
| 45 | +- `claude-opus-4-6-20260205` |
| 46 | +- `claude-opus-4-5-20251101` |
| 47 | +- `claude-sonnet-4-5-20250929` |
| 48 | +- `claude-haiku-4-5-20251001` |
121 | 49 |
|
122 | | -### Information |
123 | | -```bash |
124 | | -make help # Show all available commands |
125 | | -make models # Show supported Claude models |
126 | | -make info # Show project information |
127 | | -make check-claude # Check if Claude Code CLI is available |
128 | | -``` |
| 50 | +Alias/fallback behavior: |
| 51 | +- `model` is optional in `/v1/chat/completions`. |
| 52 | +- `opus`, `claude-opus-latest`, `claude-opus-4-6` resolve to Opus 4.6. |
| 53 | +- If Opus 4.6 is rejected at runtime, gateway retries once with latest configured Opus 4.5. |
| 54 | +- If all attempted models are rejected, API returns `400` with `error.code = model_not_supported`. |
129 | 55 |
|
130 | 56 | ## API Usage |
131 | 57 |
|
132 | | -### Chat Completions |
| 58 | +Chat completion: |
133 | 59 |
|
134 | 60 | ```bash |
135 | 61 | curl -X POST http://localhost:8000/v1/chat/completions \ |
136 | 62 | -H "Content-Type: application/json" \ |
137 | 63 | -d '{ |
138 | | - "model": "claude-sonnet-4-5-20250929", |
139 | 64 | "messages": [ |
140 | | - {"role": "user", "content": "Hello!"} |
| 65 | + {"role": "user", "content": "Hello"} |
141 | 66 | ] |
142 | 67 | }' |
143 | 68 | ``` |
144 | 69 |
|
145 | | -### List Models |
| 70 | +List models: |
146 | 71 |
|
147 | 72 | ```bash |
148 | 73 | curl http://localhost:8000/v1/models |
149 | 74 | ``` |
150 | 75 |
|
151 | | -### Streaming Chat |
| 76 | +Streaming: |
152 | 77 |
|
153 | 78 | ```bash |
154 | 79 | curl -X POST http://localhost:8000/v1/chat/completions \ |
155 | 80 | -H "Content-Type: application/json" \ |
156 | 81 | -d '{ |
157 | 82 | "model": "claude-sonnet-4-5-20250929", |
158 | | - "messages": [ |
159 | | - {"role": "user", "content": "Tell me a joke"} |
160 | | - ], |
| 83 | + "messages": [{"role": "user", "content": "Tell me a joke"}], |
161 | 84 | "stream": true |
162 | 85 | }' |
163 | 86 | ``` |
164 | 87 |
|
165 | | -## Project Structure |
166 | | - |
167 | | -``` |
168 | | -claude-code-api/ |
169 | | -├── claude_code_api/ |
170 | | -│ ├── main.py # FastAPI application |
171 | | -│ ├── api/ # API endpoints |
172 | | -│ │ ├── chat.py # Chat completions |
173 | | -│ │ ├── models.py # Models API |
174 | | -│ │ ├── projects.py # Project management |
175 | | -│ │ └── sessions.py # Session management |
176 | | -│ ├── core/ # Core functionality |
177 | | -│ │ ├── auth.py # Authentication |
178 | | -│ │ ├── claude_manager.py # Claude Code integration |
179 | | -│ │ ├── session_manager.py # Session management |
180 | | -│ │ ├── config.py # Configuration |
181 | | -│ │ └── database.py # Database layer |
182 | | -│ ├── models/ # Data models |
183 | | -│ │ ├── claude.py # Claude-specific models |
184 | | -│ │ └── openai.py # OpenAI-compatible models |
185 | | -│ ├── utils/ # Utilities |
186 | | -│ │ ├── streaming.py # Streaming support |
187 | | -│ │ └── parser.py # Output parsing |
188 | | -│ └── tests/ # Test suite |
189 | | -├── Makefile # Development commands |
190 | | -├── pyproject.toml # Project configuration |
191 | | -├── setup.py # Package setup |
192 | | -└── README.md # This file |
193 | | -``` |
194 | | - |
195 | | -## Testing |
196 | | - |
197 | | -The test suite validates: |
198 | | -- Health check endpoints |
199 | | -- Models API (Claude models only) |
200 | | -- Chat completions with Haiku model |
201 | | -- Hello world functionality |
202 | | -- OpenAI compatibility (structure) |
203 | | -- Error handling |
204 | | - |
205 | | -Run specific test suites: |
206 | | -```bash |
207 | | -make test-hello # Test hello world with Haiku |
208 | | -make test-models # Test models API |
209 | | -make test-chat # Test chat completions |
210 | | -``` |
211 | | - |
212 | | -## Development |
213 | | - |
214 | | -### Setup Development Environment |
215 | | -```bash |
216 | | -make dev-setup |
217 | | -``` |
218 | | - |
219 | | -### Code Quality |
220 | | -```bash |
221 | | -make format # Format code |
222 | | -make lint # Check linting |
223 | | -make type-check # Type checking |
224 | | -``` |
225 | | - |
226 | | -### Quick Validation |
227 | | -```bash |
228 | | -make quick-test # Test core functionality |
229 | | -``` |
230 | | - |
231 | | -## Deployment |
232 | | - |
233 | | -### Check Deployment Readiness |
234 | | -```bash |
235 | | -make deploy-check |
236 | | -``` |
237 | | - |
238 | | -### Production Server |
239 | | -```bash |
240 | | -make start-prod # Start with multiple workers |
241 | | -``` |
242 | | -Use http://127.0.0.1:8000/v1 as OpenAPI endpoint |
243 | | - |
244 | 88 | ## Configuration |
245 | 89 |
|
246 | | -Key settings in `claude_code_api/core/config.py`: |
247 | | -- `claude_binary_path`: Path to Claude Code CLI |
248 | | -- `project_root`: Root directory for projects |
249 | | -- `database_url`: Database connection string |
250 | | -- `require_auth`: Enable/disable authentication |
251 | | - |
252 | | -## Design Principles |
253 | | - |
254 | | -1. **Simple & Focused**: No over-engineering |
255 | | -2. **Claude-Only**: Pure Claude gateway, no OpenAI models |
256 | | -3. **Streaming First**: Built for real-time streaming |
257 | | -4. **OpenAI Compatible**: Drop-in API compatibility |
258 | | -5. **Test-Driven**: Comprehensive test coverage |
| 90 | +Common settings are in `claude_code_api/core/config.py`: |
| 91 | +- `claude_binary_path` |
| 92 | +- `project_root` |
| 93 | +- `database_url` |
| 94 | +- `require_auth` |
259 | 95 |
|
260 | | -## Health Check |
| 96 | +## Developer Docs |
261 | 97 |
|
262 | | -```bash |
263 | | -curl http://localhost:8000/health |
264 | | -``` |
265 | | - |
266 | | -Response: |
267 | | -```json |
268 | | -{ |
269 | | - "status": "healthy", |
270 | | - "version": "1.0.0", |
271 | | - "claude_version": "1.x.x", |
272 | | - "active_sessions": 0 |
273 | | -} |
274 | | -``` |
| 98 | +For engineering workflows and internal commands: |
| 99 | +- `docs/dev.md` |
275 | 100 |
|
276 | 101 | ## License |
277 | 102 |
|
|
0 commit comments