13 November 2026 · Practical AI builds
How do I make AI answers show where they came from?
By the end of this you will have an AI assistant that answers questions from your own documents and puts a numbered source after each point, such as a file name, page number or web address. You can click or look up that source and check the answer for yourself. It suits anyone who uses AI for answers that matter, such as policies, prices, procedures or help docs, and wants to trust them without taking them on faith.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
What you need
- A set of documents or web pages you want the answers to come from, such as PDFs, Word files or your help pages.
- An AI model you can reach through an API, from any of the main providers or a model you run yourself. API use usually costs money per request, so check your provider's pricing.
- A simple retrieval setup that finds the right passages for each question. If you have not built one yet, start with how to build a chatbot that answers from your own files.
- A small script (Python or JavaScript) to glue the parts together, or someone who can write one.
Step by step
- Keep the source details with every piece of text. When you split your documents into chunks, save the file name, page number, section heading and web address alongside each chunk, not just the text.
- Give every chunk a short, stable ID. Something like the file name plus a number works well, so you can always trace an answer back to the exact passage.
- Retrieve a handful of passages per question. Your search step should return the best few chunks, usually three to eight, with their source details still attached.
- Number the passages in the prompt. Put each one in the prompt with a number and its source line, so the model can refer to them by number.
- Tell the model how to cite. Ask it to answer only from the numbered sources, add the number in square brackets after each point, and say plainly when the sources do not cover the question.
[1] source: refunds-policy.pdf, page 2
Refunds are paid to the original card within 14 days of the return arriving.
[2] source: https://example.com/delivery
Standard delivery takes 3 to 5 working days.
Question: How long do refunds take?
Answer using only the sources above. After each sentence, add the
source number in square brackets, like [1]. If the sources do not
contain the answer, say: I could not find that in the documents.
- Check the citations in code before showing the answer. Pull out every number in square brackets and make sure it matches a passage you actually sent. If a number does not exist, or the answer has no citations at all, flag it rather than showing it as checked.
import re
cited = set(re.findall(r"\[(\d+)\]", answer))
sent = {"1", "2"}
unknown = cited - sent
if not cited or unknown:
answer += "\n\n(Sources could not be checked for this answer.)"
- Turn the numbers into links. Swap each [1] for a link to the page, or for the file name and page number, and list the sources under the answer so the reader can open them.
- Look at your provider's built-in options. Some model APIs have a citations feature that returns the exact quoted text for each claim. If yours does, it can replace steps 4 to 6, but check its documentation for what it supports.
What good looks like
Ask ten questions you already know the answers to. Each answer should have a source number after every main point, every number should lead to a real passage, and when you open that passage it should actually say what the answer claims. When you ask something your documents do not cover, the tool should say it could not find it rather than citing something loosely related.
Watch out for
A citation is not proof. A model can attach the right number to a sentence the source does not quite support, or blend two passages into one claim. Spot-check answers regularly, and keep a person checking anything that affects money, safety, health or legal matters. For more on this, see how to keep AI replies from inventing facts.
Also think about who can see the sources. If your answers link to internal files, make sure those links only open for people who are allowed to read them, and do not show private file paths in a public tool.
Questions people ask
Can I get page numbers from PDFs?
Yes, if you split the PDF page by page when you load it and save the page number with each chunk. Scanned PDFs need text recognition first, and the page numbers may not match the printed ones.
Will ChatGPT-style chat apps do this on their own?
Some chat apps show sources when they search the web or your uploaded files, but how well it works varies by product and plan. Building it yourself means you choose the documents and can check every citation in code.
The whole build comes down to three things: keep the source details with every passage, ask the model to cite by number, and check those numbers before anyone relies on the answer. That gives you answers you can trace and check rather than just trust.
Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.