← Back to research
Guides11 min readPublished 2026-07-20

GPT API in Python: Complete Example with LumeAPI

Call GPT from Python with LumeAPI. Add streaming, error handling, token-cost calculation, and production logging through one OpenAI-compatible endpoint.

By LumeAPI Engineering Team

GPT API hub →

Last verified: July 20, 2026

You can call a current GPT model from Python through LumeAPI with the official OpenAI client. Set base_url to https://api.lumeapi.site/v1, load a LumeAPI key, and use an exact GPT model ID from the live catalog. The same client can later call Claude or Gemini by changing only the model ID, while the account, key, wallet, and request logs remain in one place.

This guide starts with a minimal GPT request, then adds streaming, bounded error handling, and a reproducible cost calculation. It is intentionally GPT-specific; for a provider-neutral setup, use the OpenAI-compatible Python guide.

Choose a GPT model before writing code

LumeAPI listed these GPT text models on July 20, 2026. Prices are USD per 1 million input/output tokens. Confirm the live GPT API page before deployment because availability and rates can change.

Model IDLumeAPI input / outputPage reference input / outputDifference shown on pagePractical starting point
gpt-5.4-mini$0.225 / $1.35$0.75 / $4.5070% lowerHigh-volume chat, classification, short generation
gpt-5.4$0.75 / $4.50$2.50 / $15.0070% lowerGeneral production work
gpt-5.6-terra$0.75 / $4.50$2.50 / $15.0070% lowerCost/performance GPT-5.6 workloads
gpt-5.6-sol$1.50 / $9.00$5.00 / $30.0070% lowerDemanding coding, reasoning, and agent tasks

Start with gpt-5.4-mini when request cost matters and move to a larger model only when your own evaluation shows that it improves completed-task quality. A model with a higher token price can still be economical if it needs fewer retries, but that must be measured on your workload.

Install the Python client

Use Python 3.9 or newer and install the current OpenAI package:

bash
python -m pip install --upgrade openai

Keep the key outside source code. On macOS or Linux:

bash
export LUMEAPI_KEY='your-lumeapi-key'

In PowerShell:

powershell
$env:LUMEAPI_KEY = 'your-lumeapi-key'

Use a secret manager in production. Never ship the key in browser JavaScript, a mobile bundle, a public repository, or a screenshot.

Send a minimal GPT request

Create gpt_example.py:

python
import os
from openai import OpenAI

api_key = os.getenv('LUMEAPI_KEY')
if not api_key:
    raise RuntimeError('Set LUMEAPI_KEY before running this script.')

client = OpenAI(
    api_key=api_key,
    base_url='https://api.lumeapi.site/v1',
)

response = client.chat.completions.create(
    model='gpt-5.4-mini',
    messages=[
        {
            'role': 'system',
            'content': 'Answer clearly and use no more than three sentences.',
        },
        {
            'role': 'user',
            'content': 'What should an API health check verify?',
        },
    ],
)

print(response.choices[0].message.content)
print({
    'model': response.model,
    'input_tokens': response.usage.prompt_tokens if response.usage else None,
    'output_tokens': response.usage.completion_tokens if response.usage else None,
})

Run it:

bash
python gpt_example.py

The client sends the request to https://api.lumeapi.site/v1/chat/completions. The generated text is in response.choices[0].message.content. Usage fields may be used for application-side cost estimates when the response contains them. LumeAPI also provides per-call logs where model, tokens, cost, and latency can be reviewed after a request.

Calculate the request cost

For a text model billed separately for input and output, use:

text
cost = input_tokens / 1,000,000 × input_rate
     + output_tokens / 1,000,000 × output_rate

Suppose a gpt-5.4-mini call uses 1,500 input tokens and 500 output tokens. At the LumeAPI rates verified above:

text
1,500 / 1,000,000 × $0.225 = $0.0003375
  500 / 1,000,000 × $1.35  = $0.0006750
total                              $0.0010125

Using the reference rates displayed on the same page, the calculation is $0.003375. The difference is 70% for this model and rate pair. This example does not mean every model on the platform has a 70% discount; use the current model page for the exact comparison and date.

A small helper keeps the arithmetic explicit:

python
def estimate_cost(
    input_tokens: int,
    output_tokens: int,
    input_rate: float = 0.225,
    output_rate: float = 1.35,
) -> float:
    return (
        input_tokens / 1_000_000 * input_rate
        + output_tokens / 1_000_000 * output_rate
    )

print(f'${estimate_cost(1500, 500):.7f}')

Treat local estimates as forecasts. Use the wallet and request logs as the billing record.

Stream GPT output

Streaming improves perceived response time in a chat or coding interface:

python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ['LUMEAPI_KEY'],
    base_url='https://api.lumeapi.site/v1',
)

stream = client.chat.completions.create(
    model=os.getenv('LUMEAPI_MODEL', 'gpt-5.4-mini'),
    messages=[
        {'role': 'user', 'content': 'Give me five API monitoring checks.'}
    ],
    stream=True,
)

for chunk in stream:
    if not chunk.choices:
        continue
    text = chunk.choices[0].delta.content
    if text:
        print(text, end='', flush=True)

print()

A stream event may not contain text, so check both choices and delta.content. In a web application, also stop or cancel upstream work when the client disconnects; otherwise an abandoned request can continue consuming budget.

Add production error handling

Configure a finite timeout and keep retries bounded:

python
import os
import openai
from openai import OpenAI

client = OpenAI(
    api_key=os.environ['LUMEAPI_KEY'],
    base_url='https://api.lumeapi.site/v1',
    timeout=30.0,
    max_retries=2,
)

def ask_gpt(question: str) -> str:
    try:
        response = client.chat.completions.create(
            model=os.getenv('LUMEAPI_MODEL', 'gpt-5.4-mini'),
            messages=[{'role': 'user', 'content': question}],
        )
        return response.choices[0].message.content or ''
    except openai.AuthenticationError as exc:
        raise RuntimeError('Check the LumeAPI key and base URL.') from exc
    except openai.RateLimitError as exc:
        raise RuntimeError('Rate limited; retry later with bounded backoff.') from exc
    except openai.APITimeoutError as exc:
        raise RuntimeError('The GPT request exceeded the timeout.') from exc
    except openai.APIStatusError as exc:
        raise RuntimeError(
            f'API returned HTTP {exc.status_code}; request_id={exc.request_id}'
        ) from exc

Do not retry 401 authentication failures or an invalid model name unchanged. For 429 and selected 5xx failures, use exponential backoff with jitter and cap the total time spent retrying. If the request can trigger an external side effect, add idempotency at the application layer before retrying.

Common GPT Python problems

The request goes to api.openai.com

Confirm that the exact client instance sending the request has base_url='https://api.lumeapi.site/v1'. Large applications often construct more than one SDK client.

The API returns model not found

Copy the model ID from the live catalog or GPT-5.4 mini documentation. A display name is not always an API ID.

The API returns 401

Check that the running Python process received LUMEAPI_KEY; a variable in your terminal may not exist in an IDE, container, worker, or deployment environment. Follow the LLM API 401 checklist without printing the key.

Cost is higher than expected

Inspect both input and output tokens, repeated context, retries, and model selection. Output tokens may cost more than input tokens. Query the LumeAPI call log for the model, tokens, cost, and latency of the exact request instead of estimating from message length.

Why use LumeAPI for this GPT workflow?

This integration keeps the familiar OpenAI-shaped Chat Completions request while exposing current GPT model IDs through one LumeAPI key and USD wallet. The same client can call available Claude and Gemini text models later by changing the model ID. The same account also covers listed image and video models, although those modalities can use different parameters or asynchronous job flows.

The product advantage should be verified in the workflow: copy model IDs from the catalog, compare dated prices, and inspect request logs. OpenAI compatibility does not imply that every provider-native endpoint or beta feature is identical. Test the exact streaming, tools, structured output, and error behavior your application needs.

Production checklist

  • Keep the API key server-side.
  • Use the exact current model ID.
  • Store the base URL and model in centralized configuration.
  • Set timeouts and bounded retries.
  • Log the selected model, status, request ID, latency, and usage without exposing prompts or secrets.
  • Compare cost per completed task, not only cost per token.
  • Test a larger GPT model only when your evaluation shows a benefit.
  • Review the GPT API hub, AI API pricing, and OpenAI-compatible API before production rollout.

Sources and verification

The code blocks were parsed with Python on July 20, 2026. A customer inference key was not used for a billable end-to-end request, so this article does not claim observed output quality or latency.