Skip to content

feat: support ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN and SPACEBOT_MODEL env vars - #135

Merged
jamiepine merged 1 commit into
spacedriveapp:mainfrom
adryserage:feat/custom-anthropic-env-vars
Feb 22, 2026
Merged

feat: support ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN and SPACEBOT_MODEL env vars#135
jamiepine merged 1 commit into
spacedriveapp:mainfrom
adryserage:feat/custom-anthropic-env-vars

Conversation

@adryserage

Copy link
Copy Markdown
Contributor

Summary

  • Add ANTHROPIC_BASE_URL env var to override the Anthropic API endpoint
  • Add ANTHROPIC_AUTH_TOKEN env var as an alternative to ANTHROPIC_API_KEY for proxy auth
  • Add SPACEBOT_MODEL env var to override all process types with a single model
  • Fix bug where build_anthropic_request() hardcoded the API URL, ignoring configured base_url

Why

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

Variable Purpose Default
ANTHROPIC_BASE_URL Custom API endpoint URL https://api.anthropic.com
ANTHROPIC_AUTH_TOKEN Bearer token for proxy auth (fallback if no ANTHROPIC_API_KEY)
SPACEBOT_MODEL Override all process types at once

These are non-breaking — existing ANTHROPIC_API_KEY behavior stays the same.

Changes

src/llm/anthropic/params.rs

  • Remove hardcoded ANTHROPIC_API_URL constant
  • Add base_url parameter to build_anthropic_request()
  • Add messages_url() helper that appends /v1/messages to the base URL

src/llm/model.rs

  • Pass provider_config.base_url to build_anthropic_request()

src/config.rs

  • ANTHROPIC_AUTH_TOKEN as fallback for ANTHROPIC_API_KEY in both load_from_env() and from_toml()
  • ANTHROPIC_BASE_URL overrides the default base URL when registering the Anthropic provider
  • SPACEBOT_MODEL sets all routing process types (channel, branch, worker, compactor, cortex); specific vars like SPACEBOT_CHANNEL_MODEL take precedence

Bug Fix

Previously, build_anthropic_request() always posted to https://api.anthropic.com/v1/messages regardless of what base_url was configured in ProviderConfig. This meant custom base URLs set via [llm.provider.anthropic] TOML config were silently ignored. Now the provider's base_url is passed through and used.

Test plan

  • Verify existing ANTHROPIC_API_KEY still works (no regression)
  • Test ANTHROPIC_AUTH_TOKEN as sole credential
  • Test ANTHROPIC_BASE_URL with a proxy endpoint
  • Test SPACEBOT_MODEL overrides all process types
  • Test SPACEBOT_CHANNEL_MODEL takes precedence over SPACEBOT_MODEL
  • All 26 existing Anthropic tests pass

Closes #132

…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
@pjv

pjv commented Feb 22, 2026

Copy link
Copy Markdown

any chance you could also add:

  • ANTHROPIC_MODEL
  • CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC

@jamiepine
jamiepine merged commit fa635b3 into spacedriveapp:main Feb 22, 2026
@worldofgeese

Copy link
Copy Markdown

@adryserage @jamiepine running latest release. With the following config I'm receiving auth errors:

spacebot | 2026-02-23T07:22:45.804280Z ERROR handle_message: spacebot::agent::channel: message=channel LLM call failedchannel_id=discord:1464919871170023568:1475064317324824699error=CompletionError: ProviderError: Anthropic API error (403 Forbidden): unknown error channel_id=discord:1464919871170023568:1475064317324824699agent_id=mainmessage_id=1475392417254342849

[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 = true

This 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 [llm.providers.anthropic] block omitted entirely.

@worldofgeese

Copy link
Copy Markdown

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 ANTHROPIC_AUTH_TOKEN is used, the request still sends the key via the x-api-key header (Anthropic's native auth). However, most proxy endpoints expect Authorization: Bearer <token> instead — this is how Claude Code handles it when ANTHROPIC_AUTH_TOKEN is set (as opposed to ANTHROPIC_API_KEY).

Could build_anthropic_request() be updated to detect which env var was used and switch the auth header accordingly? Something like:

  • ANTHROPIC_API_KEYx-api-key: <key> (current behavior, native Anthropic)
  • ANTHROPIC_AUTH_TOKENAuthorization: Bearer <token> (proxy-compatible, matches Claude Code behavior)

This would align with how Anthropic's own tooling distinguishes between the two variables. See the Claude Code third-party integrations docs for reference.

worldofgeese pushed a commit to worldofgeese/spacebot that referenced this pull request Feb 24, 2026
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.
worldofgeese pushed a commit to worldofgeese/spacebot that referenced this pull request Feb 24, 2026
- 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.
rktmeister pushed a commit to rktmeister/spacebot that referenced this pull request Mar 11, 2026
…ropic-env-vars

feat: support ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN and SPACEBOT_MODEL env vars
rktmeister pushed a commit to rktmeister/spacebot that referenced this pull request Mar 11, 2026
- 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.
rktmeister pushed a commit to rktmeister/spacebot that referenced this pull request Mar 11, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support custom API base URL and auth token variables (ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN)

4 participants