@jeffreycao/copilot-api 1.13.6 → 1.13.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -58,6 +58,22 @@ To start the server directly from source:
58
58
  bun run start start
59
59
  ```
60
60
 
61
+ ## Running from Source
62
+
63
+ The project can be run from source in several ways:
64
+
65
+ ### Development Mode
66
+
67
+ ```sh
68
+ bun run dev start
69
+ ```
70
+
71
+ ### Production Mode
72
+
73
+ ```sh
74
+ bun run start start
75
+ ```
76
+
61
77
  ## Using with npx
62
78
 
63
79
  You can run the project directly using npx:
@@ -90,6 +106,29 @@ npx @jeffreycao/copilot-api@latest auth login --provider dashscope
90
106
  npx @jeffreycao/copilot-api@latest start
91
107
  ```
92
108
 
109
+ ## Using with Docker
110
+
111
+ Build the image:
112
+
113
+ ```sh
114
+ docker build -t copilot-api .
115
+ ```
116
+
117
+ Run the container with a bind mount so auth data survives restarts:
118
+
119
+ ```sh
120
+ mkdir -p ./copilot-data
121
+ docker run -p 4141:4141 -v $(pwd)/copilot-data:/root/.local/share/copilot-api copilot-api
122
+ ```
123
+
124
+ This stores GitHub auth data, provider config, and other gateway state in `./copilot-data` on the host, mapped to `/root/.local/share/copilot-api` in the container.
125
+
126
+ Or pass a GitHub token directly:
127
+
128
+ ```sh
129
+ docker run -p 4141:4141 -e GH_TOKEN=your_github_token_here copilot-api
130
+ ```
131
+
93
132
  ## Electron Desktop App
94
133
 
95
134
  If you prefer a GUI, this repository also includes an Electron desktop app in `desktop/`. It supports GitHub Copilot sign-in, OpenAI Codex OAuth, and API-key configuration for DeepSeek, DashScope, OpenRouter, or a custom provider. After authorization or provider configuration, it can start and stop the local proxy with one click and shows the local endpoint, auth header, available models, usage, and logs in the app.
@@ -111,115 +150,390 @@ Main dashboard, token usage breakdown in the bundled Electron app:
111
150
  <img src="./docs/screenshots/desktop-token-usage.png" alt="Copilot API desktop app token usage view" width="49%" />
112
151
  </p>
113
152
 
114
- ## Using with Docker
153
+ ## Using with Claude Code
115
154
 
116
- Build the image:
155
+ This AI gateway can be used to power [Claude Code](https://docs.anthropic.com/en/claude-code), an experimental conversational AI assistant for developers from Anthropic.
156
+
157
+ There are two ways to configure Claude Code to use this AI gateway:
158
+
159
+ ### Interactive Setup with `--claude-code` flag
160
+
161
+ To get started, run the `start` command with the `--claude-code` flag:
117
162
 
118
163
  ```sh
119
- docker build -t copilot-api .
164
+ npx @jeffreycao/copilot-api@latest start --claude-code
120
165
  ```
121
166
 
122
- Run the container with a bind mount so auth data survives restarts:
167
+ You will be prompted to select a primary model and a "small, fast" model for background tasks. After selecting the models, a command will be copied to your clipboard. This command sets the necessary environment variables for Claude Code to use the gateway.
123
168
 
124
- ```sh
125
- mkdir -p ./copilot-data
126
- docker run -p 4141:4141 -v $(pwd)/copilot-data:/root/.local/share/copilot-api copilot-api
169
+ Paste and run this command in a new terminal to launch Claude Code.
170
+
171
+ ### Manual Configuration with `settings.json`
172
+
173
+ Alternatively, you can configure Claude Code by creating a `.claude/settings.json` file in your project's root directory. This file should contain the environment variables needed by Claude Code. This way you don't need to run the interactive setup every time.
174
+
175
+ Here is an example `.claude/settings.json` file:
176
+
177
+ ```json
178
+ {
179
+ "env": {
180
+ "ANTHROPIC_BASE_URL": "http://localhost:4141",
181
+ "ANTHROPIC_AUTH_TOKEN": "dummy",
182
+ "ANTHROPIC_MODEL": "deepseek/deepseek-v4-pro",
183
+ "ANTHROPIC_DEFAULT_SONNET_MODEL": "deepseek/deepseek-v4-pro",
184
+ "ANTHROPIC_DEFAULT_HAIKU_MODEL": "deepseek/deepseek-v4-flash",
185
+ "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1",
186
+ "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
187
+ "CLAUDE_CODE_ATTRIBUTION_HEADER": "0",
188
+ "CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION": "false",
189
+ "CLAUDE_CODE_DISABLE_TERMINAL_TITLE": "true",
190
+ "CLAUDE_CODE_ENABLE_AWAY_SUMMARY": "0"
191
+ },
192
+ "permissions": {
193
+ "deny": [
194
+ "mcp__ide__executeCode"
195
+ ]
196
+ }
197
+ }
127
198
  ```
128
199
 
129
- This stores GitHub auth data, provider config, and other gateway state in `./copilot-data` on the host, mapped to `/root/.local/share/copilot-api` in the container.
200
+ - Replace `ANTHROPIC_MODEL`, `ANTHROPIC_DEFAULT_OPUS_MODEL`, `ANTHROPIC_DEFAULT_SONNET_MODEL`, and `ANTHROPIC_DEFAULT_HAIKU_MODEL` according to your needs. After configuration, please install the claude code plugin [Plugin Integrations](#plugin-integrations).
201
+ - Setting CLAUDE_CODE_ATTRIBUTION_HEADER to 0 can prevent Claude code from adding billing and version information in system prompts, thereby avoiding prompt cache invalidation.
202
+ - Turning off CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION and CLAUDE_CODE_ENABLE_AWAY_SUMMARY can prevent quota from being consumed unnecessarily.
203
+ - Claude Code WebSearch is supported for pure search requests. For Copilot, keep the global `messageApiWebSearchModel` set to a Responses-capable GPT model or a `provider/model` alias. For provider routes, use a native Anthropic provider or an `openai-responses` provider. Add `WebSearch` to `permissions.deny` only if you want to forbid this traffic.
204
+ - If using a non-Claude model, do not enable ENABLE_TOOL_SEARCH. If using the Claude model, can enable ENABLE_TOOL_SEARCH. The current Claude Code uses the client tool search mode. In this mode, loading defer tools requires an additional request each time.
205
+ - `CLAUDE_CODE_AUTO_COMPACT_WINDOW`: Set the context capacity in tokens used for auto-compaction calculations. Defaults to the model's context window: 200K for standard models or 1M for extended context models. Use a lower value like `500000` on a 1M model (e.g., `claude-opus-4-6[1m]`) to treat the window as 500K for compaction purposes. The value is capped at the model's actual context window. `CLAUDE_AUTOCOMPACT_PCT_OVERRIDE` is applied as a percentage of this value. Setting this variable decouples the compaction threshold from the status line's `used_percentage`, which always uses the model's full context window.
130
206
 
131
- Or pass a GitHub token directly:
207
+ You can find more options here: [Claude Code settings](https://docs.anthropic.com/en/docs/claude-code/settings#environment-variables)
208
+
209
+ You can also read more about IDE integration here: [Add Claude Code to your IDE](https://docs.anthropic.com/en/docs/claude-code/ide-integrations)
210
+
211
+ ## Using with OpenCode
212
+
213
+ OpenCode already has a direct GitHub Copilot provider. Use this section when you want OpenCode to point at this AI gateway through `@ai-sdk/anthropic` and reuse the agent behaviors described earlier in this README.
214
+
215
+ ### Minimal setup
216
+
217
+ Start the AI gateway with the OpenCode OAuth app:
132
218
 
133
219
  ```sh
134
- docker run -p 4141:4141 -e GH_TOKEN=your_github_token_here copilot-api
220
+ npx @jeffreycao/copilot-api@latest auth --oauth-app=opencode
221
+ npx @jeffreycao/copilot-api@latest start
135
222
  ```
136
223
 
137
- ## Command Structure
224
+ Then point OpenCode at the gateway with `@ai-sdk/anthropic`.
138
225
 
139
- Copilot API now uses a subcommand structure with these main commands:
226
+ Example `~/.config/opencode/opencode.json`:
140
227
 
141
- - `start`: Start the gateway server. If a GitHub token is available, the server starts with Copilot enabled. If no GitHub token is available, it starts in provider-only mode when at least one enabled provider exists; otherwise it guides you through provider setup.
142
- - `auth`: Run provider login or configuration without starting the server. Use it for GitHub Copilot login, Codex OAuth, or third-party provider API key setup.
143
- - `debug`: Display diagnostic information including version, runtime details, file paths, and authentication status. Useful for troubleshooting and support.
228
+ ```json
229
+ {
230
+ "$schema": "https://opencode.ai/config.json",
231
+ "provider": {
232
+ "local": {
233
+ "npm": "@ai-sdk/anthropic",
234
+ "name": "My Local",
235
+ "options": {
236
+ "baseURL": "http://localhost:4141/v1",
237
+ "apiKey": "dummy"
238
+ },
239
+ "models": {
240
+ "gpt-5.4": {
241
+ "name": "gpt-5.4",
242
+ "modalities": {
243
+ "input": ["text", "image"],
244
+ "output": ["text"]
245
+ },
246
+ "limit": {
247
+ "context": 300000,
248
+ "output": 128000
249
+ }
250
+ },
251
+ "claude-sonnet-4.6": {
252
+ "id": "claude-sonnet-4.6",
253
+ "name": "claude-sonnet-4.6",
254
+ "modalities": {
255
+ "input": ["text", "image"],
256
+ "output": ["text"]
257
+ },
258
+ "limit": {
259
+ "context": 200000,
260
+ "output": 32000
261
+ },
262
+ "options": {
263
+ "thinking": {
264
+ "type": "adaptive"
265
+ },
266
+ "effort": "max"
267
+ }
268
+ }
269
+ }
270
+ }
271
+ }
272
+ }
273
+ ```
144
274
 
145
- ## Command Line Options
275
+ Why these fields matter:
146
276
 
147
- ### Global Options
277
+ - `npm: "@ai-sdk/anthropic"` is the important part. OpenCode will speak Anthropic Messages semantics to this AI gateway instead of flattening everything into OpenAI Chat Completions.
278
+ - `options.baseURL` should be `http://localhost:4141/v1`; the Anthropic SDK will append `/messages`, `/models`, and `/messages/count_tokens` automatically.
279
+ - `model`, `small_model`, and `agent.*.model` let you keep `gpt-5.4` for build/plan work while routing exploration and background work to `gpt-5-mini`.
280
+ - If you enable `auth.apiKeys` in this AI gateway, replace `dummy` with a real key. Otherwise any placeholder value is fine.
148
281
 
149
- The following options can be used with any subcommand. When passing them before the subcommand, use the `--key=value` form:
282
+ ## Using with Codex
150
283
 
151
- | Option | Description | Default | Alias |
152
- | ----------------- | ------------------------------------------------------ | ------- | ----- |
153
- | --api-home | Path to the API home directory (sets COPILOT_API_HOME) | none | none |
154
- | --oauth-app | OAuth app identifier (sets COPILOT_API_OAUTH_APP) | none | none |
155
- | --enterprise-url | Enterprise URL for GitHub (sets COPILOT_API_ENTERPRISE_URL) | none | none |
284
+ This AI gateway can also power Codex.
156
285
 
157
- ### Start Command Options
286
+ ### Codex `config.toml` Reference
158
287
 
159
- The following command line options are available for the `start` command:
288
+ Add the following `[model_providers.copilot_api]` section to your Codex `~/.codex/config.toml`:
160
289
 
161
- | Option | Description | Default | Alias |
162
- | -------------- | ----------------------------------------------------------------------------- | ---------- | ----- |
163
- | --port | Port to listen on | 4141 | -p |
164
- | --verbose | Enable verbose logging | false | -v |
165
- | --manual | Enable manual request approval | false | none |
166
- | --rate-limit | Rate limit in seconds between requests | none | -r |
167
- | --wait | Wait instead of error when rate limit is hit | false | -w |
168
- | --github-token | Provide GitHub token directly (must be generated using the `auth` subcommand) | none | -g |
169
- | --claude-code | Generate a command to launch Claude Code with Copilot API config | false | -c |
170
- | --show-token | Show GitHub and Copilot tokens on fetch and refresh | false | none |
171
- | --proxy-env | Initialize proxy from environment variables | false | none |
290
+ ```toml
291
+ model_provider = "copilot_api"
292
+ model_reasoning_summary = "auto"
293
+ model_verbosity = "medium"
294
+ model_context_window = 272000
295
+ model_auto_compact_token_limit = 244800
172
296
 
173
- ### Auth Command Options
297
+ [model_providers.copilot_api]
298
+ name = "OpenAI"
299
+ base_url = "http://localhost:4141"
300
+ env_key = "GITHUB_COPILOT_API_KEY"
301
+ requires_openai_auth = true
302
+ supports_websockets = false
303
+ wire_api = "responses"
304
+ request_max_retries = 3
305
+ stream_max_retries = 1
306
+ stream_idle_timeout_ms = 300000
174
307
 
175
- | Option | Description | Default | Alias |
176
- | ------------ | ------------------------- | ------- | ----- |
177
- | --provider | Provider to log in with or configure (`copilot`, `codex`, `deepseek`, `dashscope`, `openrouter`, or `custom`) | prompt | none |
178
- | --verbose | Enable verbose logging | false | -v |
179
- | --show-token | Show GitHub token on auth | false | none |
308
+ [features]
309
+ remote_compaction_v2 = true
180
310
 
181
- Use `copilot-api auth login --provider copilot` only when you want to enable the GitHub Copilot provider. Copilot is not required for `codex` or third-party provider-only usage.
311
+ [analytics]
312
+ enabled = false
313
+ ```
182
314
 
183
- Use `copilot-api auth login --provider deepseek`, `--provider dashscope`, or `--provider openrouter` to add or update those common third-party providers from the CLI. DeepSeek prompts for masked `apiKey`, provider `type` (default `anthropic`), and `baseUrl` defaulting to `https://api.deepseek.com/anthropic`. DashScope prompts for masked `apiKey`, provider `type` (default `openai-compatible`), and prefilled `baseUrl`. OpenRouter prompts for masked `apiKey` and prefilled `baseUrl` only, and writes `type: "anthropic"`. After a provider is configured and enabled, `copilot-api start` can run without any GitHub token.
315
+ > [!NOTE]
316
+ > This configuration is specific to Codex and the GitHub Copilot provider. `name` must be set to `"OpenAI"`. It can help mitigate Codex local compact cache miss issues. If you have enabled `useResponsesApiContextManagement` (Responses API context management compaction), `remote_compaction_v2` or local compact is generally not triggered, but it may still occur when tool results return a large number of tokens.
184
317
 
185
- Use `copilot-api auth login --provider custom` to add or update another third-party provider from the CLI. The command prompts for the provider name, supported type (`anthropic`, `openai-compatible`, or `openai-responses`), `baseUrl`, masked `apiKey`, and `authType`; `authType` may be left as the type default or set to `x-api-key` / `authorization`.
318
+ ## GPT Tool Search
186
319
 
187
- ### Debug Command Options
320
+ For GPT Responses models such as `gpt-5.4+`, this AI gateway can expose Responses `tool_search` through a small MCP bridge. The same bridge can be used by Claude Code and opencode, as long as the client loads MCP servers and sends Anthropic Messages traffic through this gateway.
188
321
 
189
- | Option | Description | Default | Alias |
190
- | ------ | ------------------------- | ------- | ----- |
191
- | --json | Output debug info as JSON | false | none |
322
+ Do not set Claude Code's native `ENABLE_TOOL_SEARCH` for GPT models. That flag enables Claude Code's own client-side tool search mode, and it may stop forwarding deferred tool definitions. This gateway needs the full tool definitions so it can keep the small always-loaded tool set eager and translate every other tool into Responses deferred namespaces.
192
323
 
193
- ## Configuration (config.json)
324
+ If you install `tool-search@copilot-api-marketplace`, Claude Code receives this MCP bridge automatically and you can skip the manual Claude Code MCP setup below.
194
325
 
195
- - **Location:** `~/.local/share/copilot-api/config.json` (Linux/macOS) or `%USERPROFILE%\.local\share\copilot-api\config.json` (Windows).
196
- - **Default shape:**
197
- ```json
198
- {
199
- "auth": {
200
- "apiKeys": [],
201
- "adminApiKey": "<auto-generated-on-startup>"
202
- },
203
- "providers": {},
204
- "modelMappings": {},
205
- "extraPrompts": {
206
- "gpt-5-mini": "<built-in exploration prompt>",
207
- "gpt-5.3-codex": "<built-in commentary prompt>",
208
- "gpt-5.4-mini": "<built-in commentary prompt>",
209
- "gpt-5.4": "<built-in commentary prompt>",
210
- "gpt-5.5": "<built-in commentary prompt>"
211
- },
212
- "smallModel": "gpt-5-mini",
213
- "useResponsesApiContextManagement": true,
214
- "modelResponsesApiCompactThresholds": {
215
- "gpt-5.4": 217600,
216
- "gpt-5.5": 217600
217
- },
218
- "modelReasoningEfforts": {
219
- "gpt-5-mini": "low",
220
- "gpt-5.3-codex": "xhigh",
221
- "gpt-5.4-mini": "xhigh",
222
- "gpt-5.4": "xhigh",
326
+ Add the tool search bridge to the MCP config used by Claude Code:
327
+
328
+ ```json
329
+ {
330
+ "mcpServers": {
331
+ "tool_search": {
332
+ "type": "stdio",
333
+ "command": "npx",
334
+ "args": ["-y", "@jeffreycao/copilot-api@latest", "mcp"]
335
+ }
336
+ }
337
+ }
338
+ ```
339
+
340
+ Add the tool search bridge to the MCP config used by opencode:
341
+
342
+ ```json
343
+ {
344
+ "mcp": {
345
+ "tool_search": {
346
+ "type": "local",
347
+ "command": ["npx", "-y", "@jeffreycao/copilot-api@latest", "mcp"]
348
+ }
349
+ }
350
+ }
351
+ ```
352
+
353
+ For local development, use `bun` as the command and `["run", "./src/main.ts", "mcp"]` as the args.
354
+
355
+ Internally, the gateway now configures OpenAI Responses `tool_search` in client-executed mode. Deferred tools are still exposed as searchable namespaces, but the model is explicitly asked to return the exact deferred tool names it wants to load next.
356
+
357
+ The bridge uses direct tool selection, not query search. Its tool input is `names`, a comma-separated list of exact deferred tool names, for example `TaskList,TaskGet,mcp__fetch__fetch`.
358
+
359
+ ## Plugin Integrations
360
+
361
+ Plugin integrations are available for Claude Code and opencode.
362
+
363
+ #### Claude Code plugin integration (marketplace-based)
364
+
365
+ The Claude Code integration is packaged as two plugins:
366
+
367
+ - `agent-inject` injects `__SUBAGENT_MARKER__...` on `SubagentStart`, so the gateway can infer `x-initiator: agent`.
368
+ - `tool-search` registers the `tool_search` MCP bridge used for GPT Responses deferred tool loading.
369
+
370
+ - Marketplace catalog in this repository: `.claude-plugin/marketplace.json`
371
+ - Plugin sources in this repository: `plugin/claude/agent-inject`, `plugin/claude/tool-search`
372
+
373
+ Add the marketplace remotely:
374
+
375
+ ```sh
376
+ /plugin marketplace add https://github.com/caozhiyuan/copilot-api.git
377
+ ```
378
+
379
+ Install the plugins from the marketplace:
380
+
381
+ ```sh
382
+ /plugin install agent-inject@copilot-api-marketplace
383
+ /plugin install tool-search@copilot-api-marketplace
384
+ ```
385
+
386
+ After installation, `agent-inject` injects `__SUBAGENT_MARKER__...` on `SubagentStart`, and the gateway uses it to infer `x-initiator: agent`.
387
+
388
+ The `agent-inject` plugin also registers a `UserPromptSubmit` hook that returns `{"continue": true}`, and it can inject `SessionStart` reminder rules through environment variables:
389
+
390
+ - `CLAUDE_PLUGIN_ENABLE_QUESTION_RULES=1` enables the two reminders about using the `question` tool automatically for Claude Code. Alternatively, you can add the same reminders manually in `CLAUDE.md`; see [CLAUDE.md or AGENTS.md Recommended Content](#claudemd-or-agentsmd-recommended-content).
391
+ - `CLAUDE_PLUGIN_ENABLE_NO_BACKGROUND_AGENTS_RULE=1` enables the `run_in_background: true` avoidance reminder for agent hooks.
392
+
393
+ The `tool-search` plugin bundles the same MCP bridge described in [GPT Tool Search](#gpt-tool-search), so Claude Code users do not need to add the `tool_search` server manually when they install that plugin.
394
+
395
+ #### Opencode plugin
396
+
397
+ The subagent marker producer is packaged as an opencode plugin located at `plugin/opencode/subagent-marker.js`.
398
+
399
+ **Installation:**
400
+
401
+ Copy the plugin file to your opencode plugins directory:
402
+
403
+ ```sh
404
+ # Clone or download this repository, then copy the plugin
405
+ cp plugin/opencode/subagent-marker.js ~/.config/opencode/plugins/
406
+ ```
407
+
408
+ Or manually create the file at `~/.config/opencode/plugins/subagent-marker.js` with the plugin content.
409
+
410
+ **Features:**
411
+
412
+ - Tracks sub-sessions created by subagents
413
+ - Automatically prepends a marker system reminder (`__SUBAGENT_MARKER__...`) to subagent chat messages
414
+ - Sets `x-session-id` header for session tracking
415
+ - Enables the gateway to infer `x-initiator: agent` for subagent-originated requests
416
+
417
+ The plugin hooks into `session.created`, `session.deleted`, `chat.message`, and `chat.headers` events to provide seamless subagent marker functionality.
418
+
419
+ ## Using the Usage Viewer
420
+
421
+ After starting the server, a URL to the Copilot Usage Dashboard will be displayed in your console. This dashboard is a web interface for monitoring your API usage.
422
+
423
+ 1. Start the server. For example, using npx:
424
+ ```sh
425
+ npx @jeffreycao/copilot-api@latest start
426
+ ```
427
+ 2. The server will output a URL to the usage viewer. Copy and paste this URL into your browser. It will look something like this:
428
+ `http://localhost:4141/usage-viewer?endpoint=http://localhost:4141/usage`
429
+ - If you use the `start.bat` script on Windows, this page will open automatically.
430
+
431
+ The dashboard provides a user-friendly interface to view your Copilot usage data:
432
+
433
+ > Token usage history requires Bun or Node.js >= 22.13.0. On Node.js < 22.13.0, the server runs normally but token usage storage is disabled.
434
+
435
+ - **API Endpoint URL**: The dashboard is pre-configured to fetch data from your local server endpoint via a URL query parameter. You can manually switch this to any other compatible API endpoint.
436
+ - **x-api-key Authentication**: If API Key authentication is enabled, you can provide the `x-api-key` request header. The key is persisted in the browser's local storage.
437
+ - **Period Selector**: Choose from Day, Week, or Month time ranges. The URL query parameter updates automatically when you switch, making it easy to bookmark and share.
438
+ - **Fetch Data**: Click the "Refresh" button to load or refresh the usage data. The dashboard also fetches data automatically on page load.
439
+ - **Copilot Quotas**: View quota usage for services such as Chat and Completions via progress bars. Hover over a card to see used/remaining details.
440
+ - **Token Usage Metric Cards**: See a summary of Total, Input, Output, Cache Read, Cache Write, Requests, and estimated cost for the current period.
441
+ - **Trend Chart (Week / Month)**: An interactive line chart with model and metric filters. Click a data point to inspect the usage breakdown for a specific day.
442
+ - **Model Breakdown Table**: A per-model summary of requests, input/output/cache tokens, and estimated cost for the selected period.
443
+ - **Request Events (Paginated)**: A time-sorted list of request event records with pagination support, showing timestamps, models, request IDs, and token counts.
444
+ - **Detailed Information**: See the full JSON response from the API for a detailed breakdown of all available usage statistics.
445
+ - **URL-based Configuration**: You can also specify the API endpoint and period directly via `endpoint` and `period` query parameters. For example:
446
+ `http://localhost:4141/usage-viewer?endpoint=http://your-api-server/usage&period=week`
447
+
448
+ ### Usage Viewer Screenshot
449
+
450
+ <p align="center">
451
+ <img src="./docs/screenshots/usage-viewer.png" alt="Copilot API usage viewer" width="900" />
452
+ </p>
453
+
454
+ ## Command Structure
455
+
456
+ Copilot API now uses a subcommand structure with these main commands:
457
+
458
+ - `start`: Start the gateway server. If a GitHub token is available, the server starts with Copilot enabled. If no GitHub token is available, it starts in provider-only mode when at least one enabled provider exists; otherwise it guides you through provider setup.
459
+ - `auth`: Run provider login or configuration without starting the server. Use it for GitHub Copilot login, Codex OAuth, or third-party provider API key setup.
460
+ - `debug`: Display diagnostic information including version, runtime details, file paths, and authentication status. Useful for troubleshooting and support.
461
+
462
+ ## Command Line Options
463
+
464
+ ### Global Options
465
+
466
+ The following options can be used with any subcommand. When passing them before the subcommand, use the `--key=value` form:
467
+
468
+ | Option | Description | Default | Alias |
469
+ | ----------------- | ------------------------------------------------------ | ------- | ----- |
470
+ | --api-home | Path to the API home directory (sets COPILOT_API_HOME) | none | none |
471
+ | --oauth-app | OAuth app identifier (sets COPILOT_API_OAUTH_APP) | none | none |
472
+ | --enterprise-url | Enterprise URL for GitHub (sets COPILOT_API_ENTERPRISE_URL) | none | none |
473
+
474
+ ### Start Command Options
475
+
476
+ The following command line options are available for the `start` command:
477
+
478
+ | Option | Description | Default | Alias |
479
+ | -------------- | ----------------------------------------------------------------------------- | ---------- | ----- |
480
+ | --port | Port to listen on | 4141 | -p |
481
+ | --verbose | Enable verbose logging | false | -v |
482
+ | --github-token | Provide GitHub token directly (must be generated using the `auth` subcommand) | none | -g |
483
+ | --claude-code | Generate a command to launch Claude Code with Copilot API config | false | -c |
484
+ | --show-token | Show GitHub and Copilot tokens on fetch and refresh | false | none |
485
+ | --proxy-env | Initialize proxy from environment variables | false | none |
486
+
487
+ ### Auth Command Options
488
+
489
+ | Option | Description | Default | Alias |
490
+ | ------------ | ------------------------- | ------- | ----- |
491
+ | --provider | Provider to log in with or configure (`copilot`, `codex`, `deepseek`, `dashscope`, `openrouter`, or `custom`) | prompt | none |
492
+ | --verbose | Enable verbose logging | false | -v |
493
+ | --show-token | Show GitHub token on auth | false | none |
494
+
495
+ Use `copilot-api auth login --provider copilot` only when you want to enable the GitHub Copilot provider. Copilot is not required for `codex` or third-party provider-only usage.
496
+
497
+ Use `copilot-api auth login --provider deepseek`, `--provider dashscope`, or `--provider openrouter` to add or update those common third-party providers from the CLI. DeepSeek prompts for masked `apiKey`, provider `type` (default `anthropic`), and `baseUrl` defaulting to `https://api.deepseek.com/anthropic`. DashScope prompts for masked `apiKey`, provider `type` (default `openai-compatible`), and prefilled `baseUrl`. OpenRouter prompts for masked `apiKey` and prefilled `baseUrl` only, and writes `type: "anthropic"`. After a provider is configured and enabled, `copilot-api start` can run without any GitHub token.
498
+
499
+ Use `copilot-api auth login --provider custom` to add or update another third-party provider from the CLI. The command prompts for the provider name, supported type (`anthropic`, `openai-compatible`, or `openai-responses`), `baseUrl`, masked `apiKey`, and `authType`; `authType` may be left as the type default or set to `x-api-key` / `authorization`.
500
+
501
+ ### Debug Command Options
502
+
503
+ | Option | Description | Default | Alias |
504
+ | ------ | ------------------------- | ------- | ----- |
505
+ | --json | Output debug info as JSON | false | none |
506
+
507
+ ## Configuration (config.json)
508
+
509
+ - **Location:** `~/.local/share/copilot-api/config.json` (Linux/macOS) or `%USERPROFILE%\.local\share\copilot-api\config.json` (Windows).
510
+ - **Default shape:**
511
+ ```json
512
+ {
513
+ "auth": {
514
+ "apiKeys": [],
515
+ "adminApiKey": "<auto-generated-on-startup>"
516
+ },
517
+ "providers": {},
518
+ "modelMappings": {},
519
+ "extraPrompts": {
520
+ "gpt-5-mini": "<built-in exploration prompt>",
521
+ "gpt-5.3-codex": "<built-in commentary prompt>",
522
+ "gpt-5.4-mini": "<built-in commentary prompt>",
523
+ "gpt-5.4": "<built-in commentary prompt>",
524
+ "gpt-5.5": "<built-in commentary prompt>"
525
+ },
526
+ "smallModel": "gpt-5-mini",
527
+ "useResponsesApiContextManagement": true,
528
+ "modelResponsesApiCompactThresholds": {
529
+ "gpt-5.4": 217600,
530
+ "gpt-5.5": 217600
531
+ },
532
+ "modelReasoningEfforts": {
533
+ "gpt-5-mini": "low",
534
+ "gpt-5.3-codex": "xhigh",
535
+ "gpt-5.4-mini": "xhigh",
536
+ "gpt-5.4": "xhigh",
223
537
  "gpt-5.5": "xhigh"
224
538
  },
225
539
  "useMessagesApi": true,
@@ -304,7 +618,7 @@ Use `copilot-api auth login --provider custom` to add or update another third-pa
304
618
  - **smallModel:** Fallback model used for tool-less warmup messages (e.g., Claude Code probe requests); defaults to gpt-5-mini.
305
619
  - **useResponsesApiContextManagement:** When `true`, the proxy adds Responses API `context_management` compaction instructions. Defaults to `true`. Set it to `false` to disable this globally. When enabled, the request includes `context_management` in the body and keeps only the latest compaction carrier on follow-up turns. This is especially useful for long-running tasks.
306
620
  - **modelResponsesApiCompactThresholds:** Per-model Responses API `compact_threshold` overrides used when the proxy adds `context_management`. These values take precedence over the fallback threshold from `resolveResponsesCompactThreshold` (`max_prompt_tokens * ratio`, or the default fallback). Defaults set `gpt-5.4` and `gpt-5.5` to `217600` (`272000 * 0.8`). Models not listed continue to use the normal fallback logic.
307
- - **modelReasoningEfforts:** Per-model `reasoning.effort` sent to the Copilot Responses API. Allowed values are `none`, `minimal`, `low`, `medium`, `high`, and `xhigh`. If a model isnt listed, `high` is used by default.
621
+ - **modelReasoningEfforts:** Per-model `reasoning.effort` sent to the Copilot Responses API. Allowed values are `none`, `minimal`, `low`, `medium`, `high`, and `xhigh`. If a model isn't listed, `high` is used by default.
308
622
  - **useMessagesApi:** When `true`, Claude-family models that support Copilot's native `/v1/messages` endpoint will use the Messages API; otherwise they fall back to `/chat/completions`. Set to `false` to disable Messages API routing and always use `/chat/completions`. Defaults to `true`.
309
623
  - **useResponsesApiWebSocket:** When `true`, Responses API requests use Copilot's websocket transport for models that advertise `ws:/responses`; models that only advertise `/responses` continue to use HTTP. Set to `false` to disable websocket routing and use HTTP `/responses` whenever the selected model supports it. Defaults to `true`.
310
624
  - **useResponsesApiWebSearch:** When `true`, the server keeps Responses API tools with `type: "web_search"` and forwards them upstream. Set to `false` to strip those tools from `/responses` payloads. Defaults to `true`.
@@ -420,324 +734,8 @@ curl http://localhost:4141/dashscope/v1/messages \
420
734
  -d '{"model":"qwen3.6-plus","max_tokens":1024,"messages":[{"role":"user","content":"hello"}]}'
421
735
  ```
422
736
 
423
- ## Using with Claude Code
424
-
425
- This AI gateway can be used to power [Claude Code](https://docs.anthropic.com/en/claude-code), an experimental conversational AI assistant for developers from Anthropic.
426
-
427
- There are two ways to configure Claude Code to use this AI gateway:
428
-
429
- ### Interactive Setup with `--claude-code` flag
430
-
431
- To get started, run the `start` command with the `--claude-code` flag:
432
-
433
- ```sh
434
- npx @jeffreycao/copilot-api@latest start --claude-code
435
- ```
436
-
437
- You will be prompted to select a primary model and a "small, fast" model for background tasks. After selecting the models, a command will be copied to your clipboard. This command sets the necessary environment variables for Claude Code to use the gateway.
438
-
439
- Paste and run this command in a new terminal to launch Claude Code.
440
-
441
- ### Manual Configuration with `settings.json`
442
-
443
- Alternatively, you can configure Claude Code by creating a `.claude/settings.json` file in your project's root directory. This file should contain the environment variables needed by Claude Code. This way you don't need to run the interactive setup every time.
444
-
445
- Here is an example `.claude/settings.json` file:
446
-
447
- ```json
448
- {
449
- "env": {
450
- "ANTHROPIC_BASE_URL": "http://localhost:4141",
451
- "ANTHROPIC_AUTH_TOKEN": "dummy",
452
- "ANTHROPIC_MODEL": "deepseek/deepseek-v4-pro",
453
- "ANTHROPIC_DEFAULT_SONNET_MODEL": "deepseek/deepseek-v4-pro",
454
- "ANTHROPIC_DEFAULT_HAIKU_MODEL": "deepseek/deepseek-v4-flash",
455
- "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1",
456
- "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
457
- "CLAUDE_CODE_ATTRIBUTION_HEADER": "0",
458
- "CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION": "false",
459
- "CLAUDE_CODE_DISABLE_TERMINAL_TITLE": "true",
460
- "CLAUDE_CODE_ENABLE_AWAY_SUMMARY": "0"
461
- },
462
- "permissions": {
463
- "deny": [
464
- "mcp__ide__executeCode"
465
- ]
466
- }
467
- }
468
- ```
469
-
470
- - Replace `ANTHROPIC_MODEL`, `ANTHROPIC_DEFAULT_OPUS_MODEL`, `ANTHROPIC_DEFAULT_SONNET_MODEL`, and `ANTHROPIC_DEFAULT_HAIKU_MODEL` according to your needs. After configuration, please install the claude code plugin [Plugin Integrations](#plugin-integrations).
471
- - Setting CLAUDE_CODE_ATTRIBUTION_HEADER to 0 can prevent Claude code from adding billing and version information in system prompts, thereby avoiding prompt cache invalidation.
472
- - Turning off CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION and CLAUDE_CODE_ENABLE_AWAY_SUMMARY can prevent quota from being consumed unnecessarily.
473
- - Claude Code WebSearch is supported for pure search requests. For Copilot, keep the global `messageApiWebSearchModel` set to a Responses-capable GPT model or a `provider/model` alias. For provider routes, use a native Anthropic provider or an `openai-responses` provider. Add `WebSearch` to `permissions.deny` only if you want to forbid this traffic.
474
- - If using a non-Claude model, do not enable ENABLE_TOOL_SEARCH. If using the Claude model, can enable ENABLE_TOOL_SEARCH. The current Claude Code uses the client tool search mode. In this mode, loading defer tools requires an additional request each time.
475
- - `CLAUDE_CODE_AUTO_COMPACT_WINDOW`: Set the context capacity in tokens used for auto-compaction calculations. Defaults to the model's context window: 200K for standard models or 1M for extended context models. Use a lower value like `500000` on a 1M model (e.g., `claude-opus-4-6[1m]`) to treat the window as 500K for compaction purposes. The value is capped at the model's actual context window. `CLAUDE_AUTOCOMPACT_PCT_OVERRIDE` is applied as a percentage of this value. Setting this variable decouples the compaction threshold from the status line's `used_percentage`, which always uses the model's full context window.
476
-
477
- You can find more options here: [Claude Code settings](https://docs.anthropic.com/en/docs/claude-code/settings#environment-variables)
478
-
479
- You can also read more about IDE integration here: [Add Claude Code to your IDE](https://docs.anthropic.com/en/docs/claude-code/ide-integrations)
480
-
481
- ## GPT Tool Search
482
-
483
- For GPT Responses models such as `gpt-5.4+`, this AI gateway can expose Responses `tool_search` through a small MCP bridge. The same bridge can be used by Claude Code and opencode, as long as the client loads MCP servers and sends Anthropic Messages traffic through this gateway.
484
-
485
- Do not set Claude Code's native `ENABLE_TOOL_SEARCH` for GPT models. That flag enables Claude Code's own client-side tool search mode, and it may stop forwarding deferred tool definitions. This gateway needs the full tool definitions so it can keep the small always-loaded tool set eager and translate every other tool into Responses deferred namespaces.
486
-
487
- If you install `tool-search@copilot-api-marketplace`, Claude Code receives this MCP bridge automatically and you can skip the manual Claude Code MCP setup below.
488
-
489
- Add the tool search bridge to the MCP config used by Claude Code:
490
-
491
- ```json
492
- {
493
- "mcpServers": {
494
- "tool_search": {
495
- "type": "stdio",
496
- "command": "npx",
497
- "args": ["-y", "@jeffreycao/copilot-api@latest", "mcp"]
498
- }
499
- }
500
- }
501
- ```
502
-
503
- Add the tool search bridge to the MCP config used by opencode:
504
-
505
- ```json
506
- {
507
- "mcp": {
508
- "tool_search": {
509
- "type": "local",
510
- "command": ["npx", "-y", "@jeffreycao/copilot-api@latest", "mcp"]
511
- }
512
- }
513
- }
514
- ```
515
-
516
- For local development, use `bun` as the command and `["run", "./src/main.ts", "mcp"]` as the args.
517
-
518
- Internally, the gateway now configures OpenAI Responses `tool_search` in client-executed mode. Deferred tools are still exposed as searchable namespaces, but the model is explicitly asked to return the exact deferred tool names it wants to load next.
519
-
520
- The bridge uses direct tool selection, not query search. Its tool input is `names`, a comma-separated list of exact deferred tool names, for example `TaskList,TaskGet,mcp__fetch__fetch`.
521
-
522
- ## Using with OpenCode
523
-
524
- OpenCode already has a direct GitHub Copilot provider. Use this section when you want OpenCode to point at this AI gateway through `@ai-sdk/anthropic` and reuse the agent behaviors described earlier in this README.
525
-
526
- ### Minimal setup
527
-
528
- Start the AI gateway with the OpenCode OAuth app:
529
-
530
- ```sh
531
- npx @jeffreycao/copilot-api@latest auth --oauth-app=opencode
532
- npx @jeffreycao/copilot-api@latest start
533
- ```
534
-
535
- Then point OpenCode at the gateway with `@ai-sdk/anthropic`.
536
-
537
- Example `~/.config/opencode/opencode.json`:
538
-
539
- ```json
540
- {
541
- "$schema": "https://opencode.ai/config.json",
542
- "provider": {
543
- "local": {
544
- "npm": "@ai-sdk/anthropic",
545
- "name": "My Local",
546
- "options": {
547
- "baseURL": "http://localhost:4141/v1",
548
- "apiKey": "dummy"
549
- },
550
- "models": {
551
- "gpt-5.4": {
552
- "name": "gpt-5.4",
553
- "modalities": {
554
- "input": ["text", "image"],
555
- "output": ["text"]
556
- },
557
- "limit": {
558
- "context": 300000,
559
- "output": 128000
560
- }
561
- },
562
- "claude-sonnet-4.6": {
563
- "id": "claude-sonnet-4.6",
564
- "name": "claude-sonnet-4.6",
565
- "modalities": {
566
- "input": ["text", "image"],
567
- "output": ["text"]
568
- },
569
- "limit": {
570
- "context": 200000,
571
- "output": 32000
572
- },
573
- "options": {
574
- "thinking": {
575
- "type": "adaptive"
576
- },
577
- "effort": "max"
578
- }
579
- }
580
- }
581
- }
582
- }
583
- }
584
- ```
585
-
586
- Why these fields matter:
587
-
588
- - `npm: "@ai-sdk/anthropic"` is the important part. OpenCode will speak Anthropic Messages semantics to this AI gateway instead of flattening everything into OpenAI Chat Completions.
589
- - `options.baseURL` should be `http://localhost:4141/v1`; the Anthropic SDK will append `/messages`, `/models`, and `/messages/count_tokens` automatically.
590
- - `model`, `small_model`, and `agent.*.model` let you keep `gpt-5.4` for build/plan work while routing exploration and background work to `gpt-5-mini`.
591
- - If you enable `auth.apiKeys` in this AI gateway, replace `dummy` with a real key. Otherwise any placeholder value is fine.
592
-
593
- ## Using with Codex
594
-
595
- This AI gateway can also power Codex.
596
-
597
- ### Codex `config.toml` Reference
598
-
599
- Add the following `[model_providers.copilot_api]` section to your Codex `~/.codex/config.toml`:
600
-
601
- ```toml
602
- model_provider = "copilot_api"
603
- model_reasoning_summary = "auto"
604
- model_verbosity = "medium"
605
- model_context_window = 272000
606
- model_auto_compact_token_limit = 244800
607
-
608
- [model_providers.copilot_api]
609
- name = "OpenAI"
610
- base_url = "http://localhost:4141"
611
- env_key = "GITHUB_COPILOT_API_KEY"
612
- requires_openai_auth = true
613
- supports_websockets = false
614
- wire_api = "responses"
615
- request_max_retries = 3
616
- stream_max_retries = 1
617
- stream_idle_timeout_ms = 300000
618
-
619
- [features]
620
- remote_compaction_v2 = true
621
-
622
- [analytics]
623
- enabled = false
624
- ```
625
-
626
- > [!NOTE]
627
- > This configuration is specific to Codex and the GitHub Copilot provider. `name` must be set to `"OpenAI"`. It can help mitigate Codex local compact cache miss issues. If you have enabled `useResponsesApiContextManagement` (Responses API context management compaction), `remote_compaction_v2` or local compact is generally not triggered, but it may still occur when tool results return a large number of tokens.
628
-
629
- ## Plugin Integrations
630
-
631
- Plugin integrations are available for Claude Code and opencode.
632
-
633
- #### Claude Code plugin integration (marketplace-based)
634
-
635
- The Claude Code integration is packaged as two plugins:
636
-
637
- - `agent-inject` injects `__SUBAGENT_MARKER__...` on `SubagentStart`, so the gateway can infer `x-initiator: agent`.
638
- - `tool-search` registers the `tool_search` MCP bridge used for GPT Responses deferred tool loading.
639
-
640
- - Marketplace catalog in this repository: `.claude-plugin/marketplace.json`
641
- - Plugin sources in this repository: `plugin/claude/agent-inject`, `plugin/claude/tool-search`
642
-
643
- Add the marketplace remotely:
644
-
645
- ```sh
646
- /plugin marketplace add https://github.com/caozhiyuan/copilot-api.git
647
- ```
648
-
649
- Install the plugins from the marketplace:
650
-
651
- ```sh
652
- /plugin install agent-inject@copilot-api-marketplace
653
- /plugin install tool-search@copilot-api-marketplace
654
- ```
655
-
656
- After installation, `agent-inject` injects `__SUBAGENT_MARKER__...` on `SubagentStart`, and the gateway uses it to infer `x-initiator: agent`.
657
-
658
- The `agent-inject` plugin also registers a `UserPromptSubmit` hook that returns `{"continue": true}`, and it can inject `SessionStart` reminder rules through environment variables:
659
-
660
- - `CLAUDE_PLUGIN_ENABLE_QUESTION_RULES=1` enables the two reminders about using the `question` tool automatically for Claude Code. Alternatively, you can add the same reminders manually in `CLAUDE.md`; see [CLAUDE.md or AGENTS.md Recommended Content](#claudemd-or-agentsmd-recommended-content).
661
- - `CLAUDE_PLUGIN_ENABLE_NO_BACKGROUND_AGENTS_RULE=1` enables the `run_in_background: true` avoidance reminder for agent hooks.
662
-
663
- The `tool-search` plugin bundles the same MCP bridge described in [GPT Tool Search](#gpt-tool-search), so Claude Code users do not need to add the `tool_search` server manually when they install that plugin.
664
-
665
- #### Opencode plugin
666
-
667
- The subagent marker producer is packaged as an opencode plugin located at `plugin/opencode/subagent-marker.js`.
668
-
669
- **Installation:**
670
-
671
- Copy the plugin file to your opencode plugins directory:
672
-
673
- ```sh
674
- # Clone or download this repository, then copy the plugin
675
- cp plugin/opencode/subagent-marker.js ~/.config/opencode/plugins/
676
- ```
677
-
678
- Or manually create the file at `~/.config/opencode/plugins/subagent-marker.js` with the plugin content.
679
-
680
- **Features:**
681
-
682
- - Tracks sub-sessions created by subagents
683
- - Automatically prepends a marker system reminder (`__SUBAGENT_MARKER__...`) to subagent chat messages
684
- - Sets `x-session-id` header for session tracking
685
- - Enables the gateway to infer `x-initiator: agent` for subagent-originated requests
686
-
687
- The plugin hooks into `session.created`, `session.deleted`, `chat.message`, and `chat.headers` events to provide seamless subagent marker functionality.
688
-
689
- ## Using the Usage Viewer
690
-
691
- After starting the server, a URL to the Copilot Usage Dashboard will be displayed in your console. This dashboard is a web interface for monitoring your API usage.
692
-
693
- 1. Start the server. For example, using npx:
694
- ```sh
695
- npx @jeffreycao/copilot-api@latest start
696
- ```
697
- 2. The server will output a URL to the usage viewer. Copy and paste this URL into your browser. It will look something like this:
698
- `http://localhost:4141/usage-viewer?endpoint=http://localhost:4141/usage`
699
- - If you use the `start.bat` script on Windows, this page will open automatically.
700
-
701
- The dashboard provides a user-friendly interface to view your Copilot usage data:
702
-
703
- > Token usage history requires Bun or Node.js >= 22.13.0. On Node.js < 22.13.0, the server runs normally but token usage storage is disabled.
704
-
705
- - **API Endpoint URL**: The dashboard is pre-configured to fetch data from your local server endpoint via the URL query parameter. You can change this URL to point to any other compatible API endpoint.
706
- - **Fetch Data**: Click the "Fetch" button to load or refresh the usage data. The dashboard will automatically fetch data on load.
707
- - **Usage Quotas**: View a summary of your usage quotas for different services like Chat and Completions, displayed with progress bars for a quick overview.
708
- - **Detailed Information**: See the full JSON response from the API for a detailed breakdown of all available usage statistics.
709
- - **URL-based Configuration**: You can also specify the API endpoint directly in the URL using a query parameter. This is useful for bookmarks or sharing links. For example:
710
- `http://localhost:4141/usage-viewer?endpoint=http://your-api-server/usage`
711
-
712
- ### Usage Viewer Screenshot
713
-
714
- <p align="center">
715
- <img src="./docs/screenshots/usage-viewer.png" alt="Copilot API usage viewer" width="900" />
716
- </p>
717
-
718
- ## Running from Source
719
-
720
- The project can be run from source in several ways:
721
-
722
- ### Development Mode
723
-
724
- ```sh
725
- bun run dev start
726
- ```
727
-
728
- ### Production Mode
729
-
730
- ```sh
731
- bun run start start
732
- ```
733
-
734
737
  ## Usage Tips
735
738
 
736
- - To avoid hitting GitHub Copilot's rate limits, you can use the following flags:
737
- - `--manual`: Enables manual approval for each request, giving you full control over when requests are sent.
738
- - `--rate-limit <seconds>`: Enforces a minimum time interval between requests. For example, `copilot-api start --rate-limit 30` will ensure there's at least a 30-second gap between requests.
739
- - `--wait`: Use this with `--rate-limit`. It makes the server wait for the cooldown period to end instead of rejecting the request with an error. This is useful for clients that don't automatically retry on rate limit errors.
740
-
741
739
  ### CLAUDE.md or AGENTS.md Recommended Content
742
740
 
743
741
  To add these reminders manually, include the following in `CLAUDE.md` for Claude Code, or `AGENTS.md` for opencode/codex: