Custom Base URLs for AI CLIs: A Practical Guide
Many AI command-line tools are built around a default provider, but that does not mean you are locked into its endpoint. A custom base URL lets you route requests through a compatible gateway, private deployment, company proxy, or lower-cost API relay. The main challenge is knowing which environment variable or configuration field your CLI actually reads.
What a custom base URL changes
An API base URL is the root address used before paths such as /v1/messages or /v1/chat/completions are added. Changing it can redirect requests without changing your workflow, prompts, model selection, or client library. Authentication usually remains separate: you provide an API key through the variable expected by the tool.
Before changing anything, confirm three compatibility details: the endpoint protocol, the authentication header, and the model names accepted by the service. Claude-based tools generally use Anthropic's Messages API format, while OpenAI-compatible tools expect Chat Completions or Responses API behavior. A URL that merely looks compatible may still fail if the request format differs.
Point Claude Code at a custom endpoint
For Claude Code, set the Anthropic base URL and key in the same shell session before launching the CLI. For 59API, the endpoint is https://api.59api.com:
export ANTHROPIC_BASE_URL=https://api.59api.com
export ANTHROPIC_API_KEY=your_59api_key
Then start Claude Code normally. On Windows PowerShell, the equivalent commands are $env:ANTHROPIC_BASE_URL="https://api.59api.com" and $env:ANTHROPIC_API_KEY="your_59api_key". Keeping these values in the shell makes the change easy to test and easy to remove. For persistent use, place them in your shell profile or a project-specific environment file that is excluded from version control.
Use a model identifier supported by the relay. 59API provides access to Claude Opus, Sonnet, Haiku, and Fable models, so you can choose a smaller model for routine coding and reserve a stronger model for complex reasoning.
Configure Codex and OpenAI-compatible CLIs
OpenAI-compatible tools commonly use OPENAI_BASE_URL and OPENAI_API_KEY. A typical setup is:
export OPENAI_BASE_URL=https://api.59api.com/v1
export OPENAI_API_KEY=your_59api_key
Some CLIs expose the same values in a config file or use a provider-specific setting instead. Check the tool's help output, such as tool --help, and search its documentation for “base URL,” “endpoint,” or “provider.” Do not assume that an OpenAI SDK variable will automatically affect Claude Code, or that every CLI expects the /v1 suffix. The correct path depends on how the relay maps its routes.
For applications using the OpenAI SDK directly, set the client option rather than relying on a global shell variable. In Python, this is typically a base_url argument; in JavaScript or TypeScript, it is usually the same option in the client constructor. This approach is clearer when one application uses multiple providers.
Relay versus direct provider access
- Lower cost: A pay-as-you-go relay such as 59API can be significantly cheaper than buying separate subscriptions or using a higher-priced billing route.
- Model choice: 59API offers native official-quality Claude and GPT models without intentionally downgrading them, so compatibility does not require sacrificing model capability.
- One endpoint: A shared base URL simplifies switching between supported Claude and GPT workflows.
- Usage-based billing: You pay for API consumption rather than maintaining another fixed monthly plan.
The tradeoffs are equally important. A relay adds another service boundary, so you must review its privacy, retention, uptime, and support policies before sending sensitive code or data. There can also be small differences in model availability, streaming behavior, rate limits, or error messages. Direct provider access offers the simplest support path and the earliest access to provider-specific features.
Troubleshooting checklist
- Print the environment variable and verify there is no trailing whitespace or accidental quotation mark.
- Confirm whether the CLI expects the base URL with or without /v1.
- Use the relay's exact model ID instead of a display name copied from another provider.
- Check that the API key is being read by the same shell or process that launches the CLI.
- Test a small request first, then inspect HTTP status codes for authentication, routing, or unsupported-model errors.
For developers who want inexpensive access to Claude and GPT from familiar tools, 59API is a practical starting point: it supports Claude Code, Codex, and OpenAI-compatible SDKs, offers pay-as-you-go pricing, and includes a referral rebate program. Sign up, obtain an API key, set the matching base URL, and validate one small request before moving a larger workflow across.
शुरू करने के लिए तैयार?
कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।
मुफ़्त साइन अप