AI agents
Key points

Key points

  1. 1Classifying a query before retrieval lets you apply a different retrieved-token cap per query class instead of one fixed cap for everything.
  2. 2A routing table maps each query class to a cap, a retrieval depth and a latency tier, and it should be stored as data you can edit, not hardcoded.
  3. 3You tune each cap from recorded tokens and cost per class, not from intuition.
  4. 4Small, well-designed bundles of retrieval strategies capture most of the savings, because the gains concentrate in simple queries.

Why one fixed cap wastes money on easy questions and starves hard ones

A single retrieved-token cap is a compromise. Set it at 4,000 tokens and every "how do I export a CSV" question pays for 4,000 tokens of context it does not need. Set it at 800 and every policy question that spans four documents arrives at the model half-blind. The Cost-Aware RAG work makes this tension explicit: static retrieval configurations cannot resolve it across heterogeneous query workloads, because definitional queries waste budget on unnecessary context while analytical prompts are underserved by shallow retrieval (Mishra and Naik, AI journal).

The same paper reports what per-query routing bought on its 28-query benchmark: 26% fewer billed tokens than always-heavy retrieval and 34% lower mean latency than always answering without retrieval, with answer-quality parity in both cases (Mishra, arXiv preprint). Read those as the shape of the opportunity, not as a promise for your corpus. The important part is the mechanism: pick a strategy per query, and the savings concentrate in the simpler queries.

There is a second reason to cap at all, beyond cost. Models do not attend uniformly across a long context. Performance is often highest when relevant information sits at the beginning or end of the input and degrades when the model must find it in the middle, even for explicitly long-context models (Liu et al., Lost in the Middle). Stuffing more retrieved text into the prompt is not a free accuracy upgrade. A cap per class is a way of matching how much context a question genuinely needs.

Classify the query before you retrieve

The cap only helps if you know the class before the retriever runs. You need a cheap, fast, auditable classifier that fires in milliseconds, before any expensive retrieval work.

Three classifier families are practical:

Intent classification. Map the question to a small set of intents (how-to, lookup, policy, escalation, small talk). A fine-tuned small model or a rules-plus-embedding hybrid works. The production-RAG query understanding material treats intent detection, metadata filtering and complexity-aware pipeline routing as the layer that sits between the raw user question and the retriever (Raj kumar, Towards AI). That is exactly the position your cap lookup should occupy.

Expected answer shape. Ask what the answer will look like. A single value or short procedure needs little context. A comparison across several documents, or a "summarize all the policies touching X" request, needs a lot. Answer shape is often easier to detect than intent from the raw question.

Latency tier. Some queries arrive on a path with a hard deadline (a voice turn, an inline autocomplete), others on a background path where a slower, deeper retrieval is fine. The latency-budget guidance separates a live voice turn, a support chat and a background research task as having different constraints, and recommends measuring time to first useful output separately from total completion time (Supermemory, latency budgets). Latency tier is a legitimate routing dimension on its own.

A practical default: classify on intent and answer shape, then let latency tier override the cap downward when the request is on an interactive path. Record the classifier's decision and its confidence on every request. When confidence is low, route to a safe middle class rather than guessing at the extreme.

Build the routing table

The routing table is the thing you version, review and tune. Keep it out of code. Store it as a JSON or YAML document keyed by class, with the cap, the retrieval depth and any latency constraint together, because they move as a bundle.

{
  "version": "routing-table-v3",
  "classes": {
    "howto_step": {
      "token_cap": 800,
      "top_k": 4,
      "latency_tier": "interactive",
      "fallback": "howto_step"
    },
    "policy_multi_doc": {
      "token_cap": 4000,
      "top_k": 10,
      "latency_tier": "standard",
      "fallback": "policy_multi_doc"
    },
    "escalation": {
      "token_cap": 6000,
      "top_k": 12,
      "latency_tier": "background",
      "fallback": "escalation"
    }
  }
}

The cap is enforced after retrieval and ranking, by trimming the assembled context to the token budget rather than by dropping whole ranked documents blindly. Token accounting is not portable across providers: a prompt that registers at one count in one tokenizer can exceed it in another, so measure with the tokenizer of the model you actually call (Silicon Data, cost per token).

The paper behind BudgetMem frames this as explicit, query-aware performance-cost control, structuring memory processing as modules each offered in three budget tiers with a lightweight router selecting the tier per query (Zhang et al., BudgetMem). You do not need reinforcement learning to start. A hand-written table captures most of the value, and you can move to a learned router later when you have logs.

ClassTypical questionCapTop-kLatency tierWhy
howto_step"How do I reset the API key?"8004interactiveOne procedure, one doc
lookup_fact"What is the refund window?"6003interactiveSingle value, short answer
policy_multi_doc"What rules govern data retention across regions?"4,00010standardSpans several documents
comparison"Compare plan A and plan B limits"3,0008standardNeeds both sides
escalation"A customer is disputing a charge and threatening chargeback"6,00012backgroundNeeds full history and policy

Two rules keep the table honest. First, the cap is a maximum, not a target: a small question in a big class should still retrieve only what it needs. Second, every class has a fallback so a low-confidence classification still resolves to a cap.

Measure tokens and cost per class

The table is a hypothesis. The logs decide whether it is right. For every request, record the class, the classifier confidence, retrieved tokens before and after trimming, input tokens sent to the model, output tokens, retrieval latency, total latency, and the cost computed from the per-token prices of the model you called.

Because prices vary by orders of magnitude across models and by an 8x or greater input-to-output ratio within a model family, a single blended "cost" number hides which class is expensive for which reason (Silicon Data, cost per token). Store the input and output token counts separately and price them separately.

A worked example makes the tuning loop concrete. Suppose a support assistant handles 10,000 queries a day, and the class mix is 70% how-to, 20% policy and 10% escalation. Under a flat 4,000-token cap at an illustrative $2 per million input tokens, the input bill is 10,000 × 4,000 × $2 / 1,000,000 = $80 a day. With a routed table (800, 4,000 and 6,000 tokens for the three classes), the per-day input token total is 7,000 × 800 + 2,000 × 4,000 + 1,000 × 6,000 = 19,600,000 tokens, which at $2 per million is $39.20 a day. That is a 51% cut on this illustrative workload, driven entirely by the how-to class dropping from 4,000 to 800. Your prices and mix will differ; the arithmetic is the template.

Do not trust that saving until you have checked quality on the class you trimmed. Compare answers on a fixed gold set before and after the change. The latency-budget guidance is blunt about this: treat optimizations as experiments with a fixed set of questions and an explicit quality criterion, and watch for supporting evidence falling outside a shrunken candidate set (Supermemory, latency budgets).

Tune each cap on evidence

With per-class logs, each cap becomes a one-dimensional knob you can move on data. The decision rule is simple:

  • Raise the cap for a class when its answer quality is below target and long multi-document answers are the failure mode.
  • Lower the cap for a class when quality is met but the class's retrieved tokens cluster well below the cap (you are paying for headroom you never use) or when trimming is cutting evidence the model needed.
  • Move a class to a different latency tier, not just a different cap, when its P95 latency is the problem.

One caution on reading the numbers: retrieval quality does not rise monotonically with context volume. Beyond a point, adding more retrieved text can dilute attention and bury the useful passage in the middle of a long prompt (Liu et al., Lost in the Middle). That is a reason to keep the how-to and lookup caps genuinely small, not merely smaller than the policy cap.

When you patch a class's cap, bump the table version and keep the old one. If a quality regression shows up two weeks later, you want to be able to point at the table that was live on the bad request, not reconstruct it from memory.

Keeping the classifier and table healthy over time

Two things drift. The query mix drifts as the product changes, and the classifier drifts as language and the product's vocabulary change. Both break the table quietly.

Track the class distribution weekly. A class that grows fast is a class whose cap now dominates your bill, and the class that shrinks may be over-provisioned. Track classifier confidence alongside accuracy on a labeled sample: a classifier that is confidently wrong is worse than one that routes uncertain queries to the safe middle class.

The retrieval layer has its own failure modes worth watching for the same reason. The mem0 retrieval guide notes that most agent memory failures happen at retrieval, not at write time, and that a query like a specific account code can be drowned out by semantic similarity while keyword retrieval recovers it (mem0, memory retrieval strategies). If your policy and escalation classes keep missing exact identifiers, the fix may be a hybrid retriever inside the class, not a bigger cap.

For teams that need the tuning loop to survive more than one corpus, the same discipline appears in long-term memory design: decide deliberately what gets retained and retrieved rather than saving everything, because a bigger haystack is not the same as better recall (JetBrains, long-term memory). Per-class caps are the retrieval-side version of that decision.

When the workload spans several model types (text for answers, embedding models for retrieval, occasionally an image or audio step), routing each has its own price sheet. Quanta Intelligence's single API covers text, image, video, audio and embedding models behind one key, which keeps the per-class cost accounting in one place instead of across several vendor bills.

FAQ

FAQ

Do I need a machine-learning classifier to start?

No. A rules-plus-embedding classifier or a small fine-tuned model is enough, and the router just needs to be fast, transparent and adjustable per query (Mishra, arXiv preprint). Start with intent and answer shape, add latency tier as an override, and log the confidence. Move to a learned router only once logs show where the hand-written rules misroute.

How many query classes should I have?

Few enough that you can measure each one. Five to eight classes is a practical range: a few interactive small-context classes, a couple of standard multi-document classes, and one background class for escalations. Every class needs enough traffic to produce a usable sample; a class with a handful of queries a week tells you nothing about its cap.

What happens when the classifier is uncertain?

Route to a safe middle class rather than guessing at an extreme. A low-confidence how-to question routed to the policy cap costs a little more than needed; the same question routed to a tiny lookup cap risks an incomplete answer. Record the fallback so you can measure how often uncertainty actually occurs and whether it is drifting.

Is a bigger retrieved-token cap ever the right fix?

Only when the failure is genuinely missing evidence that exists in your corpus and did not make it into the cap. If the evidence is present but the model ignored it, a bigger cap can make things worse, because models attend poorly to material buried in the middle of long contexts (Liu et al., Lost in the Middle). Change the cap on evidence, not as a first resort.

How do I know the table is still working after the product changes?

Re-run your gold set per class on a fixed cadence, and watch class distribution and classifier confidence weekly. A class whose traffic share or confidence shifts is the one most likely to need a new cap. Bump the table version on every change so a regression can be traced to the exact table that was live.

Sources

Sources

  1. Cost-Aware Query Routing in RAG: Empirical Analysis of ...
  2. Cost-Aware Query Routing in RAG: Empirical Analysis of ...
  3. Lost in the Middle: How Language Models Use Long ...
  4. Query Understanding for Production RAG: Intent to Retrieval
  5. Latency Budgets for Memory Retrieval
  6. Understanding LLM Cost Per Token: A 2026 Practical Guide
  7. Learning Query-Aware Budget-Tier Routing for Runtime ...
  8. Memory Retrieval Strategies for AI Agents
  9. What Is Long-Term Memory in AI Agents? A Practical Guide
About the author

About the author

Antonio Evans
Antonio Evans

Antonio Evans is the founder of Quanta Intelligence and a fractional CTO who builds HIPAA-ready AI systems for health and life-science companies. Before that he was technical co-founder of FriendsEAT for twenty years, CEO of Socialdraft, and a data engineer at IBM. Antonio lives with his dog Santino in Lisbon, Portugal.

All articles