RAG Agent with Grounded Citations
Retrieval-augmented answering that cites source chunks and refuses when the context does not support an answer.
Pipeline
- Ingest: split documents into ~300-800 token chunks with overlap; keep
doc_id,title,url, and position metadata. - Embed and index chunks in a vector store (pgvector, SQLite-vec, Qdrant, etc.). Optionally add a keyword (BM25) index for hybrid search.
- Retrieve top-k (e.g. 20) candidates, then rerank to the best 5.
- Generate with the prompt below; chunks are labelled
[1]..[n]. - Validate: check every citation number exists; if the answer has no citations, treat it as unsupported.
Prompt
Answer the question using ONLY the sources below.
Cite sources inline like [2]. Every factual sentence needs a citation.
If the sources do not contain the answer, say "I don't know based on the provided sources."
Sources:
{{#each chunks}}[{{@index1}}] {{title}}: {{text}}
{{/each}}
Question: {{question}}Tips
- Hybrid (vector + keyword) retrieval handles names, codes, and IDs far better than vectors alone.
- Evaluate with a small fixed question set and track answer correctness and citation accuracy over time.
Failure modes
- Citations that point to chunks that don't support the claim; validate cited IDs.
- Poor chunking splits answers across chunks.
- Stale index returns outdated facts; schedule re-indexing.
Inspiration
Real-world reports that inspired or relate to this blueprint, credited to Agent Use Cases:
- Make auditable rule-based decisions with RAG-written explanations (agentusecases.ai)
Markdown version: /blueprints/rag-with-citations.md
Want a deeper build of this?
Vote for an extended, fully tested version (more tools, a full workflow file). Votes decide what we build next.
Get one fully built blueprint a week
A weekly email: one blueprint with config, cost, setup time and failure modes, plus a vote on what we build next.
Related blueprints
Agent Tracing and Evaluation Setup
Capture structured traces of every LLM and tool call and run a small regression eval suite on each change.
Evaluator-Optimizer Refinement Loop
One model generates, another grades against a rubric, and the loop repeats until the rubric passes or a limit is hit.
Human-in-the-Loop Approval Gate
Let agents draft freely but require explicit human approval before any irreversible or external action.