Data sources and RAG
A model doesn't know your handbook, your product or your policies. Data sources fix that. Attach your files, and the agent looks up the relevant parts before it answers. This technique is called retrieval-augmented generation (RAG).
An open-book exam
Think of RAG as letting the agent sit an open-book exam. It doesn't memorize your files. For each question, it flips to the most relevant pages, reads them, and answers from what it just read.
That has three big benefits:
- Answers come from your content, not from the model's general knowledge.
- You can update knowledge by uploading a new file. No retraining.
- You can check its work. Every reply tells you which passages it used.
What happens behind the scenes
When you deploy, Custodian prepares your files once:
- Extract the text from each file.
- Split it into short passages, roughly a paragraph each. Neighbouring passages overlap slightly so sentences aren't cut in half.
- Index each passage by its meaning, using an embedding. That's a list of numbers that captures what the text is about, so "refund window" and "how long to return an item" end up close together.
On every message, the agent:
- Finds the 4 passages whose meaning is closest to the question.
- Adds them to the prompt with the question.
- Writes an answer based on them.
"How long do I have to return a laptop?"
↓ find the closest passages
"Returns are accepted within 30 days..." (handbook.pdf)
"Electronics must be unopened..." (handbook.pdf)
↓ answer from them
"You have 30 days, as long as the laptop is unopened."
You don't set up a database or write any of this. You just attach the file.
Attach files
- Python
- TypeScript
from custodian_labs import Custodian
app = (
Custodian(model="gpt-6-luna", system_prompt="Answer from the provided docs.")
.add_data_source_file("./support-handbook.pdf") # call again for more files
.deploy()
)
reply = app.chat("What is the refund policy?")
print(reply.retrieved_contexts) # the passages used for this answer
import { Custodian } from "@custodianlabs/sdk";
const app = await new Custodian({ model: "gpt-6-luna", systemPrompt: "Answer from the provided docs." })
.addDataSourceFile("./support-handbook.pdf") // call again for more files
.deploy();
const reply = await app.chat("What is the refund policy?");
console.log(reply?.retrievedContexts); // the passages used for this answer
In browsers or edge runtimes with no filesystem, pass bytes instead:
addDataSourceFile({ data, filename: "manual.pdf" }).
Supported files
| Kind | Extensions |
|---|---|
| Text | .txt, .md, .csv, .json, .yaml, .yml, .log |
.pdf | |
| Word | .docx, .odt |
| Excel | .xls, .xlsx, .xlsm, .xltx, .xltm |
Text files must be UTF-8. PDFs need selectable text.
What RAG is good at, and what it isn't
| Works well | Works less well |
|---|---|
| "What does the policy say about X?" | "Summarize the whole 300-page manual" |
| "How do I configure Y?" | "How many rows in this spreadsheet have Z?" |
| Questions answered by one or two passages | Questions that need every part of a file at once |
The agent only reads a few passages per question. Answers that depend on the whole file at once, like totals or full summaries, may miss things.
Getting better answers
- Tell the agent what to do when the answer isn't there. "If the documents don't cover it, say so" stops it guessing.
- Use clear headings. They help each passage make sense on its own.
- Keep files focused. Several small, on-topic files beat one giant one.
- Remove outdated content. Old passages can be retrieved just as easily as new ones.
- Check
retrieved_contexts. If the right passage isn't there, reword the file or the question. - Mask sensitive files first. See RAG on sensitive data.
Files in a squad
A file belongs to the agent you attach it to. Other agents in the squad don't search it. That lets each specialist have its own knowledge.
Files or tools?
| Use a data source when | Use a built-in tool when |
|---|---|
| You have the knowledge as files | The answer depends on current or public information |
| The content is fairly stable | The source changes often, or you don't know it in advance |
You can use both on the same agent.
See it in code: RAG agent template.