#7821: feat: Support ANTHROPIC_BASE_URL environment variable for custom endpoints
docker
agents
stale
Cluster:
Model Configuration Enhancements
## Summary
Allow users to configure custom Anthropic API endpoints via the `ANTHROPIC_BASE_URL` environment variable. This enables using alternative API providers that are compatible with the Anthropic Messages API.
## Usage
```bash
export ANTHROPIC_BASE_URL="http://your-custom-endpoint:8045"
export ANTHROPIC_API_KEY="your-api-key"
openclaw agent --message "Hello" --local
```
## Changes
- `src/agents/model-compat.ts`: Added `resolveAnthropicBaseUrl()` function to read `ANTHROPIC_BASE_URL` env var and inject it into Anthropic model config
- `src/agents/model-compat.test.ts`: Added 6 test cases covering various scenarios
## Implementation Details
The `normalizeModelCompat()` function now:
1. Detects `model.provider === "anthropic"`
2. Reads `process.env.ANTHROPIC_BASE_URL`
3. Overrides default baseUrl (`https://api.anthropic.com`) when env var is set
4. Preserves explicitly configured custom baseUrl (non-default values)
## Test plan
- [x] Unit tests pass (`pnpm test src/agents/model-compat.test.ts`)
- [x] Manual testing with custom endpoint confirmed working
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adds support for configuring Anthropic-compatible endpoints via the `ANTHROPIC_BASE_URL` environment variable. It introduces a small resolver (`resolveAnthropicBaseUrl`) and updates `normalizeModelCompat` to inject a custom `baseUrl` for `provider === "anthropic"` when the env var is set, while preserving an explicitly configured non-default `baseUrl`. It also adds unit tests covering env-var override/precedence and whitespace trimming.
The changes live in the shared model normalization path (`src/agents/model-compat.ts`), which is used to apply provider-specific compatibility tweaks (previously only z.ai/OpenAI-completions developer-role handling).
<h3>Confidence Score: 3/5</h3>
- Reasonably safe, but there is a likely breaking behavioral change in `normalizeModelCompat` that could affect call sites.
- Core logic is small and tests cover the new env-var behavior, but the refactor changed `normalizeModelCompat` from mutating to returning a new object for the z.ai path. If any caller relies on in-place mutation (doesn’t use the return value), behavior will silently change. The ANTHROPIC_BASE_URL behavior itself looks consistent and well-tested.
- src/agents/model-compat.ts (verify call sites don’t depend on in-place mutation)
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#11938: feat: support ANTHROPIC_AUTH_TOKEN environment variable
by EricMymj · 2026-02-08
81.4%
#13650: feat(agents): support env var overrides for default provider and model
by xrehpicx · 2026-02-10
76.5%
#10346: Add Anthropic to implicit model catalog (Opus 4.6 allowlist)
by nicks2bo · 2026-02-06
75.9%
#10108: fix: override stale Anthropic OAuth stealth headers for Opus 4.6
by CivilBooks · 2026-02-06
74.9%
#5945: fix: use configured model for slug generator (AI-assisted)
by HEDELKA · 2026-02-01
74.0%
#10492: fix(auth): store Anthropic setup-token as type:oauth for auto-refresh
by sparck75 · 2026-02-06
73.8%
#9163: Fix: Save Anthropic setup token to config file
by vishaltandale00 · 2026-02-04
73.8%
#13976: fix(anthropic): include Anthropic in tool call ID sanitization
by omair445 · 2026-02-11
73.6%
#4793: hooks: use configured model for slug generator
by yoyooyooo · 2026-01-30
73.5%
#2806: [AI-Assisted] Fix: Repair tool_use/tool_result pairing for Claude o...
by Arthur742Ramos · 2026-01-27
73.2%