Design a Prompt-Aware RAG Query Service
Serve grounded answers through multiple prompt templates while safely reusing versioned prompt-prefix cache work.
Problem statement
Design a retrieval-augmented generation (RAG) query service that answers questions from a versioned text corpus.
For this exercise, assume each authenticated caller belongs to one tenant and selects one approved immutable prompt-template version, such as answer, summarize, or compare.
The service retrieves only evidence that caller may access, renders the selected instructions and evidence as a prompt prefix, and returns a newly generated answer with citations.
Also assume the model provider can return an opaque reusable cache handle for a prepared prompt prefix. Final answers are not cached. A handle may be reused only for the exact tenant and access scope, evidence and corpus generation, prompt-template version, and model configuration that created it.
A template, corpus, or access-policy change must stop affected reuse within 60 seconds.
Concurrent requests for the same missing prefix should share one cache build. Waiters must have a bounded wait or an explicit fallback, and a partial or failed handle must never become reusable.
Keep document-ingestion internals, model training, chat history, the prompt-authoring UI, and the provider's internal cache implementation out of scope.
What to build
- Answer a tenant-scoped question from authorized retrieved evidence and return citations.
- Resolve and pin one approved immutable prompt-template version for each query.
- Reuse or create an opaque prompt-prefix cache handle only for an exact authorized, versioned scope.
- Give concurrent cache misses, provider rejection, expiry, and generation failure bounded, explicit outcomes.
At scale
- For this exercise, assume 1,000 query requests per second and a 20x hot-prefix traffic skew.
- For this exercise, assume the searchable corpus contains about 10 million versioned chunks across tenants.
- For this exercise, target at most 200 ms p95 service overhead when a valid prompt-cache handle is reused and four seconds p95 end to end on a cache miss.
- For this exercise, template, corpus, and access-policy changes must stop affected handle reuse within 60 seconds.