How to Build an AI Agent That Uses Your Documents Safely

When a built-in file feature is enough, and how to build a document agent with the right access, cited passages, current versions, tests and human review.

You want an AI agent that answers questions or works through tasks using your policies, contracts or procedures. Connecting the files is the easy part. The hard part is making sure the agent shows each person only what they may see, works from the current version, points to the passage behind every answer, admits when the documents don’t contain the answer, and never treats text inside a file as an order.

First, check whether you need an agent at all. Plain search or an assistant’s built-in project and notebook features handle many document questions. Build an agent only when the job needs several steps across a defined document set, then keep it safe with seven habits: a small, authoritative document set; access matching who may see what; answers that cite document and passage; a routine for retiring old versions; tests that include unanswerable questions; a named reviewer; and an owner who maintains it all. New to agents? Start with our beginner's guide to building an AI agent.

TL;DR

  • Try search or a built-in project or notebook feature before building an agent.
  • Start small with final approved documents and an owner; keep sensitive categories out unless the tool is cleared for them.
  • Know whether permissions are inherited or copied, fix oversharing first, and put only universally visible documents in a shared collection.
  • Require document, version and passage for every answer, and accept “not in the documents” as a valid result.

Start by checking whether a simpler option is enough

If you only need to find a file, use your document system’s search.

If you want to ask questions about a handful of files, an AI assistant’s built-in document features may be enough. An AI assistant responds within a conversation a person leads. These features vary by vendor and plan and change often; the table reflects vendors’ help pages as of November 2026 [1, 2, 3, 4].

FeatureWhat it does with your documentsWhat to check
ChatGPT Projects (OpenAI)Keeps related chats, files and instructions together so ChatGPT can use them as context [1]File limits vary by plan, from 5 files on Free to 40 on Pro, Business, Enterprise and Edu [1]
Claude Projects (Anthropic)Workspaces with their own knowledge base of uploaded documents; free accounts are limited to five projects [2]On paid plans, large projects switch to a retrieval mode that Anthropic says expands capacity up to 10 times; a redesigned version is in beta for some paid users [2]
Gemini Notebook (Google, formerly NotebookLM)Answers from the sources you add, with citations that quote them [5, 6]Free users can add up to 50 sources per notebook [3]
Microsoft CopilotAnswers grounded in the organizational data each user can already view [4]For Microsoft 365 commercial (work) customers; depends on your organization’s permission setup [4]

OpenAI’s help page, as captured in October 2026, also describes Company Knowledge, which answers organization-specific questions from connected apps on ChatGPT Business, Enterprise and Edu [7].

An AI agent is different: given a goal, it may choose or adapt its steps and tools to reach it, within the limits you set. A tool is an approved action or source of information the agent can use. An agent isn’t automatically better than an assistant. It earns its complexity when the work has several steps, such as checking a request against three policies and drafting a reply for approval. Otherwise, see when you shouldn't build an agent at all.

Other agent types have their own guides: open-web research in the preceding guide on building a research agent, filing and labeling in the next one on sorting and organizing information, and change alerts in the change-monitoring guide.

Document knowledge is not agent memory

When an agent “uses your documents,” it is usually retrieving. A search step finds the passages that best match the question and hands them to the language model, which writes an answer from them [8]. Picture a librarian who pulls three relevant pages rather than the whole cabinet. The technical name is retrieval-augmented generation (RAG): a language model combined with an outside knowledge source, such as your files [9]. The retrieved passages become part of the agent’s context: what it has in front of it for this job.

Memory, or state, is something else. It carries selected information or progress from one step or session to the next, such as a preference saved last week. Products sometimes blur the two; OpenAI describes projects that can use their own chats and files as context for later responses [1]. Keep them separate: the document collection is the authoritative source you maintain; memory is what the agent carries forward. What should persist, and for how long, is covered in giving an agent the context and memory it needs.

Step 1: Pick a small, authoritative document set

Write the agent’s job in one sentence, such as “Answer managers’ questions about leave policy, citing the policy.” List only the documents that job needs, use final approved versions, and name an owner.

Then flag sensitive material. OWASP’s AI security guidance names the kinds of data an AI application can expose, including personal identifiers, health records, financial and legal material, credentials and confidential business data [10]. If your documents include any of these, or client files, HR records or anything regulated, use only an AI system your organization has cleared for that data, and leave the material out until someone with authority approves. Don’t rely on an instruction like “never reveal salaries”; OWASP warns that such system-prompt restrictions may not always be honored [10].

Step 2: Decide who can see what through the agent

A permission is what a person or system is allowed to see or do. The guiding rule is least privilege: give a user or process only the access its tasks require [11]. OWASP warns that with weak or misaligned access controls, a retrieval system can retrieve and disclose personal, proprietary or other sensitive content [9]. Find out which of two models your setup uses.

Inherited permissions. The agent checks each user’s existing access in the source system. Microsoft says Copilot only surfaces organizational data the user can at least view [4], and a Copilot Studio agent using SharePoint shows only content the person chatting may access [12]. Google says Gemini in Workspace won’t access content a user lacks permission to see [5], and Gemini Notebook imports a Drive file only if you have view access, losing the source if that access ends [3]. OpenAI says Company Knowledge respects permissions in connected apps [7]. Anthropic says Claude’s Google Workspace connectors mirror your existing Google permissions [13].

Microsoft’s own Copilot deployment guidance starts with “remediating oversharing” [14]. Our reading: inherited permissions are only as good as the permissions underneath, so an overshared folder becomes easier to search through an AI.

Non-inherited permissions. Files you upload or copy into a tool usually lose their original access rules. In a shared ChatGPT project, ChatGPT can draw on everything in the project, and members with chat access can see its files [1]. On Claude Team and Enterprise plans, members with “Can view” access to a shared project can see its knowledge [2]. A viewer of a shared Gemini Notebook can read all its sources [15], and Microsoft’s Copilot Studio documentation lists no user authentication for documents uploaded to an agent, unlike SharePoint sources [16]. A shared connector may also use one account for everyone. Anthropic’s Claude Tag, in public beta, connects to Google with its own credential; anyone in a channel where it’s on can ask it to read whatever that account can see, so Anthropic recommends a dedicated account that starts with no access [17].

The practical rule: if the agent uses one shared collection, include only documents every user may see. If different people need different documents, use inherited permissions or separate agents, and test either setup in Step 7. For credentials and tool access in general, see giving an agent tool access without too much control.

Step 3: Require answers you can trace to a passage

Every answer should name the document, its version or date, and the passage it relied on. Each of the four vendors above offers some form of this in its builder tools as of November 2026 [18, 19, 20, 16]:

  • OpenAI’s API file search tool retrieves from uploaded files using semantic and keyword search and returns file citations [18].
  • Google’s Gemini API File Search tool retrieves from uploaded files, with citations showing which parts of the documents were used [19].
  • Anthropic’s Claude API citations feature returns the exact passages behind each claim; Anthropic says its own evaluations found it more likely than prompting alone to cite the most relevant passages [20].
  • Microsoft Copilot Studio can be set, by turning off “Allow ungrounded responses,” to return knowledge answers only when they include an in-text citation, though Microsoft notes this sometimes withholds correct answers that lack one [16].

A citation shows where text came from, not that the answer reads it correctly or that the document is current. Still, instruct the agent to cite document and passage every time, and to say the documents don’t cover a question when no passage supports an answer. Instruction-writing itself is covered in our guide to clear goals and boundaries.

Step 4: Keep stale and superseded documents out

An agent that finds last year’s travel policy and this year’s may quote either. OWASP notes that documents from different sources can contradict each other and create knowledge conflicts [9]. Models don’t reliably reject bad text, either. In the ClashEval benchmark, presented at NeurIPS 2024, researchers gave GPT-4o, GPT-3.5, Llama-3, Claude Opus, Claude Sonnet and Gemini 1.5 Flash 1,294 fact-based questions paired with deliberately altered documents [21]. The models often adopted the wrong information even when they knew the right answer, over 60% of the time for GPT-4o [21, 22]. The authors caution that the dataset was intentionally packed with errors, so those rates aren’t real-world rates [21].

In Gemini Notebook, a source is either a copy or an auto-synced version of the original, and Drive imports sync every few minutes [3]. Anthropic says Google Docs added to Claude chats and projects sync with Drive [13]. Uploaded copies don’t change when the original does, and links can break; Microsoft warns that renaming a SharePoint site or folder used by Copilot Studio can sever the connection [12].

We found no published study on how often document agents answer from outdated files. The practices below are our editorial judgment:

  • Keep one “current” folder or library, and move superseded versions to an archive the agent can’t reach.
  • Put the effective date and version in each file name or metadata. OpenAI’s and Google’s file-search tools can both filter by metadata, so an agent can search only current documents [18, 19].
  • Give every document a review-by date and an owner.

Step 5: Expect retrieval errors and plan for them

Retrieval can fail before the model writes a word. In an experience report presented at the CAIN 2024 software engineering conference, Deakin University researchers drew on three RAG case studies in research, education and biomedicine, the last a GPT-4 system tested on 1,000 questions from the BioASQ biomedical dataset [8, 23]. They identified seven failure points, including these [8]:

  • the answer isn’t in the documents;
  • the right passage ranks too low to be retrieved;
  • a retrieved passage never reaches the model;
  • the model has the passage but misses the answer;
  • the answer is incomplete.

When a related question had no answer in the documents, the system could be “fooled into giving a response” [8]. It was a small practitioner study, not a controlled benchmark.

Loading every file into one huge prompt doesn’t fix this. In “Lost in the Middle,” published in 2024 in Transactions of the Association for Computational Linguistics, researchers tested GPT-3.5-Turbo, Claude 1.3, MPT-30B-Instruct and LongChat-13B [24]. Performance was often best when the relevant passage came first or last and dropped when it was buried in the middle [24]. Those models are now dated, and newer ones may behave differently.

Retrieval reduces errors but doesn’t remove them. Legal research providers had marketed RAG as “eliminating” (Casetext) or “avoid[ing]” (Thomson Reuters) hallucinations, and LexisNexis promised “hallucination-free” citations [25]. A preregistered study published in the Journal of Empirical Legal Studies in 2025 tested LexisNexis and Thomson Reuters tools from March to May 2024, counting an answer as a hallucination if it was false or cited a source that didn’t support it [25, 26]. Each company’s tools hallucinated less often than general-purpose GPT-4, but more than 17% of the time [25]. Those products may have changed since. So make “I couldn’t find that in the documents” an acceptable answer, and treat any uncited answer as unverified.

Step 6: Treat document text as information, not instructions

Documents are untrusted content: material the agent processes but should never obey. Prompt injection happens when text in a model’s input changes its behavior in unintended ways. OWASP calls the version that arrives through outside content such as websites or files indirect prompt injection, and notes the text doesn’t have to be visible to a human [27]. OWASP describes a scenario in which hidden text in a submitted document alters how a retrieval-based screening system responds, and it recommends detecting hidden content and validating documents before they enter the collection [9].

Britain’s National Cyber Security Centre argued in December 2025 that language models draw no inherent line between data and instructions, so this risk is unlikely to be fully eliminated and builders should reduce both its likelihood and its impact [28]. For a document agent, that means:

  • accept documents only from known, trusted sources, and check new ones before adding them [9];
  • keep the agent’s tools to the least privilege it needs (read-only where possible), with human approval for anything consequential [27];
  • keep document text clearly separated from the agent’s instructions [27].

For fuller defenses, see protecting an agent from unsafe instructions and untrusted content.

Step 7: Test before anyone relies on it

Evaluation means checking the agent’s outputs against what you expected. The Deakin team concluded that a RAG system can only be fully validated in operation and that its robustness develops over time [8]. Treat pre-launch tests as a minimum, not proof. Build a test set of a few dozen questions (our suggestion, not a research-backed number) covering:

  • Known answers: check the answer, citation and version.
  • Unanswerable questions: related questions the documents don’t cover, where the right result is “not found” [8].
  • Stale-version traps: questions whose answer changed between versions.
  • Permission checks: the same questions from users with different access; nobody should see a document they shouldn’t [12, 3].
  • Sensitive-data checks: requests for things the agent must not reveal.
  • Hidden-content checks: a harmless test file with unusual formatting, which the agent should treat as text, not instructions.

Use dummy copies of sensitive files. Record each run (each time the agent works on a task). The broader method is in testing an AI agent before you trust its work.

Step 8: Put a human review point before answers are used

OWASP warns about overreliance on model output and calls for people to oversee and fact-check it, above all where the information is critical or sensitive [29]. OpenAI’s help page tells Company Knowledge users to review answers and source links before relying on them [7].

Decide who reviews which answers and what they check: does the cited passage say what the answer claims, and is it current? Answers about pay, health, legal rights, client commitments or money go to a qualified person before anyone acts. If the agent drafts a reply or updates a record, add an approval step, where a person says yes before the action happens. See making an agent ask before it acts.

Step 9: Maintain it like a document collection

Name an owner, and keep a log of each retrieval and answer. OWASP recommends detailed logs of retrieval activity and regular audits of the knowledge base [9]. As an editorial suggestion, the owner should review the system monthly or quarterly: retire superseded documents, review a sample of logged answers and citations, confirm linked sources still work [12], and remove access people no longer need. Rerun the test set whenever a document, permission or model changes.

If nobody can own this, use a simpler built-in feature. Ongoing oversight is covered in monitoring an agent after you launch it.

Three example setups

These are illustrative hypotheticals, not tested deployments.

HR policy helper for managers. Documents: published HR policies only. Access: managers, inherited from the HR policy site. Sensitive-data flags: employee files, medical notes and pay data stay out entirely. Human review point: HR confirms any answer before a manager uses it to approve leave or take disciplinary action.

Client contract lookup for a small agency. Documents: signed contracts in one current folder, drafts archived. Access: account leads see only their own clients’ contracts. Sensitive-data flags: client-confidential and legal content, kept in a system your firm has cleared for client data. Human review point: the account lead checks the cited clause before quoting terms to a client, and liability questions go to legal review.

IT how-to helper. Documents: internal setup guides and approved troubleshooting steps. Access: all staff, so only documents everyone may see. Sensitive-data flags: no passwords, network diagrams or security procedures. Human review point: the IT owner reviews a weekly sample of answers, and the agent refers users to IT before any settings change.

Practical Takeaways

  • Try search or a built-in project or notebook feature before building an agent.
  • Start small with final approved documents and an owner; keep sensitive categories out unless the tool is cleared for them.
  • Know whether permissions are inherited or copied, fix oversharing first, and put only universally visible documents in a shared collection.
  • Require document, version and passage for every answer, and accept “not in the documents” as a valid result.
  • Test unanswerable, stale-version, permission and hidden-content questions, and name a reviewer and an owner.

Sources

  1. Projects in ChatGPT. OpenAI Help Center (Internet Archive capture of 2026-11-11), accessed 2026-11-13.
  2. What are projects?. Claude Help Center (Anthropic), accessed 2026-11-13.
  3. Add or discover new sources for your notebook. Gemini Notebook Help (Google), accessed 2026-11-13.
  4. Data, Privacy, and Security for Microsoft Copilot. Microsoft Learn, 2026-11-03, accessed 2026-11-13.
  5. Generative AI in Google Workspace Privacy Hub. Google Workspace Admin Help, 2026-10-06, accessed 2026-11-13.
  6. Use chat in Gemini Notebook. Gemini Notebook Help (Google), accessed 2026-11-13.
  7. Company Knowledge in ChatGPT. OpenAI Help Center (Internet Archive capture of 2026-10-05), accessed 2026-11-13.
  8. Seven Failure Points When Engineering a Retrieval Augmented Generation System. arXiv (full text v1); published in Proceedings of CAIN 2024 (IEEE/ACM), 2024-01, accessed 2026-11-13.
  9. LLM08:2025 Vector and Embedding Weaknesses. OWASP Gen AI Security Project, 2024-11, accessed 2026-11-13.
  10. LLM02:2025 Sensitive Information Disclosure. OWASP Gen AI Security Project, 2024-11, accessed 2026-11-13.
  11. least privilege – Glossary. NIST Computer Security Resource Center, accessed 2026-11-13.
  12. Add SharePoint as a knowledge source (Microsoft Copilot Studio). Microsoft Learn, 2026-10-05, accessed 2026-11-13.
  13. Use Google Workspace connectors. Claude Help Center (Anthropic), accessed 2026-11-13.
  14. Secure and govern Microsoft Copilot: foundational deployment guidance. Microsoft Learn, 2026-11-13, accessed 2026-11-13.
  15. Create a notebook in Gemini Notebook. Gemini Notebook Help (Google), accessed 2026-11-13.
  16. Knowledge sources overview (Microsoft Copilot Studio). Microsoft Learn, 2026-08-04, accessed 2026-11-13.
  17. Connect Google Drive, Calendar, and Gmail (Claude Tag). Claude documentation (Anthropic), accessed 2026-11-13.
  18. File search. OpenAI API documentation, accessed 2026-11-13.
  19. File Search (Gemini API). Google AI for Developers / Google AI Studio documentation, accessed 2026-11-13.
  20. Citations. Claude Platform Docs (Anthropic), accessed 2026-11-13.
  21. ClashEval: Quantifying the tug-of-war between an LLM's internal prior and external evidence (arXiv v2 full text). arXiv, 2024-06, accessed 2026-11-13.
  22. ClashEval: Quantifying the tug-of-war between an LLM's internal prior and external evidence (NeurIPS 2024 poster page). NeurIPS, 2024-12, accessed 2026-11-13.
  23. Seven Failure Points When Engineering a Retrieval Augmented Generation System (CAIN 2024 program entry). CAIN 2024 / conf.researchr.org, 2024-04, accessed 2026-11-13.
  24. Lost in the Middle: How Language Models Use Long Contexts. Transactions of the Association for Computational Linguistics (MIT Press), 2024-02, accessed 2026-11-13.
  25. Hallucination-Free? Assessing the Reliability of Leading AI Legal Research Tools. Journal of Empirical Legal Studies (listing: Stanford Law School), 2025-06, accessed 2026-11-13.
  26. Hallucination-Free? Assessing the Reliability of Leading AI Legal Research Tools (arXiv v1). arXiv, 2024-05-30, accessed 2026-11-13.
  27. LLM01:2025 Prompt Injection. OWASP Gen AI Security Project, 2024-11, accessed 2026-11-13.
  28. Prompt injection is not SQL injection (it may be worse). UK National Cyber Security Centre (NCSC), 2025-12-08, accessed 2026-11-13.
  29. LLM09:2025 Misinformation. OWASP Gen AI Security Project, 2024-11, accessed 2026-11-13.