Back to blog
Tutorial·2 min read·373 words

OpenAI API Python Tutorial — Use GPT-5 at 99% Off with Qubax

The OpenAI Python SDK works with any OpenAI-compatible API. This tutorial shows you how to use GPT-5.6 Terra through Qubax — at **99% off** OpenRouter's price —

OpenAI API Python Tutorial — Use GPT-5 at 99% Off with Qubax — illustration

OpenAI API Python Tutorial: GPT-5 at 99% Off

The OpenAI Python SDK works with any OpenAI-compatible API. This tutorial shows you how to use GPT-5.6 Terra through Qubax — at 99% off OpenRouter's price — with real Python code you can run today.

Why Qubax Instead of OpenAI Direct?

ReasonDetail
PriceGPT-5.6 Terra: $0.03/1M vs $2.50/1M on OpenRouter
No credit cardPay with USDC on Base chain
Same APIDrop-in replacement, same SDK
340+ modelsSwitch between GPT, Claude, Gemini, Grok with one key

Setup

bash
pip install openai

Basic Chat Completion

python
from openai import OpenAI

client = OpenAI(
    base_url="https://api.qubax.ai/v1",
    api_key="qbx_live_YOUR_API_KEY",
)

response = client.chat.completions.create(
    model="gpt-5.6-terra",
    messages=[
        {"role": "system", "content": "You are a helpful coding assistant."},
        {"role": "user", "content": "Write a Python function to merge two sorted lists"},
    ],
)

print(response.choices[0].message.content)

Streaming Responses

python
stream = client.chat.completions.create(
    model="gpt-5.6-terra",
    messages=[{"role": "user", "content": "Explain async/await in Python"}],
    stream=True,
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Tool Calling (Function Calling)

python
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather for a city",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "City name"}
                },
                "required": ["city"],
            },
        },
    }
]

response = client.chat.completions.create(
    model="gpt-5.6-terra",
    messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
    tools=tools,
)

# The model will return a tool call — handle it and send results back
print(response.choices[0].message.tool_calls)

Cost Calculation

python
# Track your spending
tokens_in = response.usage.prompt_tokens     # e.g., 150
tokens_out = response.usage.completion_tokens  # e.g., 300

cost = (tokens_in * 0.0325 + tokens_out * 0.1950) / 1_000_000
print(f"This request cost: ${cost:.6f}")
# Output: This request cost: $0.000063

Switching Models

Same API key, just change the model string:

python
# GPT-5.6 Terra — cheapest
client.chat.completions.create(model="gpt-5.6-terra", messages=[...])

# Claude Sonnet 5 — best for coding
client.chat.completions.create(model="claude-sonnet-5", messages=[...])

# Grok 4.5 — from xAI
client.chat.completions.create(model="grok-4.5", messages=[...])

Ready to build? [Get $1 free credits](https://qubax.ai/register) and start coding.

Article tags

#python#openai#gpt-5#tutorial#api
Share:Post on XTelegramLinkedInYHacker NewsReddit
Qubax AI

Qubax AI

AI Models at up to 99% off · Pay with crypto

Access GPT, Claude, Gemini, GLM & 340+ models through one OpenAI-compatible API. Up to 99% off. Pay with 200+ cryptocurrencies. Get $1 free credits — no credit card needed.

Related articles