Analyze with AI
Get AI-powered insights from this Mad Devs article:
An AI feature is working, adoption is growing, and the monthly bill is rising faster than forecast. The total tells you that something changed, but not whether the right response is a shorter prompt, a different model, a narrower integration, or a redesigned processing step. LLM cost optimization means reducing the cost of completing a useful product scenario while meeting defined requirements for quality, latency, and reliability. That makes the workflow, rather than the token price, the practical unit of analysis. The goal is to identify which work is necessary, which work can be changed, and whether the net benefit justifies implementation and ongoing support.
Key takeaways
- Attribute spend to a product scenario and its processing stages, not only to a provider or model.
- Challenge each operation separately: generation, matching, retrieval, validation, and retries may need different methods.
- Compare candidates on successful outcomes, including quality, latency, fallbacks, and failed runs.
- Prioritize net benefit after implementation and support costs, then begin with a bounded production workflow.
Find where the workflow is spending money
Start with one meaningful scenario, such as extracting fields from an invoice, classifying a support request, matching a record, or answering from an internal knowledge base. Trace it from the input to the result that the product accepts. An aggregate model invoice cannot show which stage expanded or which failure caused another attempt.
For each run, capture the model and version, input and output volume, provider-reported charge, latency, and outcome. Add the operations around the model: retrieval and search queries, embedding calls, reranking, tool calls, validation, and any self-hosted compute. Record which context came from the user, the application, retrieved documents, conversation history, and tool responses. A large context may be justified, but it should be possible to identify its source.
Then reconstruct repetition. Did parsing trigger the same call again? Did a weak answer invoke a stronger model? Did the system retrieve the same documents on every turn? Separate successful first attempts, fallback successes, and failures. This reveals whether spend follows productive usage or recovery work.
Use three related measurements:
- Call cost covers one inference request.
- Run cost covers every step and retry initiated for one input.
- Successful-task cost connects spend to an accepted result.
A practical calculation is:
Cost per successful task = total cost for a comparable group of runs / number of successfully completed tasks
Define both sides. A comparable group might be one document type, customer tier, language, or request class over a stable period. “Successful” might mean accepted structured output, a correct match, or an answer that passes a task-specific review. The numerator can include model and embedding charges, retrieval services, external APIs, and allocated inference infrastructure. If it excludes manual review, engineering support, or shared platform costs, state that boundary rather than treating the result as total cost.
This is attribution, not an observability-platform project. Existing application traces, provider usage exports, and outcome logs may be sufficient for the first baseline. The important requirement is a joinable identifier that connects processing steps and charges to the same product scenario.
Match the change to the source of waste
Once a trace shows where cost accumulates, choose an intervention that changes that cause. A cheaper model does not fix an oversized tool response, and caching does not help diverse prefixes. Treat the workflow as operations with different requirements.
Use specialized processing for a narrow task
First ask whether an operation needs generation. Stable identifiers and business rules may be handled deterministically. Exact matching is explainable but fails when equivalent terms differ in wording. Embeddings can capture semantic proximity when the output is a choice among known candidates. A generative model remains appropriate for synthesis, interpretation across evidence, or open-ended responses.
Mad Devs faced this distinction while connecting generated Related Topics to existing glossary pages. As described in Semantic Interlinking Without OpenAI, the team considered exact matching, adding the whole glossary list to the generation prompt, embeddings, a graph approach, and external APIs. It selected FastEmbed with a local embedding model for term matching. The threshold was chosen experimentally, and a failed match leaves readable, unlinked text as a fallback. This is not evidence of a measured migration from paid LLM calls, nor is local execution free: it uses compute, memory, storage, and maintenance. The engineering lesson is narrower. Content generation and term matching are different operations, so the workflow can retain generation where needed while assigning matching to a specialized component.
Test a representative labeled set with exact rules, several embedding thresholds, and the current step. Review false links separately from missed links because their product impact may differ.
Reduce unnecessary context at the integration boundary
Context can expand before a prompt template ever sees it. A ticketing, CRM, or document API may return markup, histories, user objects, permissions, and metadata when the next step only needs status, owner, blocker, and a short description. Filtering these fields at the integration boundary avoids repeatedly serializing and processing irrelevant data.
The Mad Devs guide to Python MCP servers illustrates this with a tool that returns a compact structure instead of a full Jira API response. The tool contract selects the fields required for the agent to continue. This reduces context volume and also limits untrusted markup and sensitive data entering the model. The publication does not report a percentage cost reduction, so the example supports the design choice, not a savings forecast.
Apply the same logic to document pipelines and retrieval: remove markup, deduplicate metadata, and fetch details only when needed. Keep provenance for validation. Over-compression can omit a qualifier, access rule, or exception that determines correctness.
Test the revised contract on cases that need rarely used fields. Accept it only if task success and latency remain within requirements.
Reuse or defer work when the task allows it
Reuse has several forms with different correctness boundaries.
Provider prompt caching can reduce the charge or compute for a repeated prefix. It depends on provider rules, prefix stability, timing, and traffic concentration. The benefit applies to eligible cached input, not automatically to the whole workflow.
Result caching returns a previous answer when the same request should receive the same result within a freshness period. Its key must include factors that change correctness, such as source version, tenant, locale, permissions, and personalization. Updates and revoked access require invalidation. Do not assume a semantically similar request is equivalent.
Asynchronous or batch processing changes when work runs. Document classification, enrichment, indexing, and evaluations may tolerate a queue. Its benefit depends on volume, provider terms, turnaround, and failure handling. Interactive requests usually cannot accept an overnight batch window.
Before implementing reuse, quantify requests by stable prefix, cache key, and permitted latency. Low-frequency or personalized traffic may offer little reusable work. Define stale-data detection and failed-batch handling.
Change models or serving only when the workload justifies it
Select models per task class, not from one leaderboard or price column. A lower-cost model may handle constrained extraction but fail on ambiguous documents. Retries and escalation can erase its cheaper initial call.
Routing can assign routine and ambiguous cases to different models. It needs a reliable signal and fallback. Compare cheap-model success, retry, escalation, and direct use of the stronger model. Enough traffic must take the lower-cost path without unacceptable quality or latency loss.
Self-hosting moves spend from an API bill into accelerators, orchestration, monitoring, capacity planning, and on-call work. Sustained demand or control requirements may justify it. For bursty workloads, idle capacity and engineering overhead can outweigh lower marginal inference cost. Utilization and peak capacity matter more than theoretical throughput.
Use the following matrix after diagnosis, not as a generic checklist:
| Observed cost cause | Possible change | Applicability condition | What to verify |
|---|---|---|---|
| A narrow matching or classification step uses generation | Deterministic rules or embeddings | Output space is known and edge cases can fall back safely | False matches, missed matches, fallback rate, full operating cost |
| Tool or retrieval responses dominate input | Compact integration contract or staged detail retrieval | Required fields can be identified without losing decision-critical evidence | Task success by case type, context volume, latency, data provenance |
| Stable prefixes repeat across requests | Provider prompt caching | Sufficient traffic repeats an eligible identical prefix | Actual hit rate, eligible-token charges, prefix/version changes |
| Identical outcomes are recomputed | Result cache | Freshness, tenancy, permissions, and invalidation can be encoded | Correct cache keys, stale responses, access isolation, hit rate |
| Offline jobs use synchronous inference | Queue or batch processing | Product SLA permits delayed completion | Turnaround, queue failures, retries, total run cost |
| One expensive model handles mixed-complexity work | Task-specific model selection or routing | Cases can be separated with a dependable escalation path | Quality by segment, retry and fallback cost, tail latency |
| API demand is sustained and predictable | Self-hosted serving | Utilization and control benefits cover infrastructure and staffing | Loaded cost, peak capacity, reliability, maintenance effort |
Validate savings against quality and latency
A cost change is a candidate until it passes a comparable evaluation. Keep the baseline and candidate on the same input set, task definitions, and acceptance criteria. Record model, prompt, retrieval, tool-contract, routing, and runtime configuration versions so a difference can be reproduced.
The evaluation set should represent both traffic and risk. Separate document types, languages, request complexity, long-context cases, and critical failure modes. A lower average cost can hide a regression in the category that drives escalations or customer harm. Report results by meaningful segment and inspect failures, rather than relying on one aggregate score.
For each candidate, collect:
- Cost of the complete execution path, including retries, validation, fallbacks, retrieval, embeddings, and relevant infrastructure.
- Task quality against explicit criteria, such as field-level extraction correctness, classification accuracy, match precision and recall, or evidence-grounded answer review.
- End-to-end latency, including queue time and tail latency where it affects the product.
- Failure, timeout, retry, and escalation rates.
- The resulting cost per successful task, using the same accounting boundary as the baseline.
Mad Devs’ article From Unit Tests to LLM-Evals describes semantic checks and regression tracking from work on Campo and Enji Fleet. It also states important limits: semantic similarity can miss invented details, thresholds require calibration by task and domain, and the published runner is a structural demonstration rather than a production-ready evaluation system. Use similarity as one signal, not proof of factual correctness. Extraction may need field assertions and sampled review; knowledge answers may need checks against source evidence; routing may need task-specific labels.
Cached results require another distinction. They help make repeated system and cost tests stable, but a cached output does not show how a new model or revised prompt performs. When the changed component is under evaluation, execute those scenarios again and retain raw outputs and traces. Otherwise, the experiment may measure cache behavior while leaving the candidate’s quality unknown.
Define acceptance before looking at results. A candidate can reduce cost and still be rejected because it raises critical errors, breaches latency objectives, or creates an unreliable fallback path. Conversely, a small per-call reduction can be valuable if it applies broadly and preserves the operating envelope.
Prioritize changes by net benefit
Technical savings are only one part of the decision. Estimate how much current spend the change can affect and how often its applicability conditions occur. A compact tool response may touch every run; a result cache may apply only to a small repeated subset. Use measured traffic proportions rather than assuming all requests qualify.
For each candidate, account for design, implementation, test data, evaluation, rollout, and rollback work. Add recurring costs: hosted cache capacity, embedding or routing calls, accelerator reservations, monitoring, incident response, model updates, and ownership time. Then record expected effects on quality, latency, and reliability, including the cost of additional retries or manual review.
Keep four economic outcomes separate:
- Lowering the current bill reduces existing recurring spend.
- Slowing cost growth improves unit economics as usage increases, even if total spend still rises.
- Moving spend replaces API charges with infrastructure and operational ownership.
- Reducing failed attempts improves the cost of a successful outcome without necessarily changing the first-call price.
When the inputs are credible, a simple payback estimate is:
Payback period = implementation cost / net monthly savings
Net monthly savings should subtract new recurring expenses. Treat traffic, cache-hit rate, model mix, utilization, and provider pricing as assumptions and test sensitivity to changes. This estimate is a planning tool, not a guaranteed result.
Consider two candidates without inventing percentages. Replacing a narrow operation may produce a smaller gross reduction but require limited maintenance and apply to nearly every run. Self-hosting may offer a larger theoretical inference reduction, yet require new infrastructure skills, spare capacity, and continuous support. The first can have a better net benefit and shorter payback even though its technical saving looks less dramatic.
Prioritize a small set: a low-risk change that removes measured waste, a higher-impact candidate that needs evaluation, and any reliability fix that reduces expensive failures. This creates evidence for the next decision without turning optimization into a speculative platform rewrite.
Scope a production optimization project
Start with one workflow that is expensive enough to matter and bounded enough to evaluate. Define its accepted result, latency and reliability requirements, volume profile, and accounting boundary. Capture the baseline for cost per successful task and quality by relevant case type.
Select a small number of changes tied to observed causes, such as a compact tool response and one alternative for a narrow processing step. Specify the comparison dataset, configurations, acceptance gates, and rollback conditions. The delivery scope should include instrumentation, implementation, evaluation, staged rollout, and post-release observation. Document the final decision, known limitations, cache or model invalidation rules, and the team that owns the component.
An internal team is usually sufficient when the cause is clear, the workflow is observable, the required backend and AI evaluation skills are available, and people have protected time to implement and monitor the change. External support is useful when the work crosses AI logic, backend integrations, evaluation design, and inference infrastructure, or when the team can diagnose the issue but cannot allocate delivery capacity.
The objective is not necessarily to rebuild the AI system. A technical assessment can narrow the work to a tool contract, a matching step, a routing policy, or an evaluation gate. Mad Devs’ AI and machine learning development services include assessment and improvement of existing AI systems, which can be scoped around the workflow, evidence, and release constraints already defined by the product team.
Conclusion
Choose one costly scenario and trace every operation between its input and an accepted result. That baseline will show whether the first useful change belongs in context handling, specialized processing, reuse, model selection, or serving. Define the quality and latency gates before implementation, compare the complete execution paths, and calculate net benefit after ongoing costs. The next concrete action is to instrument that scenario and review a representative group of successful, retried, and failed runs with the team that owns it.
Plan your AI cost optimizationIdentify costly steps and scope changes with quality, latency, and cost checks. Discuss your setup |








