OpenAI API Cost Optimization Guide
OpenAI API cost optimization starts with one measurable unit: cost per successful product task. Record input tokens, output tokens, model, route, retries, and whether the result passed the application’s acceptance check. That exposes the difference between a cheap API call and a cost-effective workflow.
This guide uses current Frugal Relay route prices as concrete planning inputs. Prices and route eligibility can change, so verify the live pricing catalog before approving a production budget.
Calculate the current baseline
Section titled “Calculate the current baseline”For token-priced models:
request cost = (input tokens / 1,000,000 x input price) + (output tokens / 1,000,000 x output price)Do this calculation separately for each feature. A support classifier, code-review job, and long-form assistant have different token shapes and should not share one blended average.
The AI API cost calculator includes request volume and retries. Use it for a typical request and a high-percentile request to create a range instead of one fragile forecast.
Current low-cost GPT-5.6 routes
Section titled “Current low-cost GPT-5.6 routes”The public catalog listed these Plus Account Route rates on September 1, 2026. Prices are input / output per 1 million tokens.
| Model and route | Input / output price | Cost for 1M input + 1M output |
|---|---|---|
gpt-5.6-luna - Plus Account Route | $0.009 / $0.054 | $0.063 |
gpt-5.6-terra - Plus Account Route | $0.09 / $0.54 | $0.63 |
gpt-5.6-sol - Plus Account Route | $0.225 / $1.35 | $1.575 |
These numbers are route-specific facts, not a quality ranking. Evaluate candidate models on the same prompts and validation rules before routing production traffic.
1. Remove input that does not affect the result
Section titled “1. Remove input that does not affect the result”Input grows quietly when every call includes the full conversation, repeated policy text, unused tool schemas, and documents that were already summarized. Inspect the request actually sent over the network, not only the newest user message.
Useful reductions include:
- Keep only the conversation turns needed for the current task.
- Replace old history with a reviewed summary when the application can tolerate it.
- Send only tools available in the current workflow step.
- Retrieve relevant document sections instead of attaching the full source by default.
- Deduplicate repeated instructions before they reach the client.
Measure task success before and after each change. A shorter prompt is not an optimization if it creates more follow-up calls.
2. Give output an explicit contract
Section titled “2. Give output an explicit contract”Output-heavy requests can dominate cost because the listed GPT-5.6 output rate is six times its input rate. Define the smallest response the application consumes: a category, a bounded list, or a validated object.
For a chat-completions request, keep the output ceiling close to the useful result:
import osfrom openai import OpenAI
client = OpenAI( api_key=os.environ["FRUGAL_RELAY_API_KEY"], base_url="https://frugalrelay.me/v1",)
response = client.chat.completions.create( model="gpt-5.6-luna", messages=[ { "role": "user", "content": "Return one label: billing, access, bug, or other.", } ], max_tokens=20,)Confirm the exact options supported by the selected model and route. Validate the response in the application rather than assuming an instruction always produces the requested shape.
3. Route by task, not by one global default
Section titled “3. Route by task, not by one global default”Create a small evaluation set for each task type and run the same cases through the candidate models. Record valid-result rate, latency, input tokens, output tokens, and retries. Select the lowest cost per accepted result that meets the feature’s requirements.
Keep the policy explicit. A configuration table with task type -> model -> fallback is easier to audit than scattered model names in application code. Re-run the evaluation when a prompt, model, route, or response contract changes.
4. Bound retries before production
Section titled “4. Bound retries before production”Retries multiply every cost in the request. Log the reason for each retry and separate transient transport failures from invalid responses or permanent request errors. Use a fixed maximum attempt count, timeouts, and backoff for failures that are actually safe to repeat.
Do not hide retry volume inside an unexplained budget multiplier. If the measured retry rate is 3%, include 1.03 expected calls per planned request in the forecast and track whether that rate changes after deployment.
5. Cache only stable work
Section titled “5. Cache only stable work”Application-side caching can remove repeated deterministic tasks such as classification of an immutable document version. Build the cache key from every input that changes the result, including the model, prompt version, and relevant request options. Set an expiry or invalidate the entry when those inputs change.
Do not cache personalized, time-sensitive, or safety-critical responses without reviewing the data and correctness implications. A high hit rate is not useful if the cache returns the wrong result.
Add spend guardrails
Section titled “Add spend guardrails”Use separate API keys for development, evaluation, and production. Configure an appropriate daily or monthly limit for each key, and alert before the limit is exhausted. Keep an application-side circuit breaker for runaway loops; the key limit is the final containment layer, not the first signal.
During a migration, send a controlled traffic slice through the new route and compare observed cost per accepted result with the estimate. The OpenAI-compatible migration guide provides a canary and rollback checklist.
OpenAI API cost optimization checklist
Section titled “OpenAI API cost optimization checklist”- Record input tokens, output tokens, model, route, status, and retry count per feature.
- Calculate both typical and high-percentile request costs.
- Remove unused history, documents, and tool schemas.
- Set a response contract and an appropriate output ceiling.
- Evaluate lower-priced models on the same acceptance tests.
- Bound retries and distinguish transient from permanent failures.
- Add per-key spending limits and an application-side stop condition.
- Verify the named route and live price before a budget decision.
What is the fastest way to find wasted OpenAI API spend?
Section titled “What is the fastest way to find wasted OpenAI API spend?”Group usage by product feature and sort by total output cost, retry cost, and invalid-result cost. That reveals whether the largest opportunity is prompt size, response length, model selection, or repeated work.
Is the lowest token price always the best model choice?
Section titled “Is the lowest token price always the best model choice?”No. Compare cost per result that passes the feature’s quality and validation checks. Additional retries or repair calls can make a lower token price more expensive at the workflow level.
Can an existing OpenAI client use Frugal Relay?
Section titled “Can an existing OpenAI client use Frugal Relay?”Frugal Relay documents an OpenAI-compatible chat-completions endpoint at https://frugalrelay.me/v1. Change the configured base URL and credentials, then test every request feature and error path the application depends on before moving production traffic.