Objective
An answer you can't trace is an answer you can't trust. For support bots, research agents, and anything regulated, the agent must say where an answer came from. Here you'll extend RAG so every answer ships with citations: retrieve the top passages, generate the answer from them, and return the exact sources used, each with the citation handle the answer refers to. All in one database, no citation framework. The same pipeline works from any framework or a voice agent — see Use it from your agent at the end.
Want the whole app, not just the queries? The tutorial Build a local RAG app with citations in one database turns this pattern into a working support bot in 28 lines of Python, and compares it with the same app built on Postgres, pgvector and Ollama.
Step 1: Create the cited knowledge base
Each chunk carries a stable doc_ref (the citation handle) alongside its text and embedding.
CREATE TABLE IF NOT EXISTS recipe_rag_cited (
chunk_id INTEGER PRIMARY KEY,
doc_ref TEXT, -- citation handle, e.g. 'SecurityPolicy §4.2'
content TEXT,
embedding VECTOR(384)
);
Step 2: Load documents with citation handles
Policy passages with the references a compliance-minded agent must cite. ON CONFLICT DO NOTHING makes the load safe to run again.
INSERT INTO recipe_rag_cited (chunk_id, doc_ref, content) VALUES
(1,'SecurityPolicy §2.1','All customer data is encrypted at rest using AES-256.'),
(2,'SecurityPolicy §4.2','Access to production databases requires multi-factor authentication and is logged.'),
(3,'Privacy §3.0','Personal data is retained for 24 months after account closure, then permanently deleted.'),
(4,'SLA §1.4','The service guarantees 99.9% monthly uptime, with credits for any breach.'),
(5,'Privacy §5.1','Customers may request a full export or deletion of their data within 30 days.'),
(6,'SecurityPolicy §6.0','Third-party vendors are reviewed annually and must sign a data-processing agreement.')
ON CONFLICT (chunk_id) DO NOTHING;
Step 3: Embed the knowledge base
The embedding model runs in-database — your citation-aware retrieval index in one line.
UPDATE recipe_rag_cited SET embedding = EMBED(content);
Step 4: Retrieve the top passages with their citation handles
Retrieval returns both the text and the doc_ref, so the citation travels with the evidence. We project the similarity as relevance and order by that alias.
SELECT chunk_id, doc_ref, content,
COSINE_SIMILARITY(embedding, EMBED('How is customer data protected and who can access it?')) AS relevance
FROM recipe_rag_cited
ORDER BY relevance DESC
LIMIT 3;
Step 5: Materialize the top sources, then generate a cited answer
First empty the two working tables, so every question starts from a clean slate. Then freeze the top-3 retrieved passages — with their citation handles — into a small table. Concatenate them into one context string, each passage prefixed with its citation handle, and feed that to GENERATE(), telling it to cite each source name inline. To ask another question, put it in Step 4 and in both Step-5 queries that name it (the INSERT INTO recipe_rag_cited_top and the GENERATE prompt), then run Steps 4–6 again.
CREATE TABLE IF NOT EXISTS recipe_rag_cited_top (
chunk_id INTEGER PRIMARY KEY,
doc_ref TEXT,
content TEXT,
relevance DOUBLE
);
CREATE TABLE IF NOT EXISTS recipe_rag_cited_ctx (id INTEGER PRIMARY KEY, sources TEXT);
DELETE FROM recipe_rag_cited_top;
DELETE FROM recipe_rag_cited_ctx;
INSERT INTO recipe_rag_cited_top (chunk_id, doc_ref, content, relevance)
SELECT chunk_id, doc_ref, content,
COSINE_SIMILARITY(embedding, EMBED('How is customer data protected and who can access it?')) AS relevance
FROM recipe_rag_cited
ORDER BY relevance DESC
LIMIT 3;
INSERT INTO recipe_rag_cited_ctx (id, sources)
SELECT 1, GROUP_CONCAT('(' || doc_ref || ') ' || content, ' ') FROM recipe_rag_cited_top;
SELECT GENERATE(
'Answer the question using ONLY these sources, and cite the source name in parentheses inline, e.g. (SecurityPolicy §4.2). Sources: ' ||
sources ||
' Question: How is customer data protected and who can access it? Answer with inline citations:') AS cited_answer
FROM recipe_rag_cited_ctx;