Guide
OpenAI Python API: a working example, then the same call on one key
Prices checked Nov 26, 2026 · 3 min read

The OpenAI Python API is the openai package: install it, set OPENAI_API_KEY, create a client and call a model. The minimal example is five lines. If you want the same code on another endpoint, you change two arguments: base_url and api_key. Both versions are below, and each follows its provider's documentation. We did not run them for this page, so test with a $0.01 call first.
OpenAI Python API: the official minimal example
OpenAI's quickstart gives these steps. First install the SDK and export your key (create one on the OpenAI key guide):
pip install openai
export OPENAI_API_KEY="your_api_key_here"
Then save this as example.py:
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-6.2-astra",
input="Write a one-sentence bedtime story about a unicorn.",
)
print(response.output_text)
Run python example.py. OpenAI() takes no arguments because the SDK reads the key from the environment. That is also the safe way to hold it: OpenAI says to keep keys out of client-side code and to load them from an environment variable or a key management service on the server.
The model name gpt-6.2-astra comes from the quickstart. Model names change, so if a call returns an unknown-model error, check OpenAI's models list for the current name.
What changes to use an OpenAI-compatible endpoint
The Python SDK lets you point a client at any server that speaks the same request format. You set base_url and a key for that server. TapModels lists POST /v1/chat/completions as OpenAI-compatible, with stream: true giving server-sent events.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.tapmodels.com/v1",
api_key=os.environ["TAPMODELS_API_KEY"],
)
res = client.chat.completions.create(
model="gpt-6-2-sol",
messages=[{"role": "user", "content": "Write a one-sentence bedtime story about a unicorn."}],
max_tokens=200,
)
print(res.choices[0].message.content)
print(res.usage)
Three differences to notice:
- Method. This uses
chat.completions.create. The TapModels contract does not list the Responses API, soclient.responses.createstays on OpenAI. - Model id. Our ids use hyphens, such as
gpt-6-2-sol. List what is live withGET /v1/models. - Key. A TapModels key works on our endpoint only. We do not issue OpenAI keys, and an OpenAI key does not work here.
To stream the answer, add stream=True and loop over the chunks:
stream = client.chat.completions.create(
model="gpt-6-2-sol",
messages=[{"role": "user", "content": "List three uses of a webhook."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
Handle the errors you will actually see
Our contract returns JSON errors with a code and a matching HTTP status.
| HTTP | Code | What to do |
|---|---|---|
| 401 | unauthorized | The key is missing or revoked. Check TAPMODELS_API_KEY. |
| 402 | insufficient_credits | Add credits. Balance minus held credits is below the estimate. |
| 404 | model_not_found | Fix the model id. |
| 422 | invalid_input | The message names the field that failed. |
| 429 | rate_limited | Wait for Retry-After, then retry. |
The SDK raises an exception for each. Catch openai.RateLimitError and sleep for the header value; do not loop at full speed, because retries count too. The rate limit guide has a backoff snippet.
What one call costs
Read res.usage after each call. Say a request uses 1,000 input tokens and 300 output tokens on GPT-6.2 Sol.
| Route | Input rate | Output rate | Cost of the call |
|---|---|---|---|
| OpenAI list price | $2.00 per 1M | $10.00 per 1M | $0.002 + $0.003 = $0.005 |
| TapModels catalog | $0.60 per 1M | $3.00 per 1M | $0.0006 + $0.0009 = $0.0015 |
The arithmetic: 1,000 / 1,000,000 x $2.00 = $0.002, and 300 / 1,000,000 x $10.00 = $0.003. OpenAI's rates are from its pricing page checked on 2026-11-26. Ours are the catalog prices, 60 and 300 credits per 1M, at 100 credits per $1. Run 100,000 such calls and the gap is $500.00 against $150.00.
When to keep calling OpenAI directly
Use OpenAI's own endpoint when you need the Responses API, OpenAI's invoices or its data terms. Use a compatible endpoint when you want one key across vendors or a lower rate per token, and you accept that a reseller sits between you and the model. How to evaluate a provider lists what to check.
The same client also works for Claude and Gemini models by changing the model string, which is the practical reason to keep base_url in a config value. Next step: see every chat model and its price on the LLM API page.
Frequently asked questions
How do I call the OpenAI API from Python?
Run pip install openai, set OPENAI_API_KEY in your environment, then create a client with OpenAI() and call a method such as client.responses.create with a model and an input. OpenAI's quickstart prints the reply with response.output_text. The SDK reads the key from the environment for you.
Where do I put my OpenAI API key in Python?
Put it in the OPENAI_API_KEY environment variable and let the SDK read it. Do not hard-code it in the script or commit it. OpenAI says to keep keys out of client-side code and to load them on the server from an environment variable or a key management service.
Can I use the OpenAI Python library with a different provider?
Yes, when that provider exposes an OpenAI-compatible endpoint. You pass base_url and api_key when you create the client. TapModels lists POST /v1/chat/completions as OpenAI-compatible, so the same client works with api.tapmodels.com/v1 and a TapModels key. Check the provider's docs for the routes it supports.
Does TapModels support the OpenAI Responses API?
The TapModels contract lists chat completions (POST /v1/chat/completions, with streaming) and an Anthropic-style messages route. It does not list the Responses API. Use client.chat.completions.create against TapModels, and keep client.responses.create for calls that go to OpenAI directly.
What does one chat completion cost?
It depends on tokens. A call with 1,000 input and 300 output tokens on GPT-6.2 Sol costs $0.0015 on TapModels ($0.60 and $3.00 per 1M tokens). The same call at OpenAI's listed $2.00 and $10.00 rates costs $0.005. Read usage in the response to see your real token counts.
Sources
- OpenAI API quickstart (Python) · checked Nov 26, 2026
- OpenAI API reference: authentication and key safety · checked Nov 26, 2026
- OpenAI API pricing · checked Nov 26, 2026