KatInterview

Design a Grounded Document Q&A Assistant on a Hosted LLM API

System DesignModel answer
HardModel answerSystem Designragllmembeddings

Last updated

Build a small retrieval-augmented chat tool with streaming answers, short memory, resilient API calls and tests, then justify the key design trade-offs.

Task

You are asked to build, and then defend, a small command-line assistant that answers questions about a local set of documents using a hosted LLM provider's embedding and chat endpoints.

Part 1: Implementation

Write a compact Python tool that:

  1. Ingests a small local corpus (Markdown and/or PDF files), splits the text into chunks, computes embeddings, and stores them in a searchable index.
  2. Retrieves the top-k most relevant passages for a user question.
  3. Builds a prompt from the retrieved passages and calls the provider's chat completion endpoint with streaming output.
  4. Maintains short-term conversation memory so follow-up questions work.
  5. Reads the API credential from an environment variable (never hard-coded).
  6. Handles errors, retries and rate limits sensibly.
  7. Ships with a short README and a few smoke tests.

Part 2: Design discussion

Explain and justify your choices for:

  • Chunking strategy
  • Embedding model selection
  • Vector index choice
  • Latency and cost budget
  • Caching
  • Prompt templating
  • Safety and PII handling
  • Logging and monitoring
  • Offline evaluation of retrieval and answer quality
  • Fallback behavior when retrieval finds nothing useful

State your assumptions about corpus size, traffic and latency targets up front. A strong answer shows working, minimal code and quantified trade-offs.

Hints

Hint 1

Start by estimating scale: a few thousand chunks fit in memory as a normalized matrix, so brute-force cosine similarity may be all you need before reaching for a vector database.

Hint 2

Separate retryable failures (rate limits, 5xx, timeouts) from non-retryable ones (other 4xx). Use exponential backoff with jitter and respect any server-provided wait hint.

Hint 3

Decide up front what happens when the best similarity score is low: tell the model there is no context and instruct it to say it does not know, rather than letting it guess.

Hint 4

For evaluation, build a small labeled set of question and source-chunk pairs and measure recall@k and MRR for retrieval separately from answer faithfulness.