Agent Engineering / Context & Memory
Retrieve context with hybrid search, filters and reranking, and cite it
- MUST enforce access control in the retrieval layer, before ranking: filter by the caller’s tenant, groups and classification server-side. Never retrieve first and ask the model to hide what the user may not see.
- MUST use hybrid retrieval: keyword (BM25) and semantic (embedding) search together. Keyword search finds exact identifiers, error codes and names that embeddings miss; semantic search finds paraphrases.
- SHOULD retrieve a wider candidate set (for example the top 50–150) and rerank it with a reranking model, then pass only the best few passages (for example 5–20) to the model.
- MUST apply metadata filters the question implies (product, version, date range, language, document type) instead of hoping ranking sorts them out.
- MUST cite every claim in the answer to the passage it came from (source and section), and quote exactly where wording matters.
- MUST say when retrieval found nothing relevant, or found conflicting sources, instead of answering from general knowledge as if it were sourced.
- SHOULD prefer the newest version when sources conflict, unless the question asks about a specific version, and say that a conflict existed.
- SHOULD rewrite or decompose vague or multi-part questions into focused queries before retrieving, and retrieve again when the first results do not answer the question.
- SHOULD keep retrieved text compact in context: passages, not whole documents; drop them once the answer is drafted (BP-0011).
- SHOULD use a structured index instead of text retrieval where one exists: a code index for symbols (BP-0010), a database query for records.
- MAY skip retrieval and load the whole source when it is small enough to fit comfortably in context; RAG is for corpora that do not.
When to apply
Section titled “When to apply”Any agent answering from a document corpus, knowledge base or memory store.
Procedure
Section titled “Procedure”- Resolve the caller’s identity and access labels.
- Derive filters and focused queries from the question.
- Run keyword and semantic search with the access and metadata filters; merge the candidates.
- Rerank; keep the best passages.
- Answer with citations; flag gaps and conflicts.
Examples
Section titled “Examples”Good answer shape: “Restart the SMS_EXECUTIVE service, then check the MP status page [SOP sccm-upgrade §3.2, modified 2026-11-10]. The older runbook says to reboot the server instead [runbook mp-recovery §2, 2024-03-01]; the SOP is newer.”
Bad: an answer with no sources, mixing a 2019 procedure with the current one, from a tenant the user cannot access.
Anti-patterns
Section titled “Anti-patterns”- Vector-only search that cannot find “error 0x80070005”.
- Stuffing the top 50 chunks into the prompt unranked.
- Access filtering in the prompt.
- Confident answers when nothing relevant was retrieved.
Checklist
Section titled “Checklist”- Access enforced server-side before ranking
- Hybrid keyword + semantic search; metadata filters applied
- Wide candidate set reranked; few passages passed
- Every claim cited; gaps and conflicts stated