Self-Querying & Citations
Push RAG further with self-querying retrievers that turn natural language into metadata filters, and answers that cite their sources so users can trust and verify them.
Self-Querying & Citations is a free LLM Apps in Production (RAG + Vector DB + Caching) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the LLM Apps in Production (RAG + Vector DB + Caching) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
When Questions Carry Filters
Users ask things like give me 2023 reports about pricing. That sentence contains a filter (year 2023) and a semantic query (pricing).
A self-querying retriever automatically separates the two.
How Self-Querying Works
An LLM reads the question and emits a structured query: the semantic search string plus a metadata filter. The retriever then applies both to the vector store.
Describing Your Metadata
You tell the retriever what fields exist so it knows what it can filter on.
from langchain.chains.query_constructor.schema import AttributeInfo
fields = [
AttributeInfo(name='year', description='Publication year', type='integer'),
AttributeInfo(name='topic', description='Document topic', type='string')
]Building the Retriever
Combine the LLM, the store, a content description, and the field info into a self-query retriever.
from langchain.retrievers.self_query.base import SelfQueryRetriever
retriever = SelfQueryRetriever.from_llm(
llm, vectorstore,
'Company reports', fields
)Seeing It in Action
Now a natural-language question is split into a filter and a search automatically — no manual filter code.
docs = retriever.invoke(
'pricing reports from 2023'
)Why Citations Matter
In production, users must be able to verify answers. Unsourced answers are hard to trust and hide hallucinations. Citations link each claim back to its document.
Carrying Source Metadata
Citations rely on each chunk storing where it came from — file name, page, or URL — in its metadata. Set this at load time.
doc.metadata['source'] = 'policy.pdf#p3'Prompting for Citations
Number the context chunks and ask the model to cite the numbers it used. This is simple and reliable.
ctx = '\n'.join(
f'[{i}] {d.page_content}'
for i, d in enumerate(docs)
)
# 'Cite sources like [1] after each claim.'Mapping Numbers to Sources
After generation, map the cited numbers back to real source metadata so the UI can show clickable references.
sources = {i: d.metadata['source']
for i, d in enumerate(docs)}Verifying Citations
Models sometimes cite wrong or nonexistent sources. A safety check confirms each cited chunk actually supports the claim, flagging unsupported statements.
Putting It Together
Self-querying gets the right documents using filters in the question; citations make the resulting answer transparent. Together they raise both precision and trust in advanced RAG.
Quick Check
Test your advanced RAG knowledge.
Recap
You learned two advanced RAG techniques:
- Self-querying turns natural language into metadata filters plus a semantic query
- Describe your fields so the LLM knows what to filter
- Citations link claims to sources for trust
- Carry source metadata, prompt for citations, and verify them
Filtering and citing together make RAG both precise and trustworthy.
Frequently asked questions
Is the “Self-Querying & Citations” lesson free?
Yes — the full text of “Self-Querying & Citations” is free to read here on the web, and the LLM Apps in Production (RAG + Vector DB + Caching) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the LLM Apps in Production (RAG + Vector DB + Caching) course, upgrade to CoddyKit PRO.
What will I learn in “Self-Querying & Citations”?
Push RAG further with self-querying retrievers that turn natural language into metadata filters, and answers that cite their sources so users can trust and verify them. You practise LLM Apps in Production (RAG + Vector DB + Caching) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start LLM Apps in Production (RAG + Vector DB + Caching)?
No prior experience is required. LLM Apps in Production (RAG + Vector DB + Caching) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Self-Querying & Citations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this LLM Apps in Production (RAG + Vector DB + Caching) lesson?
Yes. Every LLM Apps in Production (RAG + Vector DB + Caching) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.