Stable LLM Output Formats Without Blowing the Budget
Getting consistent output formats from LLMs
If you use LLMs in production, inconsistent output is expensive. A single malformed JSON response can break a workflow, trigger retries, and waste tokens. The goal is not just “better prompts”; it is a repeatable system that returns the same shape every time while keeping API spend low.
The good news: you can usually get reliable structured output without paying for a more expensive model tier. In many cases, the cheapest approach is a strong prompt, a strict schema, and a lightweight retry strategy. If you are routing through a low-cost relay like 59API, you can keep using official-quality Claude and GPT models through https://api.59api.com while minimizing per-call cost.
Start with the output contract, not the prompt
Before you ask the model anything, define the exact format you want. This should include field names, types, allowed values, and what to do when information is missing. The more precise the contract, the fewer retries you will need.
- Good: Return valid JSON with keys: title (string), priority (integer 1-5), tags (array of strings).
- Better: Return only JSON. No commentary, no code fences, no markdown.
For simple workflows, this alone can cut failure rates dramatically. If your app makes 10,000 calls per month and 5% need retries, trimming that to 1% can save 400 extra requests. At scale, that is real money.
Use schema-first prompting
The model should see the schema and the instructions together. Put the output rules near the top of the prompt, and keep them unambiguous. Example structure:
- Task description
- Exact output schema
- Rules: “output JSON only,” “use null if unknown,” “do not invent fields”
- One short example if needed
When you need consistency, fewer words are often better. Long prompts increase token costs and can introduce confusion. A compact, schema-first prompt is cheaper and usually more reliable.
Make the model’s job easier with constrained fields
Consistency improves when the model chooses from bounded options instead of generating free-form text. Replace open-ended fields with enums whenever possible.
- Use status: "new", "in_progress", "done" instead of a free-text status note.
- Use sentiment: "positive", "neutral", "negative" instead of a paragraph.
- Use confidence: a number from 0 to 1 instead of prose.
This reduces ambiguity and lowers the chance of having to resend a request. It also makes downstream validation faster and simpler.
Add validation and retry only where it matters
Do not blindly retry every failure. First validate locally. If the response is close to correct, you may only need a small repair step. A typical pattern:
- Parse the response as JSON.
- Check required keys and types.
- If it fails, send the broken output back with a narrow repair prompt.
This “repair pass” is often cheaper than starting over because it uses fewer tokens. For example, if a first pass costs $0.002 and a full retry costs another $0.002, a focused repair prompt might cost only $0.0007. Over 50,000 monthly calls, that difference adds up quickly.
Use the right model for the task
Not every structured-output task needs the largest model. Extraction, classification, and template filling are often handled well by smaller, cheaper models. Save higher-end models for cases where reasoning really matters.
This is where cost optimization becomes practical. With 59API, you can access Claude models such as Opus, Sonnet, Haiku, and Fable, plus GPT models, through one relay that is compatible with Claude Code, Codex, and any OpenAI SDK. That means you can test a cheaper model first, keep the same integration, and upgrade only when needed.
Measure format accuracy, not just token spend
The cheapest request is the one you do not have to repeat. Track three numbers:
- Format pass rate: percentage of responses that parse on the first try.
- Repair rate: percentage that need a fix-up request.
- Cost per valid output: total spend divided by successful structured responses.
If Model A costs 20% less per token but fails twice as often, it may be more expensive overall. Optimizing for valid output, not raw token price, is the real win.
Why a low-cost relay helps
Using a relay like 59API can lower the cost of experimentation. When you are tuning prompts, schemas, and retry logic, you will run many test calls. A cheaper pay-as-you-go setup makes that iteration affordable. Since 59API uses native official-quality models rather than downgraded substitutes, you can optimize output formats without sacrificing model behavior. The referral rebate can also soften ongoing spend if you are sharing the tool with teammates or the broader dev community.
Practical setup checklist
- Define a strict JSON schema before prompting.
- Ask for JSON only; forbid extra text.
- Use enums and nulls instead of free-form prose where possible.
- Validate responses locally before accepting them.
- Retry with a narrow repair prompt, not a full regeneration.
- Test cheaper models first, then escalate only when necessary.
If you want to keep structured outputs reliable without overspending, it is worth trying a relay built for cost efficiency. Sign up for 59API and connect it to your existing OpenAI-compatible tooling at https://api.59api.com.
Pronto para começar?
Conecte Claude e GPT em minutos pelos menores preços, sem cortes. Cadastre-se e obtenha sua chave API.
Cadastro grátis