KatInterview

Build a Grounded LLM Item Recommender Endpoint

System DesignModel answer
MediumModel answerSystem Designllmragprompt-engineering

Last updated

Design a natural-language item recommendation endpoint backed by an LLM: request flow, prompt and output schema, retrieval, hallucination and injection controls, evaluation, and monitoring.

You own a backend service that keeps a product catalog in a relational database. Each item has id, name, description, category and price. The service can already call a hosted open-source LLM over HTTP (send a prompt or chat messages, get generated text back). You must add a new endpoint for this user story:

As a shopper, I type what I am looking for in plain language and get a few recommended catalog items, each with a short reason it fits.

You are not expected to produce a finished, fully tested implementation. Pseudocode is fine, but say what you would test.

Constraints

  • The model's context window cannot hold the whole catalog.
  • Only items that exist in the catalog may be recommended.
  • Any item fact shown to the user (price, name, and so on) must be correct.

Questions you may want to ask first

  • How many results should be returned, and is an empty result acceptable?
  • What is the latency budget, and can responses be cached?
  • Is there user history (such as favorites) that can be used?
  • Does the catalog already have keyword or vector search?

Part 1: Endpoint and flow. Define the request and response contract and walk through each step from user text to response. State which steps are ordinary code and which are the model. Cover limits, the empty result, parsing and validating the model output, and latency, cost and failure behavior of the external call.

Part 2: Prompt. Write the actual prompt (fixed instructions plus the per-request template). Explain your choices, show the required output format, and explain how code consumes it. Cover separation of trusted and untrusted text and the "nothing fits" case.

Part 3: Retrieval, hallucination and quality. Explain how RAG applies, how the model can still hallucinate or be manipulated despite retrieval, and what control you would put on each failure mode. Describe fallback behavior, offline evaluation, online evaluation and production monitoring.

Hints

Hint 1

Let ordinary code decide what the model may see (retrieval) and what the user may see (validation plus a database lookup). The model should only choose among candidates and explain the choice.

Hint 2

Ask the model to return item IDs and reasons only, as strict JSON. Join the IDs back to the database for name and price so the model never supplies a fact that is shown to the user.

Hint 3

Treat both the shopper's query and the catalog descriptions as untrusted data. Put them in delimited sections, tell the model to ignore instructions inside them, and validate the output regardless.

Hint 4

Plan for failure: timeouts, malformed JSON, unknown IDs and empty retrieval each need a defined fallback, such as returning the plain retrieval ranking without reasons.