How to Set ANTHROPIC_BASE_URL and an API Key in Claude Code
Configure ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN in Claude Code, prevent conflicting settings, and verify the connection without exposing your key.

To connect Claude Code to BetterToken, set ANTHROPIC_BASE_URL=https://bettertoken.ai with no /v1, and pass your own API key through ANTHROPIC_AUTH_TOKEN. A practical default is to keep both values in your user-level ~/.claude/settings.json, where they apply across projects without placing credentials in a repository.
The supported configuration and VS Code options are documented in the current BetterToken guide for Claude Code. BetterToken provides separate, pay-as-you-go API access in this setup. An API key does not become a Claude subscription or change the rules of your Anthropic account.
If you do not have a key yet, first sign in to BetterToken Workspace, create your own API Key, and check the current Claude Code guide for the group or mapping required by your selected model. Do not use a shared team key or an old group name copied from someone else’s example: the configuration below assumes that you already have your own suitable key.
The two values you need
Claude Code uses the Anthropic protocol. Its Base URL is therefore different from the OpenAI-compatible URL used by Codex and similar clients: `https://www.bettertoken.ai/v1%60.?utm_source=blog&utm_medium=organic_content&utm_campaign=SEO-015&utm_content=anthropic-base-url-api-key-nastroyka
Step 1: Remove conflicting environment variables
Check whether old values are still set before changing the configuration:
Do not print the token itself. If these variables are set in the current shell and should no longer override the configuration file, unset them:
Then inspect ~/.zshrc, ~/.bashrc, .env files, IDE settings, and any provider manager you use. A process can retain the environment it inherited at startup even after you edit the file that originally defined a variable.
Step 2: Add the user-level configuration
According to the official Claude Code settings reference, user settings live in ~/.claude/settings.json, shared project settings in .claude/settings.json, and local project settings in .claude/settings.local.json.
Add this configuration for BetterToken:
Replace only YOUR_API_KEY. If the file already contains permissions, hooks, plugins, or other fields, do not overwrite the entire file. Add or merge the env object and keep the result as valid JSON.
Restrict access to the file, then confirm that the permissions were applied:
The ls -l output must not grant read or write permission to group or other users. Do not attach the complete file to an issue. For team setups, do not publish one shared production token; each person should use their own key.
Step 3: Restart Claude Code
Exit the current Claude Code process completely, then start claude again. Opening a new terminal tab without restarting an already running Claude Code process is not enough: the process keeps the environment it received at startup.
The VS Code extension has a separate configuration point, claudeCode.environmentVariables, in VS Code settings.json. Do not assume that the terminal shell and the extension always read the same variables.
Step 4: Verify the connection with a small task
Start Claude Code in a test directory and send a safe request:
Note the current time before sending the request. The setup is working when:
- the response arrives without
401,403,ConnectionRefused, ormodel not found; - a new entry with a timestamp after the test began appears in your BetterToken Workspace;
- that entry shows the expected model, status, and usage;
- Claude Code does not fall back to the previous provider after the restart.
A successful reply alone does not prove which route handled the request: a configuration conflict may have sent it through another provider. The new Workspace entry for the test request is the confirmation. Open your working repository only after confirming that a new Workspace entry with a timestamp after the test began is present.
How to find conflicting settings
Do not assume a universal precedence order. The effective configuration depends on how Claude Code was launched, any managed policy, and the environment already inherited by the process. First locate every source that contains the relevant setting names:
The command prints file names only, not the token value. Also inspect organization-managed settings, the VS Code extension, and any external provider manager involved in the launch. Then change one source at a time, restart the client completely, and repeat the small request while checking for a new entry in the Workspace.
Common errors
ConnectionRefused or a connection to the wrong endpoint
Check the address literally: https://bettertoken.ai, with no /v1, no /messages, and no trailing space. Claude Code adds the required protocol path itself.
401 or authentication failure
Create a new key if you suspect exposure, copy it without surrounding spaces, and confirm that Claude Code is reading ANTHROPIC_AUTH_TOKEN rather than a variable intended for another client. Never send the token to support in plain text.
Changes do not take effect
Close every Claude Code process, check for old values with printenv, and start the client again. In VS Code, run Reload Window or restart the extension.
model not found
Do not copy an arbitrary Model ID from an old article. For keys and models that require explicit mapping, get the current Model ID from Setup or the current Claude Code configuration guide.
Quick checklist
- The Claude Code Base URL does not contain
/v1. - The real key is not stored in Git or visible in a screenshot.
- Every source of old settings has been found and verified one at a time.
- Claude Code has been restarted completely.
- A small read-only request appears in the Workspace.
Once all five checks pass, continue with the real task. If one fails, open the step-by-step Claude Code setup guide, select your client, and compare the fields one at a time instead of replacing the entire configuration.