WRITE · TRACE · UNDERSTAND

Project: assemble and verify a permission-scoped RAG response

Use the secure_bm25 contract with k1=1.2,b=.75 over authorized documents.

Read lesson: Project: assemble and verify a permission-scoped RAG response →

Your experiment

Use the secure_bm25 contract with k1=1.2,b=.75 over authorized documents. Request has exactly tenant,roles,query,k,word_budget (nonnegative integer). Visit top-k ranked chunks, admitting whole text only when its whitespace-word count fits remaining budget, skipping oversized chunks. output is a saved provider-output fixture with exactly claims: a list of closed records text,source,start,end,quote. Require nonempty claim text, a source in admitted context, valid integer code-point offsets, and exact quoted text. Return context_ids,words,claims,joined answer and abstained. Empty claims mean abstention. No model inference or semantic entailment judgment is implemented.

Python runs in your browser. Your draft stays on this device.

What to look for

Review each boundary independently: ACLs, corpus statistics, context budget, citation provenance, faithfulness and abstention. A valid structural response is not proof against prompt injection. Replay follows the main orchestration function; helper computations execute and are checked separately.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Project: assemble and verify a permission-scoped RAG response works

The idea

Retrieval-augmented generation, RAG, answers a question from documents fetched at request time. This project builds the parts around the model and leaves the model out: its output is a saved fixture, so every step is ordinary code you can test.

The pipeline is a chain in which each stage narrows what the next may use. Permission filtering decides what can be ranked. BM25 ranking decides what is considered. The word budget decides what is admitted as context. Finally, every claim in the output must quote, by exact character offsets, a document that was admitted. A claim citing anything else raises PermissionError, even when that document exists. When the output has no claims, the response is marked abstained instead of inventing an answer.

How it works

  1. Require the request to have exactly tenant, roles, query, k and word_budget, with a non-negative integer budget.
  2. Rank the caller's authorized documents with secure_bm25 and keep the top k.
  3. Walk the ranked documents and admit a whole text when its whitespace word count fits the remaining budget, skipping any that do not fit.
  4. Require output to be a dict holding only claims, and each claim to have exactly text, source, start, end and quote.
  5. For each claim, check that source is in the admitted context, that the offsets are valid, and that the slice equals quote.
  6. Return context_ids, words, the accepted claim texts, their joined answer, and abstained, which is true when there are none.

What it costs

Retrieval
one BM25 ranking pass
Context
≤ word_budget words
Per claim
one slice comparison

Ranking the authorized documents dominates; packing and claim checks are linear in the context and the number of claims, and no model is called in this Lab.

Where it is used

  • Lewis and co-authors introduced retrieval-augmented generation in 2020, pairing a dense retriever over Wikipedia with a BART generator.
  • Microsoft 365 Copilot grounds its answers in Microsoft Graph content that the signed-in user already has permission to access.
  • Amazon Bedrock Knowledge Bases returns citations to the retrieved source passages alongside each generated response.

Watch out for

  • Validating a citation against all documents instead of the admitted context: a real but unadmitted or hidden document would then pass.
  • Truncating a document to make it fit the budget: the Lab admits whole texts only, which keeps claim offsets valid.
  • Accepting extra keys in request, output or a claim: the schemas are closed, and an unexpected field is an error.