-
Notifications
You must be signed in to change notification settings - Fork 154
feat: support custom baseURL via opencode.json and ANTHROPIC_BASE_URL… #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -82,6 +82,38 @@ The plugin checks these in order: | |||||
| 1. macOS Keychain (all `Claude Code-credentials*` entries — multiple accounts are detected automatically) | ||||||
| 2. `~/.claude/.credentials.json` (fallback, works on all platforms) | ||||||
|
|
||||||
| ## Custom base URL / Proxy support | ||||||
|
|
||||||
| You can route Anthropic API requests through a proxy or custom endpoint by configuring the `baseURL` option in your `opencode.json`: | ||||||
|
|
||||||
| ```json | ||||||
| { | ||||||
| "provider": { | ||||||
| "anthropic": { | ||||||
| "options": { | ||||||
| "baseURL": "https://your-proxy.example.com/v1", | ||||||
| "headers": { | ||||||
| "X-Custom-Header": "value" | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| }, | ||||||
| "plugin": ["opencode-claude-auth@latest"] | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| The plugin respects the following priority (highest to lowest): | ||||||
|
|
||||||
| 1. `provider.anthropic.options.baseURL` from `opencode.json` | ||||||
| 2. `ANTHROPIC_BASE_URL` environment variable | ||||||
| 3. Default: `https://api.anthropic.com/v1` | ||||||
|
|
||||||
| Any custom headers you define in `provider.anthropic.options.headers` are automatically forwarded to all API requests. This is useful for: | ||||||
|
|
||||||
| - Using an HTTP proxy or API gateway | ||||||
| - Adding authentication headers (e.g., `X-Gateway-Key`) | ||||||
| - Routing through a load balancer or monitoring proxy | ||||||
|
|
||||||
| ## Multiple accounts (macOS) | ||||||
|
|
||||||
| If you have [multiple Claude Code accounts](https://gist.github.com/KMJ-007/0979814968722051620461ab2aa01bf2) authenticated on macOS, the plugin detects all of them from the Keychain automatically. Each account is labeled by its subscription tier (Claude Pro, Claude Max, etc.). | ||||||
|
|
@@ -182,6 +214,7 @@ All configurable parameters can be overridden via environment variables. If Anth | |||||
|
|
||||||
| | Variable | Description | Default | | ||||||
| | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | | ||||||
| | `ANTHROPIC_BASE_URL` | Custom base URL for Anthropic API requests (e.g., proxy or custom endpoint). Takes precedence over config file. | `https://api.anthropic.com/v1` | | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: README.md
Line: 217
Comment:
The description incorrectly says the env var takes precedence over the config file. The actual priority (reflected both in the code and the priority list above the table) is config file > env var > default.
```suggestion
| `ANTHROPIC_BASE_URL` | Custom base URL for Anthropic API requests (e.g., proxy or custom endpoint). Overridden by `provider.anthropic.options.baseURL` in `opencode.json` when both are set. | `https://api.anthropic.com/v1` |
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||
| | `ANTHROPIC_CLI_VERSION` | Claude CLI version for user-agent and billing headers | `2.1.80` | | ||||||
| | `ANTHROPIC_USER_AGENT` | Full User-Agent string (overrides CLI version) | `claude-cli/{version} (external, cli)` | | ||||||
| | `ANTHROPIC_BETA_FLAGS` | Comma-separated beta feature flags | `claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,prompt-caching-scope-2026-01-05` | | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -318,9 +318,14 @@ const plugin: Plugin = async () => { | |
| modelCount: Object.keys(provider.models).length, | ||
| }) | ||
|
|
||
| const effectiveBaseURL = | ||
| provider.options?.baseURL || | ||
| process.env.ANTHROPIC_BASE_URL || | ||
| "https://api.anthropic.com/v1" | ||
|
|
||
| return { | ||
| apiKey: "", | ||
| baseURL: "https://api.anthropic.com/v1", | ||
| baseURL: effectiveBaseURL, | ||
|
Comment on lines
+321
to
+328
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The README and PR description prominently state that Prompt To Fix With AIThis is a comment left during a code review.
Path: src/index.ts
Line: 321-328
Comment:
**Custom headers from `provider.options.headers` are documented but not forwarded**
The README and PR description prominently state that `provider.anthropic.options.headers` entries are "automatically forwarded to all API requests" (useful for `X-Gateway-Key` etc.). However, only `provider.options?.baseURL` is consumed here — `provider.options?.headers` is never read. `buildRequestHeaders` takes its input only from the `init` argument supplied by the framework, not from provider config headers. Any user who configures headers in `opencode.json` expecting them to reach the proxy will receive a silent failure.
How can I resolve this? If you propose a fix, please make it concise. |
||
| async fetch(input: RequestInfo | URL, init?: RequestInit) { | ||
| const latest = getCachedCredentials() | ||
| if (!latest) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The table entry for
ANTHROPIC_BASE_URLsays "Takes precedence over config file," but the code and the priority section above it both say the opposite:provider.anthropic.options.baseURLwins over the env var. A user reading only the table will believe they can override a config-file URL with the env var, try it, and find it silently doesn't work.Prompt To Fix With AI