Featured
- Get link
- X
- Other Apps
OpenAI API with Python in 2026: One Real Build, With the Cost Math
OpenAI API with Python in 2026: One Real Build, With the Cost Math
Most OpenAI Python tutorials stop at "it printed a response." The question people actually search next is what it costs per run. So this post builds one small, useful tool, a batch document summarizer, and prices it at the live per-model rates I read on October 23, 2026. One thing up front, because the receipt only means something if it is honest: I made no paid API calls for this article. Input tokens below are measured with OpenAI's tokenizer on real documents. Output tokens are a stated ceiling, not a metered figure.
The 30-second answer
Summarizing about 1,000 articles of roughly 2,400 tokens each costs about $0.48 on gpt-4o-mini and about $8.01 on gpt-4o, at standard rates. Moving the job to the Batch API halves both, to about $0.24 and $4.01. Model choice moves the bill about 17x. Batching takes 50% off. Prompt caching, the move most guides lead with, does almost nothing for this particular workload, and I explain why below.
What I checked, and when
- SDK: the
openaipackage on PyPI is at version 3.37.0 (uploaded October 22, 2026), requiring Python 3.10 or newer. - Prices: read from OpenAI's pricing page (developers.openai.com/api/docs/pricing) on October 23, 2026. Prices change, so treat the tables as a dated snapshot and re-check before budgeting.
- Tokenizer:
tiktoken0.14.0, using theo200k_baseencoding that the gpt-4o family uses. - Free credits: I could not verify any current free tier or starter credit from the pricing page, so I am not promising one. Assume you need a funded account.
The build: a batch document summarizer
The tool reads every .txt file in a folder, asks the model for a three-bullet summary, and logs the token usage the API reports for each call. The logging is the point: you cannot manage a bill you do not measure.
import sys, csv, pathlib
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from the environment
MODEL = "gpt-4o-mini"
SYSTEM = ("You are a precise technical summarizer. Summarize the document "
"in exactly 3 bullet points, max 120 words total. No preamble.")
def summarize(text):
r = client.chat.completions.create(
model=MODEL,
max_completion_tokens=200, # hard cap on output spend
messages=[{"role": "system", "content": SYSTEM},
{"role": "user", "content": text}],
)
u = r.usage
return r.choices[0].message.content, u.prompt_tokens, u.completion_tokens
rows = []
for path in sorted(pathlib.Path(sys.argv[1]).glob("*.txt")):
summary, tin, tout = summarize(path.read_text())
rows.append([path.name, tin, tout])
print(f"{path.name}: {tin} in / {tout} out")
with open("usage.csv", "w", newline="") as f:
csv.writer(f).writerows([["file", "in", "out"], *rows])
Three decisions matter for cost. First, max_completion_tokens caps the most expensive part of the bill, since output tokens cost three to six times more than input tokens on every model below. Second, the instruction asks for a fixed shape (three bullets, 120 words), which keeps output predictable. Third, every call writes its real usage to a CSV, so after your first run you can replace my estimates with your own numbers.
The receipt
I used six real documents, the finished articles from this blog's archive with HTML stripped, as the test corpus. Token counts come from tiktoken locally, so they cost nothing.
| Measurement | Value |
|---|---|
| Documents | 6 (1,715 to 1,991 words each) |
| Document tokens, total | 14,208 (average about 2,368 per document) |
| System prompt | 30 tokens per call |
| Chat message framing (my estimate) | about 7 tokens per call |
| Input per 6-document run | about 14,430 tokens |
| Output per 6-document run | 1,200 tokens at the cap (6 x 200). Real output will be lower. |
Because output is counted at the cap, every cost below is a ceiling on the output side. Your measured bill will come in at or under it. The framing overhead is an estimate, so the input figure could be off by a few dozen tokens; the cost impact is a fraction of a cent.
Prices per million tokens, standard tier, read October 23, 2026:
| Model | Input | Cached input | Output |
|---|---|---|---|
| gpt-4o | $2.50 | $1.25 | $10.00 |
| gpt-4o-mini | $0.15 | $0.075 | $0.60 |
| gpt-3.5-turbo | $0.50 | not listed | $1.50 |
| gpt-5.4-mini | $0.75 | $0.075 | $4.50 |
| gpt-5.4-nano | $0.20 | $0.02 | $1.25 |
| gpt-6-luna | $0.10 | $0.01 | $0.50 |
Cost per run, and at scale
The arithmetic is tokens divided by one million, times the price. For the 6-document run on gpt-4o: 14,430 input tokens cost $0.0361 and 1,200 output tokens cost $0.0120, so the run is about $0.048. On gpt-4o-mini the same run is about $0.0029, under a third of a cent. Scaling the same per-document averages to 1,000 documents (about 2.4 million input tokens, 200,000 output tokens at the cap):
Comparison table: models versus cost versus fit
| Model | 1,000 docs, standard | 1,000 docs, Batch | My take for summarizing |
|---|---|---|---|
| gpt-4o | $8.01 | $4.01 | Overkill for plain summaries; worth it for nuanced or legal-style text |
| gpt-4o-mini | $0.48 | $0.24 | The sensible default; cheap and capable for this task |
| gpt-3.5-turbo | $1.50 | $0.75 | Costs more than gpt-4o-mini here; there is little reason to pick it |
| gpt-5.4-mini | $2.70 | $1.35 | Pricier; only if mini-class output quality falls short for you |
| gpt-5.4-nano | $0.73 | $0.37 | Near gpt-4o-mini on price; test before switching |
| gpt-6-luna | $0.34 | $0.17 | Cheapest listed; check output quality on your own documents |
The cost columns are arithmetic on the listed prices. The "my take" column is judgment, not a benchmark. I did not run a quality evaluation across these models, so test two or three on 20 of your own documents before committing. Also note that tokenizers can differ between model families, so counts for the newer models may differ slightly from the o200k_base figures I used.
Cost-cutting moves, ranked by what they did here
1. Pick the smallest model that passes your test
This is the biggest lever: gpt-4o to gpt-4o-mini cuts the bill from $8.01 to $0.48 per thousand documents. Build a 20-document sample, read the summaries side by side, and only move up if the cheap one actually fails.
2. Use the Batch API for anything not urgent
The pricing page lists batch rates at half the standard rate for the models I compared. A summarizer that runs overnight does not need an instant answer. You upload a JSONL file of requests, wait for completion, and download results. The trade-off is latency, so it suits backfills and nightly jobs but not a user waiting on a button.
3. Cap output and constrain the format
Output tokens are the priciest per token. A fixed three-bullet format plus max_completion_tokens keeps the worst case predictable.
4. Prompt caching: real discount, wrong workload
Cached input is 50% off on gpt-4o and gpt-4o-mini, and roughly 90% off on most newer models. But caching rewards a long, repeated prefix. My system prompt is 30 tokens, and the document, which dominates the cost, is different every call. As I understand OpenAI's documentation, caching only applies once a prompt prefix is long enough (about 1,024 tokens); I did not re-verify that threshold, so check the docs. Caching pays off if you prepend a long style guide or a few-shot block to every request. It does not pay off for short instructions plus unique documents.
5. Trim the input
Strip navigation, boilerplate, and markup before sending. My test files were already stripped of HTML; raw pages would have cost noticeably more tokens for no gain.
Keeping the docs side of this in sync
If you are building tools like this one, the code is half the deliverable; the other half is documentation that stays accurate. We cover the manual approach in Stop Writing Docstrings by Hand and compare options in AI Docstring Generators Compared.
Disclosure: I sell a product in this space, so weigh this accordingly. AI Doc Generator is listed at $49 on Gumroad as of October 23, 2026 (re-check the page for the current price). It is aimed at generating project documentation, and it is a paid alternative to wiring up your own OpenAI pipeline for that job. You do not need it to follow anything in this article, and the build above works without it. For a walkthrough of the DIY route, see Auto-Generate Project Documentation.
Honest verdict
Who this is for: Python developers who want a working OpenAI pipeline and a defensible budget number before they ship. If you process hundreds to thousands of documents, the model-and-batch choices here save real money.
Who it is NOT for:
- Anyone who needs a metered receipt from this post. Output tokens here are a ceiling, and I did not benchmark summary quality across models. Run the script on your own files and read
usage.csv. - People who need zero spend. I found no verifiable free tier, and local models (Ollama and similar) are the free route. Our roundup of AI coding tools covers more options.
- Latency-sensitive apps. The cheapest path, Batch, is asynchronous.
- Teams with strict data rules. Sending documents to any hosted API is a policy decision, and cost math does not answer it.
Questions, corrections, or updated prices you have spotted: nandminc2020@gmail.com.
More from AI Stack Lab: Auto-Generate Project Documentation and 10 Best AI Coding Tools in 2026, Tested.
Popular Posts
How to Write Better Prompts: A Practical Framework
- Get link
- X
- Other Apps
Best AI Image Generators Compared (2026)
- Get link
- X
- Other Apps
Comments
Post a Comment