Yashotantra Academy logo
level 1
Curious · 0 XP
streak
0d
perfect
0
badges
0

100 XP to Apprentice

Operations · hard

12. Cost, latency and caching in RAG systems

Budget every stage; cache the repeatable ones.

1 min read3 questionsup to 50 XPtopic 12/12

Read in another language

AI translates the explanation and quiz text while preserving code and answer scoring.

A RAG request has a latency budget spread across stages. Typical targets for an interactive assistant:

| Stage | Budget | Lever | | --- | --- | --- | | Query rewrite | 100-300 ms | small fast model, or skip for first turn | | Embed query | 10-40 ms | small embedding model, cache by query hash | | Hybrid search | 20-80 ms | index tuning, metadata pre-filter | | Rerank 50 → 6 | 60-200 ms | batch, cap candidates, cache | | Generation | 1-4 s | streaming, prompt caching, smaller model where possible |

Caching layers that matter: - Exact query cache (normalised query + tenant + doc-version) — huge hit rates on support workloads. - Semantic cache — reuse an answer when a new query is within a similarity threshold; require a conservative threshold and invalidate on document change. - Prompt prefix caching — stable system prompt and tool schemas cost far less on repeat.

ts
const key = `${tenant}:${corpusVersion}:${normalise(query)}`;
const hit = await cache.get(key);
if (hit) return hit;                       // stream from cache
const answer = await ragPipeline(query);
await cache.set(key, answer, { ttl: 3600 });

Always stream tokens: perceived latency is dominated by time-to-first-token, not total time.

Gotcha: semantic caches leak across tenants and go stale after edits. Key them by tenant *and* corpus version, and keep the threshold strict.

How to study this topic

  1. 1. Read the explanation once end to end, skipping the code.
  2. 2. Re-read the code snippets and say out loud what each line does.
  3. 3. Answer the quiz without scrolling back — that is the real check.
  4. 4. Read every explanation, even for the answers you got right.

Scoring

  • · Up to 30 XP from quiz accuracy.
  • · +15 XP for a perfect first attempt.
  • · +5 XP while a 3-day streak is running.
  • · 75% or higher marks the topic complete.

Check your understanding

  1. 1. What most improves *perceived* latency in a RAG chat?

  2. 2. How should a semantic answer cache be keyed?

  3. 3. Which stage is usually the largest slice of RAG latency?

Badges

First light
Pass your first topic
Sharp shooter
3 perfect first-try quizzes
On a roll
Study 3 days in a row
Week warrior
Study 7 days in a row
Grinder
Earn 500 XP
Deep diver
Earn 1500 XP