feat: support ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN and SPACEBOT_MODEL env vars - #135
Conversation
…MODEL env vars Add environment variable support for custom Anthropic API endpoints and authentication tokens, enabling use with API proxies (LiteLLM, Azure AI Gateway, corporate proxies). New environment variables: - ANTHROPIC_BASE_URL: override the Anthropic API endpoint - ANTHROPIC_AUTH_TOKEN: alternative to ANTHROPIC_API_KEY for proxy auth - SPACEBOT_MODEL: override all process types with a single model Also fixes a bug where build_anthropic_request() hardcoded the API URL instead of using the provider's configured base_url, making custom base URLs via TOML config ineffective. Closes spacedriveapp#132
|
any chance you could also add:
|
|
@adryserage @jamiepine running latest release. With the following config I'm receiving auth errors:
[api]
bind = "::"
[llm]
[llm.providers.anthropic]
api_type = "anthropic"
base_url = "env:ANTHROPIC_BASE_URL"
api_key = "env:ANTHROPIC_AUTH_TOKEN"
[messaging.discord]
enabled = true
token = "env:DISCORD_BOT_TOKEN"
[[agents]]
id = "main"
default = true
[agents.routing]
channel = "anthropic/claude-opus-4-6"
branch = "anthropic/claude-opus-4-6"
worker = "anthropic/claude-sonnet-4-5"
compactor = "anthropic/claude-sonnet-4-5"
cortex = "anthropic/claude-opus-4-6"
voice = ""
rate_limit_cooldown_secs = 60
[defaults]
brave_search_key = "env:BRAVE_SEARCH_API_KEY"
[[bindings]]
agent_id = "main"
channel = "discord"
require_mention = trueThis is also the case with variations such as: [llm]
anthropic_key = "env:ANTHROPIC_AUTH_TOKEN"
[llm.providers.anthropic]
api_type = "anthropic"
base_url = "env:ANTHROPIC_BASE_URL"
api_key = "env:ANTHROPIC_AUTH_TOKEN"or the |
|
I've been testing this PR with a corporate Anthropic-compatible proxy and I'm consistently getting 403 errors. I believe the issue is that when Could
This would align with how Anthropic's own tooling distinguishes between the two variables. See the Claude Code third-party integrations docs for reference. |
When ANTHROPIC_AUTH_TOKEN is the credential source (instead of ANTHROPIC_API_KEY), proxy endpoints expect Authorization: Bearer rather than x-api-key. This matches Claude Code's behavior. Adds a ProxyBearer auth path that sends Bearer without Claude Code identity headers (user-agent, x-app, oauth beta). The auth source is tracked via use_bearer_auth on ProviderConfig, set automatically when the key originates from ANTHROPIC_AUTH_TOKEN. Fixes the 403 errors reported in spacedriveapp#135 when using corporate proxies.
- Add new AnthropicAuthPath::AuthToken variant for proxy tokens - Track is_auth_token in ProviderConfig when key comes from ANTHROPIC_AUTH_TOKEN - Pass is_auth_token through build_anthropic_request to apply_auth_headers - AuthToken uses Authorization: Bearer without Claude Code identity headers - Add ANTHROPIC_MODEL env var to set all anthropic/* routes - Update tests for new AuthToken variant Fixes 403 errors when using ANTHROPIC_AUTH_TOKEN with corporate proxies. Closes discussion in spacedriveapp#135 about auth header mismatch. Note: CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC is deferred for future work.
…ropic-env-vars feat: support ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN and SPACEBOT_MODEL env vars
- Add new AnthropicAuthPath::AuthToken variant for proxy tokens - Track is_auth_token in ProviderConfig when key comes from ANTHROPIC_AUTH_TOKEN - Pass is_auth_token through build_anthropic_request to apply_auth_headers - AuthToken uses Authorization: Bearer without Claude Code identity headers - Add ANTHROPIC_MODEL env var to set all anthropic/* routes - Update tests for new AuthToken variant Fixes 403 errors when using ANTHROPIC_AUTH_TOKEN with corporate proxies. Closes discussion in spacedriveapp#135 about auth header mismatch. Note: CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC is deferred for future work.
When ANTHROPIC_AUTH_TOKEN is the credential source (instead of ANTHROPIC_API_KEY), proxy endpoints expect Authorization: Bearer rather than x-api-key. This matches Claude Code's behavior. Adds a ProxyBearer auth path that sends Bearer without Claude Code identity headers (user-agent, x-app, oauth beta). The auth source is tracked via use_bearer_auth on ProviderConfig, set automatically when the key originates from ANTHROPIC_AUTH_TOKEN. Fixes the 403 errors reported in spacedriveapp#135 when using corporate proxies.
Summary
ANTHROPIC_BASE_URLenv var to override the Anthropic API endpointANTHROPIC_AUTH_TOKENenv var as an alternative toANTHROPIC_API_KEYfor proxy authSPACEBOT_MODELenv var to override all process types with a single modelbuild_anthropic_request()hardcoded the API URL, ignoring configuredbase_urlWhy
Many organizations run Anthropic-compatible API proxies (LiteLLM, Azure AI Gateway, corporate proxies). This makes Spacebot usable in enterprise and self-hosted environments where direct API access isn't available.
New Environment Variables
ANTHROPIC_BASE_URLhttps://api.anthropic.comANTHROPIC_AUTH_TOKENANTHROPIC_API_KEY)SPACEBOT_MODELThese are non-breaking — existing
ANTHROPIC_API_KEYbehavior stays the same.Changes
src/llm/anthropic/params.rsANTHROPIC_API_URLconstantbase_urlparameter tobuild_anthropic_request()messages_url()helper that appends/v1/messagesto the base URLsrc/llm/model.rsprovider_config.base_urltobuild_anthropic_request()src/config.rsANTHROPIC_AUTH_TOKENas fallback forANTHROPIC_API_KEYin bothload_from_env()andfrom_toml()ANTHROPIC_BASE_URLoverrides the default base URL when registering the Anthropic providerSPACEBOT_MODELsets all routing process types (channel, branch, worker, compactor, cortex); specific vars likeSPACEBOT_CHANNEL_MODELtake precedenceBug Fix
Previously,
build_anthropic_request()always posted tohttps://api.anthropic.com/v1/messagesregardless of whatbase_urlwas configured inProviderConfig. This meant custom base URLs set via[llm.provider.anthropic]TOML config were silently ignored. Now the provider'sbase_urlis passed through and used.Test plan
ANTHROPIC_API_KEYstill works (no regression)ANTHROPIC_AUTH_TOKENas sole credentialANTHROPIC_BASE_URLwith a proxy endpointSPACEBOT_MODELoverrides all process typesSPACEBOT_CHANNEL_MODELtakes precedence overSPACEBOT_MODELCloses #132