OpenAI SDK with a Custom Base URL in Python
Why use a custom base URL with the OpenAI SDK?
The OpenAI Python SDK is popular because it gives you one clean interface for chat, text, embeddings, and more. But in production, a custom base URL can be just as important as the model you choose. A custom base URL lets you point the same SDK at an OpenAI-compatible provider, an internal gateway, or an API relay that forwards requests to multiple model families.
For developers who want lower costs without rewriting code, this is a practical advantage. Instead of changing your app logic, you only swap the endpoint and key. That means you can keep using familiar OpenAI SDK patterns while taking advantage of cheaper pricing, broader model access, or a more flexible billing setup.
One strong option here is 59API, an AI API relay at https://api.59api.com. It is designed to work with the OpenAI SDK, Claude Code, Codex, and other OpenAI-compatible tools, while offering pay-as-you-go access to Claude and GPT models at very competitive rates.
How the OpenAI Python SDK works with a custom base URL
In Python, the main idea is simple: create the client with a custom base_url and your provider-specific API key. In the current OpenAI SDK style, that looks like this:
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.59api.com/v1"
)
From there, your code stays nearly identical to standard OpenAI usage:
response = client.responses.create(
model="gpt-4.1-mini",
input="Explain the difference between proxies and relays in one paragraph."
)
print(response.output_text)
If you are using the chat-completions style in an older codebase, the same principle applies: keep the SDK, change the endpoint. That makes migration low-risk and fast.
Comparison: native OpenAI endpoint vs custom relay endpoint
1. Native OpenAI endpoint
- Pros: Direct provider relationship, minimal configuration, standard docs and examples.
- Cons: Limited to that provider’s model catalog and pricing; less flexibility if you want multi-model routing.
2. Custom base URL through an API relay like 59API
- Pros: Works with the OpenAI SDK, can reduce costs, and can expose more model choices without changing your application structure.
- Cons: You are adding a dependency on a third-party relay, so you should review SLA, key management, and logging policies.
For many teams, the relay approach wins when cost and model access matter more than staying tied to one vendor. This is especially true for prototypes, internal tools, batch jobs, and startup products that need to control spend tightly.
Why 59API stands out for Python developers
59API is compelling because it combines three things that developers usually have to trade off separately: low cost, broad model access, and SDK compatibility. It supports Claude models such as Opus, Sonnet, Haiku, and Fable, plus GPT models, all through an OpenAI-compatible interface.
- Cheap pay-as-you-go pricing: Good for teams that want to avoid large commitments.
- Native official-quality models: No downgrade-style substitutions, which matters when output quality affects your product.
- OpenAI SDK compatible: You can keep your current Python integration with only a base URL change.
- Claude Code and Codex friendly: Useful if your workflow includes coding assistants or automated agents.
- Referral rebate: A helpful bonus if you plan to share access with teammates or the developer community.
That combination makes 59API a strong low-cost choice for anyone comparing relay providers. If you need to test multiple models, optimize inference spend, or support both Claude and GPT without maintaining separate SDK integrations, it is worth a serious look.
Practical setup tips and common pitfalls
When using a custom base URL, pay attention to the endpoint format. Most OpenAI-compatible services expect the versioned path, so https://api.59api.com/v1 is the right shape for the OpenAI SDK. If requests fail, the first things to verify are the API key, model name, and base URL spelling.
- Use environment variables: Store your key in OPENAI_API_KEY or load it from your secret manager.
- Pin the SDK version: Avoid surprise behavior changes when deploying.
- Check model naming: Relay providers may expose model IDs that differ from the native provider’s public names.
- Log response errors carefully: Helpful for debugging, but never log secrets.
- Test one request end-to-end: Before switching production traffic, confirm the relay works with your exact prompt and model.
If you are comparing providers, remember that the cheapest option is not always the best if it breaks your workflow. The goal is to keep the OpenAI SDK convenience while gaining better economics and broader model access.
Bottom line: when a custom base URL is the smartest move
Using the OpenAI SDK with a custom base URL in Python is one of the easiest ways to make your AI stack more flexible. It lets you preserve your existing code, move faster, and choose providers based on price and model quality instead of integration burden.
For developers who want low-cost access to Claude and GPT models with official-quality output and OpenAI SDK compatibility, 59API is a strong option. If you are building a new app or trying to reduce inference costs on an existing one, it is worth signing up and testing a few requests before you commit to a higher-cost direct setup.
¿Listo para empezar?
Conecta Claude y GPT en minutos a los precios más bajos, sin recortes. Regístrate para obtener tu clave API.
Registro gratis