59API

← Retour aux guides

Call Claude and GPT in Python: A 10-Minute Setup

Guides · EN · 2026-10-22

Call Claude and GPT with one Python client

If you want to call Claude and GPT from Python, you can use the OpenAI Python SDK through an API relay that supports both model families. That gives you one client configuration and a shared request format for tasks such as summarization, classification, and code assistance.

59API provides low-cost, pay-as-you-go access to Claude models, including Opus, Sonnet, Haiku, and Fable, alongside GPT models. It offers native, official-quality models without downgrades and compatibility with the OpenAI SDK, Claude Code, and Codex. Here, we will focus on a small Python script you can run and adapt.

1. Install the SDK and prepare your credentials

Create a virtual environment with python -m venv .venv, activate it using the command for your operating system, then install the client:

python -m pip install --upgrade openai

Get an API key from your 59API account. You will also need the exact identifiers of one available Claude model and one GPT model. Copy these from the model list or documentation; display names and API identifiers can differ.

On macOS or Linux, set the following environment variables, replacing each example value:

export API59_KEY='your-59api-key'

export CLAUDE_MODEL='your-exact-claude-model-id'

export GPT_MODEL='your-exact-gpt-model-id'

Keep the key out of source control. On Windows PowerShell, the equivalent syntax is $env:API59_KEY="your-59api-key"; set the two model variables the same way.

2. Configure the OpenAI client

Create a file named quickstart.py. Put each Python paragraph below on its own line in that file. The semicolons separate valid Python statements.

import os; from openai import OpenAI

client = OpenAI(api_key=os.environ["API59_KEY"], base_url="https://api.59api.com/v1", timeout=60.0, max_retries=2)

59API's API host is https://api.59api.com. This example uses its OpenAI-compatible versioned base path, /v1; the SDK adds the endpoint path when sending a request. Check the current integration documentation if your account specifies a different base path.

3. Send the same prompt to Claude and GPT

Start with a small text request. Using the same prompt makes the outputs easier to compare:

messages = [{"role": "user", "content": "Explain Python generators in three concise bullet points, with one practical use case."}]

claude = client.chat.completions.create(model=os.environ["CLAUDE_MODEL"], messages=messages, max_tokens=300)

print("Claude:", claude.choices[0].message.content)

gpt = client.chat.completions.create(model=os.environ["GPT_MODEL"], messages=messages, max_tokens=300)

print("GPT:", gpt.choices[0].message.content)

Run the script with python quickstart.py. These are two separate, billable requests. Both use Chat Completions through the relay's OpenAI-compatible interface. Parameter support can vary by model; if your chosen model rejects an output-limit parameter, use the parameter documented for that model.

4. Check usage before scaling up

Add these lines to inspect the usage information returned with each response:

print("Claude usage:", claude.usage)

print("GPT usage:", gpt.usage)

Token counts help explain consumption, but your bill depends on the selected model's rates and any applicable caching or reasoning charges. Compare representative tasks using answer quality, response time, and actual cost.

59API's inexpensive, pay-as-you-go access makes these experiments accessible without a fixed subscription commitment. Its referral rebate can provide additional savings; check the current terms before including it in your budget.

5. Resolve common setup errors

Ready to try your own workload? Sign up for 59API, choose two available models, and run this script with a prompt from your application.

Tarifs · Docs

Prêt à commencer ?

Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.

Inscription gratuite