BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Real-world constraints6 of 10 in topic

891. Pick the cheapest cascade threshold that meets accuracy

MediumReal-world constraints Pro

Why it matters Cascades and routers (small model first, big model when unsure) are how many products cut LLM bills by half without losing quality.

A model cascade sends every query to a small model first. If the small model's confidence is >= a threshold t, its answer is used. Otherwise the query is escalated to a large model, and you pay for both calls.

Write cascade_threshold(small_conf, small_correct, large_correct, small_cost, large_cost, thresholds, target_acc). For every t in thresholds compute:
- accuracy: mean over queries of small_correct[i] if small_conf[i] >= t, else large_correct[i]
- cost: mean cost per query (small_cost for answered queries, small_cost + large_cost for escalated ones)
- escalated: number of escalated queries

Choose the t with the lowest cost among those with accuracy >= target_acc. Ties on cost go to higher accuracy, then lower t.

Inputs: small_conf is a list of floats, small_correct and large_correct are lists of 0/1 (or bools) of the same length (>= 1). Costs are floats. thresholds is a non-empty list of floats.

Return a dict {"threshold": t, "accuracy": rounded to 4 decimals, "cost": rounded to 4 decimals, "escalated": int}, or None if no threshold meets the target. Compare accuracy with the target before rounding.

Functiondef cascade_threshold(small_conf, small_correct, large_correct, small_cost, large_cost, thresholds, target_acc)

Example 1

Input: cascade_threshold([0.9, 0.6, 0.95, 0.4], [1, 0, 1, 0], [1, 1, 1, 1], 0.1, 1.0, [0.5, 0.7, 0.99], 0.9)

Output: {'threshold': 0.7, 'accuracy': 1.0, 'cost': 0.6, 'escalated': 2}

Example 2

Input: small_conf = [0.9, 0.6, 0.95, 0.4], small_correct = [1, 0, 1, 0], large_correct = [1, 1, 1, 1], small_cost = 0.1, large_cost = 1.0, thresholds = [0.5, 0.7, 0.99], target_acc = 0.7

Output: {'threshold': 0.5, 'accuracy': 0.75, 'cost': 0.35, 'escalated': 1}

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Loop over thresholds; inside, loop over queries.

Hint 2

Escalated queries cost small_cost + large_cost.

Hint 3

Filter by the target, then pick by (cost, -accuracy, t).

Common mistakes
  • Charging only large_cost for escalated queries.
  • Rounding accuracy before comparing with the target.
  • Using > instead of >= for the confidence.
Understand the idea

Raising the threshold escalates more queries: accuracy goes up toward the large model's, and so does cost. The right threshold is the cheapest one that still meets the quality bar. Note that escalated queries pay for both models, since the small model already ran. In the example, t = 0.7 escalates two queries, reaches 100 percent accuracy and costs 0.6 per query.

Shapes and data flow
Equal-length lists in, dict or None out.
Topics
Real-world constraints
Similar questions
  • Cheapest GPU fleet that meets throughput and memoryMedium
  • Largest decode batch under a latency SLOMedium
  • Schedule requests under RPM and TPM limitsMedium
  • Spot or on-demand GPUs for a training jobMedium
All Real-world constraints problems
Report
891 / 903
Python 3solution.pyPython idle
1
2
3
4
def cascade_threshold(small_conf, small_correct, large_correct, small_cost, large_cost, thresholds, target_acc):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS