Let’s Get Certified
Module 17 / 21 · Operational efficiency and optimization for generative AI applications

Spend less per accepted answer

Compare inference routes using total cost per accepted result, including retries.

Spend less per accepted answer

Work through the decision

A help desk spends less per API call after changing models, yet spends more per resolved ticket. Users retry the new answers. Which metric should guide optimization?

Check your reasoning

Divide total inference and retrieval cost, including retries, by resolved cases meeting the quality threshold. Profile repeated instructions, unused context and wasted output tokens. Recheck quality after shortening prompts or changing model routes.

Watch the source lesson at 5:04:28

Compare the choices

Trim redundant context
Remove duplication and select only relevant passages under a measured token budget, then test answer quality.
Route by complexity
Send simpler eligible work to a cheaper model and escalate requests that need greater capability. Measure cost per acceptable result.
Batch independent work
Group delay-tolerant records and measure utilization, throughput and completion against the deadline.
Cache reusable results safely
Key by request context, model and source version, entitlement and freshness needs. Invalidate when the answer's assumptions change.

Check cache scope

Two employees ask the same benefits question but have different plan access. A cache keyed only by prompt text can serve the wrong plan. Include entitlement and policy version in the cache decision, or use a public-only cache when individualized reuse cannot be made safe.

Apply it on AWS

Track input and output tokens separately by model because their rates can differ. Bedrock prompt caching can reduce repeated prefix processing for supported models. That differs from an application response cache that reuses a full answer and must check permissions and source freshness.

Practice the guide skills

  1. Count input and output tokens including retries. Trim irrelevant passages and check quality on the same cases. Watch 5:04:28
  2. Benchmark small and large models on work of different complexity. Compare total cost per accepted answer across routes. Watch 3:26:32
  3. Batch independent requests and measure utilization, completion time and cost at representative volume. Watch 5:22:44
  4. Key a response cache by relevant identity, source and model versions. Test invalidation after permission or source changes. Watch 5:12:00

Official objectives for this lesson

Local lab · Compare cost per accepted case

Save this replay as route_cost.py and run python3 route_cost.py. Rates are fictional dollars per case, including inference. Predict which route wins after retries. A case resolved on a second attempt counts once.

from decimal import Decimal as D

runs = {
    # initial calls, retry calls, cost per call, accepted cases
    "large": (100, 4, D("0.04"), 98),
    "small": (100, 70, D("0.02"), 65),
}

for name, (calls, retries, rate, accepted) in runs.items():
    total = (calls + retries) * rate
    print(f"{name}: spend ${total:.2f}, per accepted ${total / accepted:.4f}")
Check the result
large: spend $4.16, per accepted $0.0424
small: spend $3.40, per accepted $0.0523

The small model wins on total spend but loses on accepted cost and completion. Change its accepted count to 90 and compare again. The team must also set a minimum quality threshold before using cost to choose a route.

Related lesson · Measure value and risk in a generative AI pilot

Knowledge check

Spend less per accepted answer quiz

Answer 3 questions. Check each answer for an explanation.