← Back to PRs

#7821: feat: Support ANTHROPIC_BASE_URL environment variable for custom endpoints

by y1y2u3u4 open 2026-02-03 07:16 View on GitHub →
docker agents stale
## 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