Call Claude and GPT in Python: A 10-Minute Setup
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.
- Start small: test a few short requests before processing a directory or dataset.
- Limit context: send the relevant excerpt instead of repeatedly attaching an entire document.
- Match the model to the task: try an economical option for extraction and reserve more capable models for difficult reasoning.
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
- Authentication error: confirm that API59_KEY contains a valid 59API key and that the account has available credit.
- Model unavailable: copy the exact model identifier and confirm account access.
- 404 response: verify the base path and endpoint against the current documentation.
- Rate limit or timeout: reduce concurrency and use bounded retries. The client above already enables two retries for supported transient failures.
Ready to try your own workload? Sign up for 59API, choose two available models, and run this script with a prompt from your application.
¿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