Cost optimisation — caching, batching, routing
لاگت کم کرنا — کیشنگ، بیچنگ، روٹنگ
38 min read
Three ways to see it
Lever one: prompt caching. Anthropic and OpenAI both let you mark large stable parts of a prompt (a long system instruction, a policy manual, a glossary) as cached. The first request pays the full price to load it. Every subsequent request that reuses the cache pays only 10% for that portion. For a customer-service bot whose system prompt is 8000 tokens and is reused on every call, caching brings the input bill down by roughly 70-80%. The cache lives for five minutes by default and refreshes on each use. Setup is one extra parameter in the API call.
Lever two: batch API. If your work is not time-sensitive — overnight processing of a thousand FBR returns, monthly summarisation of all PIA flight reports, end-of-quarter audit document review — submit it through the batch endpoint. Anthropic and OpenAI both offer batch processing at 50% off, with results returned within 24 hours. The discount is exactly the same content. The only thing you give up is the right to see the answer in 5 seconds. For any pipeline where humans review results the next morning, this is free money.
Lever three: model routing. Not every question needs your most expensive model. Route easy, structured queries (classification, extraction, simple Q&A) to Claude Haiku or GPT-4o-mini at one-tenth the cost. Route complex reasoning to Sonnet or Opus only when the cheaper model fails or below a confidence threshold. A simple if-else in your code or a tiny router model in front of the request chain can shift 60-70% of traffic to the cheap lane. JazzCash and Easypaisa both run pyramidal routing internally for exactly this reason.
Quick check
Quick check: what makes modern AI different from a rule-based program?
The why-tree
Why-tree level one: why does caching work? Because the model can reuse the internal representation it already computed for the cached part of the prompt. The expensive step is processing those tokens; the cheap step is using the already-processed result. Vendors expose this saving directly because it costs them less.
Try this with Claude
AI-edge prompt: 'Here is my current prompt: [paste]. It runs 1000 times a day. Rewrite it so the stable parts go in a cache block and the variable parts stay outside. Mark the cache_control breakpoints. Estimate input-token savings as a percentage.' Compare what the model returns with the Anthropic prompt-caching documentation; the structure should match.
Sources
Sources and further reading. Anthropic, Prompt caching (docs.anthropic.com/en/docs/build-with-claude/prompt-caching). Anthropic, Message Batches API (docs.anthropic.com/en/docs/build-with-claude/batch-processing). OpenAI, Prompt caching and Batch API (platform.openai.com/docs/guides). Anthropic, 'Routing requests between Claude models' cookbook example. LangChain RouterChain documentation (python.langchain.com). For latency vs cost tradeoffs, see Anthropic engineering blog posts on production patterns.