Set Up Codex with a Custom OpenAI Base URL
Why use a custom base URL for Codex and OpenAI SDKs?
If you are building with Codex or any OpenAI-compatible SDK, the fastest way to reduce API spend is often not to change your app logic, but to change where your requests go. A custom base URL lets you route the same code through a different provider while keeping the same request format. That means you can keep your existing tooling, environment variables, and model calls, but pay less per request.
59API is an AI API relay built for exactly this use case. It supports Claude models like Opus, Sonnet, Haiku, and Fable, plus GPT models, and it works with Claude Code, Codex, and any OpenAI SDK. Its base URL is https://api.59api.com, and because it offers pay-as-you-go pricing with native, official-quality models and no downgrade, it is a practical option for teams that want lower cost without rewriting integrations.
What you save in real terms
Cost optimization only matters if the numbers make sense. Here is the simple math:
- If your workflow uses 100 million input tokens per month and the relay saves you even $0.20 per million tokens, that is $20 saved monthly.
- If your team runs multiple agents, code reviews, and test-generation jobs, savings can quickly reach $50 to $300 per month depending on volume.
- For higher-throughput projects, a 15% to 30% lower effective rate can mean hundreds or even thousands saved per year.
The advantage of a relay like 59API is that you do not need to compromise on compatibility. You are not reworking prompts or changing application behavior; you are swapping the endpoint and, if needed, the API key.
Set up Codex with a custom base URL
Most Codex and OpenAI-compatible tools read the endpoint from an environment variable. The common pattern is to point the client at the relay instead of the default OpenAI host.
- Base URL: https://api.59api.com
- API key: Use the key issued by 59API
- Model names: Keep using supported model IDs from the provider documentation
Example shell setup:
- macOS/Linux: export OPENAI_BASE_URL=https://api.59api.com
- macOS/Linux: export OPENAI_API_KEY=your_59api_key
- Windows PowerShell: $env:OPENAI_BASE_URL="https://api.59api.com"
- Windows PowerShell: $env:OPENAI_API_KEY="your_59api_key"
If your tool expects a different variable name, look for settings such as base_url, api_base, or endpoint. The idea is the same: keep the client library unchanged and swap the destination.
OpenAI SDK example: minimal code change
In many OpenAI SDKs, you only need to change the base URL and API key. For example, in JavaScript or TypeScript, the configuration often looks like this:
- baseURL: "https://api.59api.com"
- apiKey: process.env.OPENAI_API_KEY
In Python, the equivalent setup is similarly small. The important point is that your application logic, prompts, retries, and streaming code can stay the same. That lowers migration risk and makes it easy to test cost savings in a staging environment before rolling out to production.
How to use 59API for lower-cost development
59API is attractive when you care about both price and compatibility. It is among the cheapest relay options, but the real advantage is that it keeps the model experience close to native quality. That matters for code generation, long-context debugging, automated refactors, and agentic workflows where small quality drops can create costly rework.
- For daily coding: Use cheaper models for routine tasks like summarization, doc generation, and simple transformations.
- For hard problems: Route complex reasoning or architecture tasks to stronger models only when necessary.
- For CI automation: Use the relay for test creation, lint explanations, and PR review comments to keep per-build costs down.
This tiered approach makes the most of pay-as-you-go pricing. You do not overspend on every call, and you can still reserve premium models for the jobs where they matter most.
Practical optimization tips
- Trim prompts: Remove repeated system text and keep instructions concise. Saving 500 tokens per call adds up fast across thousands of requests.
- Cap output: Set reasonable max output limits so you do not pay for verbose completions you do not need.
- Cache stable responses: For repeated lookups, reuse prior results instead of re-querying the model.
- Monitor usage: Track token counts by workflow so you can identify the most expensive jobs.
- Benchmark by task: Compare cost per successful result, not just raw token price.
When 59API makes the most sense
Choose 59API if you want an OpenAI-compatible setup, low operational friction, and better unit economics. It is especially useful for solo developers, startups, and teams building internal tools that send many small requests. The referral rebate can also lower effective cost further, which is helpful if you plan to onboard teammates or share the platform with other developers.
If you are ready to test the savings, sign up, point your Codex or SDK client to https://api.59api.com, and run a small benchmark on your most common workflow. In many teams, the switch takes minutes and the savings show up in the first billing cycle.
Ready to get started?
Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.
Sign up free