« All posts

Cutting Claude API Costs 84% by Separating Vision and Reasoning

An OCR pipeline routed page images to Haiku and reasoning to Sonnet instead of Opus, cutting Claude API costs by 84%. Includes the model-routing math and a real JSON parsing bug.

While building an ingestion pipeline that turns PDFs into structured concept articles for a personal knowledge base, the author discovered that routing everything through claude-opus-4-8 cost $1-1.30 for a single 26-page document. The core insight was that OCR (reading text off a page) and concept extraction (identifying atomic concepts, prerequisites, difficulty levels) are fundamentally different tasks—one is rote transcription, the other needs stronger reasoning. Sending page images to the cheaper claude-haiku-4-5 model for OCR and reserving the single extraction call for claude-sonnet-4-6 cut the cost to about $0.18, an 84% reduction.

Along the way, a subtle bug surfaced: a naive string-split used to strip markdown code fences from Sonnet's JSON output broke whenever the extracted article text itself contained code samples with nested fences, causing a JSONDecodeError. The fix strips the fences line by line and extracts the JSON between the first '{' and last '}'; the pipeline also switched to streaming to avoid silently hitting the max_tokens ceiling on large extractions.

The broader lesson is to stop defaulting to the strongest model for every step. Mechanical tasks like OCR belong on cheaper models, while extraction, synthesis, or high-stakes generation justify stronger ones. With a 5x gap in input token pricing between Haiku and Opus, this routing decision compounds significantly at real volume.