59API

← 返回教程列表

OpenAI SDK with a Custom Base URL in Python

API 使用 · EN · 2026-08-22

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

2. Custom base URL through an API relay like 59API

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.

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.

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.

价格 · 接入文档

准备好开始了吗?

几分钟接入 Claude 与 GPT,全网超低价,原生不降智。立即注册即可领取 API 密钥。

免费注册