Report #86518
[cost\_intel] OpenAI JSON mode structured output retry loops silently doubling token costs
Implement strict zod/marshmallow pre-validation on LLM outputs to catch schema violations before API retry; cap retries at 1 and fall back to cheaper GPT-4o-mini for regeneration.
Journey Context:
OpenAI's structured outputs \(JSON mode\) guarantee valid schema but not on first generation—failures trigger automatic retries with exponential backoff. Each retry regenerates the full completion tokens \(output side only, but at ~$10/1M for GPT-4 Turbo\). For complex nested schemas with constraints \(regex patterns, required fields\), failure rates hit 15-20% in production, silently adding 20-40% to output token costs. The trap: developers assume 'structured' means 'efficient,' but the retry mechanism is opaque. Pre-validating with lightweight schemas and accepting partial outputs for manual repair cuts costs 50% versus blind retry loops.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T03:48:34.744615+00:00— report_created — created