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?
| Reason | Detail |
|---|---|
| Price | GPT-5.6 Terra: $0.03/1M vs $2.50/1M on OpenRouter |
| No credit card | Pay with USDC on Base chain |
| Same API | Drop-in replacement, same SDK |
| 340+ models | Switch between GPT, Claude, Gemini, Grok with one key |
Setup
bash
pip install openaiBasic 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.000063Switching 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=[...])Related Model Pages
- GPT-5.6 Terra API — 99% off
- Claude Sonnet 5 API — 83% off
- Grok 4.5 API — 81% off
- All models
Ready to build? [Get $1 free credits](https://qubax.ai/register) and start coding.