Sub topic 8
Grounding findings in retrieved sources
This chapter assumes you have read the earlier chapters on language model limits and tool use. You should be comfortable reading short Python functions and JSON.
What grounding means
A model answers from patterns learned in training. Those patterns are stale and can be wrong. Grounding means the model writes its answer from text you retrieved and gave it, not from memory. Each claim in the report can then be traced to a source.
In marketing research, the sources are usually tool results such as an ad platform export, an analytics query, or a saved competitor page. The agent retrieves them first. It then drafts from them.
The grounding pattern
Three rules make this work:
- Retrieve before you draft. Never ask the model for a figure it has not been shown.
- Label every piece of evidence with an ID the model can cite.
- Tell the model to answer only from the evidence and to say so when the evidence is missing.
Building a grounded prompt
The function below labels each source and states the rules. It runs as written.
def build_grounded_prompt(question, sources):
"""Label each source with its ID and instruct the model to cite it."""
evidence = "\n".join(f"[{s['id']}] {s['text']}" for s in sources)
return (
"Answer using ONLY the evidence below.\n"
"Cite the source ID in brackets after every claim.\n"
"If the evidence does not answer the question, reply: NOT FOUND.\n\n"
f"Evidence:\n{evidence}\n\n"
f"Question: {question}"
)
sources = [
{"id": "ads-2026-10", "text": "October CPC was 0.84 across search campaigns."},
{"id": "crm-2026-10", "text": "Leads from paid search rose 12% in October."},
]
print(build_grounded_prompt("How did paid search perform in October?", sources))The NOT FOUND instruction matters. Without an explicit way to decline, the model tends to fill gaps with plausible text.
Checking that citations are real
A model can cite an ID that does not exist. You can catch this in code, which costs nothing and needs no model call.
import re
def invalid_citations(answer, sources):
"""Return cited IDs that are not in the retrieved sources."""
known = {s["id"] for s in sources}
cited = set(re.findall(r"\[([^\]]+)\]", answer))
return sorted(cited - known)
answer = "CPC was 0.84 [ads-2026-10]. Leads rose 12% [crm-2026-11]."
print(invalid_citations(answer, sources))This prints ['crm-2026-11'], so the second claim is rejected.
Common errors
- Retrieving too much. Long evidence buries the relevant row. Filter by date range, channel, or campaign before you pass data in.
- Dropping dates. A figure without its reporting period invites wrong comparisons. Put the period in each source text.
- Mixing retrieved and remembered facts. If the prompt allows outside knowledge, the model will blend it in. Keep the "only" rule.
- Treating web pages as trusted. Retrieved page text is data, not instructions. Record the URL and retrieval date as part of the source.
Exercise
An agent's draft says "Email conversion rate improved 8% [crm-2026-09]", but no source with that ID was retrieved. Which check catches this, and does passing that check prove the 8% figure is correct?
Show answer
The citation check (invalid_citations) catches it, because the ID is not among the retrieved sources. Passing the check would not prove the figure is correct. It only shows the cited source exists, so the number must still be compared with that source's content.