@jeffreycao/copilot-api 1.13.6 → 1.13.8

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
@@ -33,9 +33,7 @@ On the GitHub Copilot path, the gateway prefers Copilot's native Anthropic-style
33
33
  - **Agent-friendly Claude handling on Copilot**: Prefer native `/v1/messages` when available, preserve Claude-style tool flows, support Anthropic beta features, Claude WebSearch through Responses-capable models, and keep subagent/session markers intact.
34
34
  - **Claude Code and OpenCode integration**: Works with Claude Code and OpenCode, including direct Anthropic-compatible usage through `@ai-sdk/anthropic`.
35
35
  - **Flexible auth and deployment options**: Supports interactive login or direct tokens, individual/business/enterprise plans, GitHub Enterprise, opencode OAuth, and custom data directories.
36
- - **Local control and visibility**: Includes a usage dashboard, rate limiting, manual approval, and optional token visibility for debugging.
37
36
  - **Multi-provider routing**: Expose provider-specific `/:provider/...` routes or use `model: "provider/model"` on the top-level API.
38
- - **Better token and context management**: Supports exact Claude token counting and configurable GPT context compaction for long-running conversations.
39
37
 
40
38
  ## Prerequisites
41
39
 
@@ -58,6 +56,22 @@ To start the server directly from source:
58
56
  bun run start start
59
57
  ```
60
58
 
59
+ ## Running from Source
60
+
61
+ The project can be run from source in several ways:
62
+
63
+ ### Development Mode
64
+
65
+ ```sh
66
+ bun run dev start
67
+ ```
68
+
69
+ ### Production Mode
70
+
71
+ ```sh
72
+ bun run start start
73
+ ```
74
+
61
75
  ## Using with npx
62
76
 
63
77
  You can run the project directly using npx:
@@ -90,6 +104,29 @@ npx @jeffreycao/copilot-api@latest auth login --provider dashscope
90
104
  npx @jeffreycao/copilot-api@latest start
91
105
  ```
92
106
 
107
+ ## Using with Docker
108
+
109
+ Build the image:
110
+
111
+ ```sh
112
+ docker build -t copilot-api .
113
+ ```
114
+
115
+ Run the container with a bind mount so auth data survives restarts:
116
+
117
+ ```sh
118
+ mkdir -p ./copilot-data
119
+ docker run -p 4141:4141 -v $(pwd)/copilot-data:/root/.local/share/copilot-api copilot-api
120
+ ```
121
+
122
+ 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.
123
+
124
+ Or pass a GitHub token directly:
125
+
126
+ ```sh
127
+ docker run -p 4141:4141 -e GH_TOKEN=your_github_token_here copilot-api
128
+ ```
129
+
93
130
  ## Electron Desktop App
94
131
 
95
132
  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,118 +148,392 @@ Main dashboard, token usage breakdown in the bundled Electron app:
111
148
  <img src="./docs/screenshots/desktop-token-usage.png" alt="Copilot API desktop app token usage view" width="49%" />
112
149
  </p>
113
150
 
114
- ## Using with Docker
151
+ ## Using with Claude Code
115
152
 
116
- Build the image:
153
+ 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.
154
+
155
+ There are two ways to configure Claude Code to use this AI gateway:
156
+
157
+ ### Interactive Setup with `--claude-code` flag
158
+
159
+ To get started, run the `start` command with the `--claude-code` flag:
117
160
 
118
161
  ```sh
119
- docker build -t copilot-api .
162
+ npx @jeffreycao/copilot-api@latest start --claude-code
120
163
  ```
121
164
 
122
- Run the container with a bind mount so auth data survives restarts:
165
+ 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
166
 
124
- ```sh
125
- mkdir -p ./copilot-data
126
- docker run -p 4141:4141 -v $(pwd)/copilot-data:/root/.local/share/copilot-api copilot-api
167
+ Paste and run this command in a new terminal to launch Claude Code.
168
+
169
+ ### Manual Configuration with `settings.json`
170
+
171
+ 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.
172
+
173
+ Here is an example `.claude/settings.json` file:
174
+
175
+ ```json
176
+ {
177
+ "env": {
178
+ "ANTHROPIC_BASE_URL": "http://localhost:4141",
179
+ "ANTHROPIC_AUTH_TOKEN": "dummy",
180
+ "ANTHROPIC_MODEL": "deepseek/deepseek-v4-pro",
181
+ "ANTHROPIC_DEFAULT_SONNET_MODEL": "deepseek/deepseek-v4-pro",
182
+ "ANTHROPIC_DEFAULT_HAIKU_MODEL": "deepseek/deepseek-v4-flash",
183
+ "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1",
184
+ "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
185
+ "CLAUDE_CODE_ATTRIBUTION_HEADER": "0",
186
+ "CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION": "false",
187
+ "CLAUDE_CODE_DISABLE_TERMINAL_TITLE": "true",
188
+ "CLAUDE_CODE_ENABLE_AWAY_SUMMARY": "0"
189
+ },
190
+ "permissions": {
191
+ "deny": [
192
+ "mcp__ide__executeCode"
193
+ ]
194
+ }
195
+ }
127
196
  ```
128
197
 
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.
198
+ - 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).
199
+ - 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.
200
+ - Turning off CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION and CLAUDE_CODE_ENABLE_AWAY_SUMMARY can prevent quota from being consumed unnecessarily.
201
+ - 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.
202
+ - 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.
203
+ - `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
204
 
131
- Or pass a GitHub token directly:
205
+ You can find more options here: [Claude Code settings](https://docs.anthropic.com/en/docs/claude-code/settings#environment-variables)
206
+
207
+ 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)
208
+
209
+ ## Using with OpenCode
210
+
211
+ 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.
212
+
213
+ ### Minimal setup
214
+
215
+ Start the AI gateway with the OpenCode OAuth app:
132
216
 
133
217
  ```sh
134
- docker run -p 4141:4141 -e GH_TOKEN=your_github_token_here copilot-api
218
+ npx @jeffreycao/copilot-api@latest auth --oauth-app=opencode
219
+ npx @jeffreycao/copilot-api@latest start
135
220
  ```
136
221
 
137
- ## Command Structure
222
+ Then point OpenCode at the gateway with `@ai-sdk/anthropic`.
138
223
 
139
- Copilot API now uses a subcommand structure with these main commands:
224
+ Example `~/.config/opencode/opencode.json`:
140
225
 
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.
226
+ ```json
227
+ {
228
+ "$schema": "https://opencode.ai/config.json",
229
+ "provider": {
230
+ "local": {
231
+ "npm": "@ai-sdk/anthropic",
232
+ "name": "My Local",
233
+ "options": {
234
+ "baseURL": "http://localhost:4141/v1",
235
+ "apiKey": "dummy"
236
+ },
237
+ "models": {
238
+ "gpt-5.4": {
239
+ "name": "gpt-5.4",
240
+ "modalities": {
241
+ "input": ["text", "image"],
242
+ "output": ["text"]
243
+ },
244
+ "limit": {
245
+ "context": 300000,
246
+ "output": 128000
247
+ }
248
+ },
249
+ "claude-sonnet-4.6": {
250
+ "id": "claude-sonnet-4.6",
251
+ "name": "claude-sonnet-4.6",
252
+ "modalities": {
253
+ "input": ["text", "image"],
254
+ "output": ["text"]
255
+ },
256
+ "limit": {
257
+ "context": 200000,
258
+ "output": 32000
259
+ },
260
+ "options": {
261
+ "thinking": {
262
+ "type": "adaptive"
263
+ },
264
+ "effort": "max"
265
+ }
266
+ }
267
+ }
268
+ }
269
+ }
270
+ }
271
+ ```
144
272
 
145
- ## Command Line Options
273
+ Why these fields matter:
146
274
 
147
- ### Global Options
275
+ - `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.
276
+ - `options.baseURL` should be `http://localhost:4141/v1`; the Anthropic SDK will append `/messages`, `/models`, and `/messages/count_tokens` automatically.
277
+ - If you enable `auth.apiKeys` in this AI gateway, replace `dummy` with a real key. Otherwise any placeholder value is fine.
148
278
 
149
- The following options can be used with any subcommand. When passing them before the subcommand, use the `--key=value` form:
279
+ ## Using with Codex
150
280
 
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 |
281
+ This AI gateway can also power Codex.
156
282
 
157
- ### Start Command Options
283
+ ### Codex `config.toml` Reference
158
284
 
159
- The following command line options are available for the `start` command:
285
+ Add the following `[model_providers.copilot_api]` section to your Codex `~/.codex/config.toml`:
160
286
 
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 |
287
+ ```toml
288
+ model_provider = "copilot_api"
289
+ model_reasoning_summary = "auto"
290
+ model_verbosity = "medium"
291
+ model_context_window = 272000
292
+ model_auto_compact_token_limit = 244800
172
293
 
173
- ### Auth Command Options
294
+ [model_providers.copilot_api]
295
+ name = "OpenAI"
296
+ base_url = "http://localhost:4141"
297
+ env_key = "GITHUB_COPILOT_API_KEY"
298
+ requires_openai_auth = true
299
+ supports_websockets = false
300
+ wire_api = "responses"
301
+ request_max_retries = 3
302
+ stream_max_retries = 1
303
+ stream_idle_timeout_ms = 300000
174
304
 
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 |
305
+ [features]
306
+ remote_compaction_v2 = true
180
307
 
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.
308
+ [analytics]
309
+ enabled = false
310
+ ```
182
311
 
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.
312
+ > [!NOTE]
313
+ > 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
314
 
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`.
315
+ ## GPT Tool Search
186
316
 
187
- ### Debug Command Options
317
+ 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
318
 
189
- | Option | Description | Default | Alias |
190
- | ------ | ------------------------- | ------- | ----- |
191
- | --json | Output debug info as JSON | false | none |
319
+ 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
320
 
193
- ## Configuration (config.json)
321
+ 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
322
 
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",
223
- "gpt-5.5": "xhigh"
224
- },
225
- "useMessagesApi": true,
323
+ Add the tool search bridge to the MCP config used by Claude Code:
324
+
325
+ ```json
326
+ {
327
+ "mcpServers": {
328
+ "tool_search": {
329
+ "type": "stdio",
330
+ "command": "npx",
331
+ "args": ["-y", "@jeffreycao/copilot-api@latest", "mcp"]
332
+ }
333
+ }
334
+ }
335
+ ```
336
+
337
+ Add the tool search bridge to the MCP config used by opencode:
338
+
339
+ ```json
340
+ {
341
+ "mcp": {
342
+ "tool_search": {
343
+ "type": "local",
344
+ "command": ["npx", "-y", "@jeffreycao/copilot-api@latest", "mcp"]
345
+ }
346
+ }
347
+ }
348
+ ```
349
+
350
+ For local development, use `bun` as the command and `["run", "./src/main.ts", "mcp"]` as the args.
351
+
352
+ 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.
353
+
354
+ 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`.
355
+
356
+ ## Plugin Integrations
357
+
358
+ Plugin integrations are available for Claude Code and opencode.
359
+
360
+ #### Claude Code plugin integration (marketplace-based)
361
+
362
+ The Claude Code integration is packaged as two plugins:
363
+
364
+ - `agent-inject` injects `__SUBAGENT_MARKER__...` on `SubagentStart`, so the gateway can infer `x-initiator: agent`.
365
+ - `tool-search` registers the `tool_search` MCP bridge used for GPT Responses deferred tool loading.
366
+
367
+ - Marketplace catalog in this repository: `.claude-plugin/marketplace.json`
368
+ - Plugin sources in this repository: `plugin/claude/agent-inject`, `plugin/claude/tool-search`
369
+
370
+ Add the marketplace remotely:
371
+
372
+ ```sh
373
+ /plugin marketplace add https://github.com/caozhiyuan/copilot-api.git
374
+ ```
375
+
376
+ Install the plugins from the marketplace:
377
+
378
+ ```sh
379
+ /plugin install agent-inject@copilot-api-marketplace
380
+ /plugin install tool-search@copilot-api-marketplace
381
+ ```
382
+
383
+ After installation, `agent-inject` injects `__SUBAGENT_MARKER__...` on `SubagentStart`, and the gateway uses it to infer `x-initiator: agent`.
384
+
385
+ The `agent-inject` plugin also registers a `UserPromptSubmit` hook that returns `{"continue": true}`, and it can inject `SessionStart` reminder rules through environment variables:
386
+
387
+ - `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).
388
+ - `CLAUDE_PLUGIN_ENABLE_NO_BACKGROUND_AGENTS_RULE=1` enables the `run_in_background: true` avoidance reminder for agent hooks.
389
+
390
+ 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.
391
+
392
+ #### Opencode plugin
393
+
394
+ The subagent marker producer is packaged as an opencode plugin located at `plugin/opencode/subagent-marker.js`.
395
+
396
+ **Installation:**
397
+
398
+ Copy the plugin file to your opencode plugins directory:
399
+
400
+ ```sh
401
+ # Clone or download this repository, then copy the plugin
402
+ cp plugin/opencode/subagent-marker.js ~/.config/opencode/plugins/
403
+ ```
404
+
405
+ Or manually create the file at `~/.config/opencode/plugins/subagent-marker.js` with the plugin content.
406
+
407
+ **Features:**
408
+
409
+ - Tracks sub-sessions created by subagents
410
+ - Automatically prepends a marker system reminder (`__SUBAGENT_MARKER__...`) to subagent chat messages
411
+ - Sets `x-session-id` header for session tracking
412
+ - Enables the gateway to infer `x-initiator: agent` for subagent-originated requests
413
+
414
+ The plugin hooks into `session.created`, `session.deleted`, `chat.message`, and `chat.headers` events to provide seamless subagent marker functionality.
415
+
416
+ ## Using the Usage Viewer
417
+
418
+ 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.
419
+
420
+ 1. Start the server. For example, using npx:
421
+ ```sh
422
+ npx @jeffreycao/copilot-api@latest start
423
+ ```
424
+ 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:
425
+ `http://localhost:4141/usage-viewer?endpoint=http://localhost:4141/usage`
426
+ - If you use the `start.bat` script on Windows, this page will open automatically.
427
+
428
+ The dashboard provides a user-friendly interface to view your Copilot usage data:
429
+
430
+ > 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.
431
+
432
+ - **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.
433
+ - **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.
434
+ - **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.
435
+ - **Fetch Data**: Click the "Refresh" button to load or refresh the usage data. The dashboard also fetches data automatically on page load.
436
+ - **Copilot Quotas**: View quota usage for services such as Chat and Completions via progress bars. Hover over a card to see used/remaining details.
437
+ - **Token Usage Metric Cards**: See a summary of Total, Input, Output, Cache Read, Cache Write, Requests, and estimated cost for the current period.
438
+ - **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.
439
+ - **Model Breakdown Table**: A per-model summary of requests, input/output/cache tokens, and estimated cost for the selected period.
440
+ - **Request Events (Paginated)**: A time-sorted list of request event records with pagination support, showing timestamps, models, request IDs, and token counts.
441
+ - **Detailed Information**: See the full JSON response from the API for a detailed breakdown of all available usage statistics.
442
+ - **URL-based Configuration**: You can also specify the API endpoint and period directly via `endpoint` and `period` query parameters. For example:
443
+ `http://localhost:4141/usage-viewer?endpoint=http://your-api-server/usage&period=week`
444
+
445
+ ### Usage Viewer Screenshot
446
+
447
+ <p align="center">
448
+ <img src="./docs/screenshots/usage-viewer.png" alt="Copilot API usage viewer" width="900" />
449
+ </p>
450
+
451
+ ## Command Structure
452
+
453
+ Copilot API now uses a subcommand structure with these main commands:
454
+
455
+ - `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.
456
+ - `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.
457
+ - `debug`: Display diagnostic information including version, runtime details, file paths, and authentication status. Useful for troubleshooting and support.
458
+
459
+ ## Command Line Options
460
+
461
+ ### Global Options
462
+
463
+ The following options can be used with any subcommand. When passing them before the subcommand, use the `--key=value` form:
464
+
465
+ | Option | Description | Default | Alias |
466
+ | ----------------- | ------------------------------------------------------ | ------- | ----- |
467
+ | --api-home | Path to the API home directory (sets COPILOT_API_HOME) | none | none |
468
+ | --oauth-app | OAuth app identifier (sets COPILOT_API_OAUTH_APP) | none | none |
469
+ | --enterprise-url | Enterprise URL for GitHub (sets COPILOT_API_ENTERPRISE_URL) | none | none |
470
+
471
+ ### Start Command Options
472
+
473
+ The following command line options are available for the `start` command:
474
+
475
+ | Option | Description | Default | Alias |
476
+ | -------------- | ----------------------------------------------------------------------------- | ---------- | ----- |
477
+ | --port | Port to listen on | 4141 | -p |
478
+ | --verbose | Enable verbose logging | false | -v |
479
+ | --github-token | Provide GitHub token directly (must be generated using the `auth` subcommand) | none | -g |
480
+ | --claude-code | Generate a command to launch Claude Code with Copilot API config | false | -c |
481
+ | --show-token | Show GitHub and Copilot tokens on fetch and refresh | false | none |
482
+ | --proxy-env | Initialize proxy from environment variables | false | none |
483
+
484
+ ### Auth Command Options
485
+
486
+ | Option | Description | Default | Alias |
487
+ | ------------ | ------------------------- | ------- | ----- |
488
+ | --provider | Provider to log in with or configure (`copilot`, `codex`, `opencode-go`, `deepseek`, `dashscope`, `openrouter`, or `custom`) | prompt | none |
489
+ | --verbose | Enable verbose logging | false | -v |
490
+ | --show-token | Show GitHub token on auth | false | none |
491
+
492
+ 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.
493
+
494
+ Use `copilot-api auth login --provider deepseek`, `--provider dashscope`, `--provider openrouter`, or `--provider opencode-go` 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"`. OpenCode Go prompts for masked `apiKey` and prefilled `baseUrl` only, and writes `type: "openai-compatible"` (baseUrl `https://opencode.ai/zen/go`). After a provider is configured and enabled, `copilot-api start` can run without any GitHub token.
495
+
496
+ 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`.
497
+
498
+ ### Debug Command Options
499
+
500
+ | Option | Description | Default | Alias |
501
+ | ------ | ------------------------- | ------- | ----- |
502
+ | --json | Output debug info as JSON | false | none |
503
+
504
+ ## Configuration (config.json)
505
+
506
+ - **Location:** `~/.local/share/copilot-api/config.json` (Linux/macOS) or `%USERPROFILE%\.local\share\copilot-api\config.json` (Windows).
507
+ - **Default shape:**
508
+ ```json
509
+ {
510
+ "auth": {
511
+ "apiKeys": [],
512
+ "adminApiKey": "<auto-generated-on-startup>"
513
+ },
514
+ "providers": {},
515
+ "modelMappings": {},
516
+ "extraPrompts": {
517
+ "gpt-5-mini": "<built-in exploration prompt>",
518
+ "gpt-5.3-codex": "<built-in commentary prompt>",
519
+ "gpt-5.4-mini": "<built-in commentary prompt>",
520
+ "gpt-5.4": "<built-in commentary prompt>",
521
+ "gpt-5.5": "<built-in commentary prompt>"
522
+ },
523
+ "smallModel": "gpt-5-mini",
524
+ "useResponsesApiContextManagement": true,
525
+ "modelResponsesApiCompactThresholds": {
526
+ "gpt-5.4": 217600,
527
+ "gpt-5.5": 217600
528
+ },
529
+ "modelReasoningEfforts": {
530
+ "gpt-5-mini": "low",
531
+ "gpt-5.3-codex": "xhigh",
532
+ "gpt-5.4-mini": "xhigh",
533
+ "gpt-5.4": "xhigh",
534
+ "gpt-5.5": "xhigh"
535
+ },
536
+ "useMessagesApi": true,
226
537
  "useResponsesApiWebSocket": true,
227
538
  "useResponsesApiWebSearch": true,
228
539
  "messageApiWebSearchModel": "gpt-5-mini"
@@ -242,11 +553,12 @@ Use `copilot-api auth login --provider custom` to add or update another third-pa
242
553
  - `temperature` (optional): Default temperature value used when the request does not specify one.
243
554
  - `topP` (optional): Default top_p value used when the request does not specify one.
244
555
  - `topK` (optional): Default top_k value used when the request does not specify one.
245
- - `extraBody` (optional): Dynamic fields merged into the upstream request body for that model. Request body fields with the same name take precedence. OpenAI-compatible providers can use this for fields such as `enable_thinking`, `preserve_thinking`, `reasoning_effort`. `thinking_budget` is a special OpenAI-compatible provider override: when configured in `extraBody`, it is forced after Anthropic `thinking.budget_tokens` translation and overrides the request-derived budget.
556
+ - `extraBody` (optional): Dynamic fields merged into the upstream request body for that model. Request body fields with the same name take precedence. OpenAI-compatible providers can use this for fields such as `enable_thinking`, `preserve_thinking`, `reasoning_effort`. `thinking_budget` is a special OpenAI-compatible provider override: when configured in `extraBody`, it is forced after Anthropic `thinking.budget_tokens` translation and overrides the request-derived budget. For providers whose name is `dashscope` or whose `baseUrl` contains `aliyuncs.com`, the request-derived `thinking_budget` (from Anthropic `thinking.budget_tokens`) is forwarded upstream; for other OpenAI-compatible providers the request-derived `thinking_budget` is stripped, while an `extraBody` `thinking_budget` is still honored.
246
557
  - `pricing` (optional): Per-model token prices, in the provider `pricingCurrency`, per 1M tokens. Supported fields are `input`, `output`, `cachedInput` (implicit cache read), `explicitCachedInput` (explicit cache read), and `cacheCreationInput`. Use `tiers` with `maxInputTokens` for input-size tiered pricing.
247
- - `contextCache` (optional): Defaults to `true` for OpenAI-compatible providers. This enables Alibaba Cloud Model Studio/DashScope explicit context cache by injecting `cache_control: { "type": "ephemeral" }` on up to 4 content blocks using the Context Cache format. The cache breakpoint strategy matches opencode's main provider flow: the first 2 system messages plus the last 2 non-system messages. Marked string content is converted to text content part arrays for `system` / `user` / `assistant` / `tool` messages; existing array content is marked on the last part. Set this to `false` when the model already supports implicit caching, or when the upstream does not accept this explicit-cache extension field.
558
+ - `contextCache` (optional): Defaults to `true` for providers whose name is `dashscope` or whose `baseUrl` contains `aliyuncs.com`; defaults to `false` for other OpenAI-compatible providers. This enables Alibaba Cloud Model Studio/DashScope explicit context cache by injecting `cache_control: { "type": "ephemeral" }` on up to 4 content blocks using the Context Cache format. The cache breakpoint strategy matches opencode's main provider flow: the first 2 system messages plus the last 2 non-system messages. Marked string content is converted to text content part arrays for `system` / `user` / `assistant` / `tool` messages; existing array content is marked on the last part. Set this to `false` when the model already supports implicit caching, or when the upstream does not accept this explicit-cache extension field. Set this to `true` for non-DashScope providers that support the same explicit-cache extension.
248
559
  - `supportPdf` (optional): Controls whether the model supports PDF/document content. Defaults to `false`; unsupported PDFs are converted to a text notice. Set it to `true` to send PDF/document blocks as OpenAI Chat Completions file parts.
249
560
  - `toolContentSupportType` (optional): Tool result content capabilities for that model, as an array of `array`, `image`, and `pdf`. Provider routes default to string-only tool content when omitted. If `supportPdf` is `true` but this list does not include `pdf`, file parts in tool results are moved to user role messages. This provider default does not change the Copilot main flow, which continues to support array + image and not PDF.
561
+ - `type` (optional): Per-model override of the provider protocol type. Supports `anthropic`, `openai-compatible`, and `openai-responses`. When set, the provider's `/v1/messages` route uses this model's type instead of the provider-level type for request routing, auth header resolution, and upstream endpoint selection. This is useful for providers like OpenCode Go whose upstream supports both OpenAI-compatible and Anthropic Messages APIs for different models. When the type is overridden, the auth header is resolved from the overridden type's default (Anthropic defaults to `x-api-key`; OpenAI-compatible/Responses default to `authorization`).
250
562
 
251
563
  Example DashScope model settings:
252
564
  ```json
@@ -300,11 +612,11 @@ Use `copilot-api auth login --provider custom` to add or update another third-pa
300
612
  }
301
613
  }
302
614
  ```
303
- Built-in token prices cover Codex GPT models in USD, DashScope `qwen3.7-max`, `qwen3.7-plus`, `glm-5.1`, `glm-5.2` in CNY, and DeepSeek `deepseek-v4-flash`, `deepseek-v4-pro`, `deepseek-chat`, `deepseek-reasoner` in CNY. User `pricing` entries override built-ins. For DashScope, cached tokens are charged as explicit cache reads when the upstream usage includes `cache_creation_input_tokens`; otherwise `cachedInput` is used as the implicit cache read price. For DeepSeek, `prompt_cache_hit_tokens` map to cached input and `prompt_cache_miss_tokens` map to regular input.
615
+ Built-in token prices cover Codex GPT models in USD, DashScope `qwen3.7-max`, `qwen3.7-plus`, `glm-5.1`, `glm-5.2` in CNY, DeepSeek `deepseek-v4-flash`, `deepseek-v4-pro`, `deepseek-chat`, `deepseek-reasoner` in CNY, and OpenCode Go models (`glm-5.2`, `deepseek-v4-flash`, `deepseek-v4-pro`, `kimi-k2.7-code`, `mimo-v2.5`, `mimo-v2.5-pro`, `qwen3.7-plus`, `qwen3.7-max`, `minimax-m2.5`, `minimax-m3`) in USD. User `pricing` entries override built-ins. For DashScope, cached tokens are charged as explicit cache reads when the upstream usage includes `cache_creation_input_tokens`; otherwise `cachedInput` is used as the implicit cache read price. For DeepSeek, `prompt_cache_hit_tokens` map to cached input and `prompt_cache_miss_tokens` map to regular input.
304
616
  - **smallModel:** Fallback model used for tool-less warmup messages (e.g., Claude Code probe requests); defaults to gpt-5-mini.
305
617
  - **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
618
  - **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.
619
+ - **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
620
  - **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
621
  - **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
622
  - **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 +732,8 @@ curl http://localhost:4141/dashscope/v1/messages \
420
732
  -d '{"model":"qwen3.6-plus","max_tokens":1024,"messages":[{"role":"user","content":"hello"}]}'
421
733
  ```
422
734
 
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
735
  ## Usage Tips
735
736
 
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
737
  ### CLAUDE.md or AGENTS.md Recommended Content
742
738
 
743
739
  To add these reminders manually, include the following in `CLAUDE.md` for Claude Code, or `AGENTS.md` for opencode/codex: