Sub topic 10
Turning findings into a report
This chapter assumes you have read the earlier chapters on tool use, structured outputs, grounding and metric calculation. You should be comfortable reading Python and JSON. By the end you will know how to turn a set of verified findings into a report that a person can read and check.
Report generation is a writing step, not an analysis step
By this point the agent has pulled data and a deterministic tool has computed the metrics. The report step does not discover anything new. Its job is to arrange findings that already exist and to word them clearly.
Keeping this boundary firm prevents most report errors. If you ask the model to "analyze the data and write the report" in one step, it may do arithmetic itself, invent explanations, or round numbers differently in different places. If you hand it a closed list of findings and ask only for prose, there is much less room to go wrong.
The pipeline for this step looks like this:
Start from a findings list
A finding is one short statement of fact, with an id and a source. The ids are what let you trace every sentence in the report back to evidence.
{
"id": "F1",
"text": "Click-through rate was 1.67%",
"metric": "ctr",
"value": 0.0167,
"source": "ads_export_2026-10"
}Build the findings list in code from the output of your metrics tool. Do not ask the model to write the findings. Formatting decisions, such as showing 0.0167 as 1.67%, also belong in code, because the model will format the same number differently across runs.
Fix the report structure in advance
Decide the sections before the model writes anything. A typical monthly report uses four:
| Section | Purpose | Typical findings used |
|---|---|---|
| Summary | Two or three sentences a busy reader can stop at | Headline metrics |
| What changed | Movement against the previous period | Period-over-period deltas |
| Risks | Data gaps, small samples, anomalies | Data quality notes |
| Next steps | Suggested actions, labeled as suggestions | Any finding |
Each section gets its own model call. Short, separate calls are easier to check than one long generation, and a failure in one section does not force you to redraft the others. The cost is a little repeated context, which is usually small because the findings list is short.
Put the section list in code, not in the prompt alone. Then the report always has the same shape, and readers learn where to look.
Prompt for citations
The most useful instruction you can give is to cite the finding ids after each sentence and to leave out anything unsupported. This turns grounding into something you can test mechanically.
import re
FINDINGS = {
"F1": {"text": "Click-through rate was 1.67%", "source": "ads_export_2026-10"},
"F2": {"text": "Cost per click was $0.84", "source": "ads_export_2026-10"},
"F3": {"text": "Cost per acquisition was $17.50", "source": "ads_export_2026-10"},
}
CITE = re.compile(r"\[(F\d+)\]")
def build_prompt(section, findings):
"""Build the prompt for one report section."""
lines = [f"[{fid}] {f['text']}" for fid, f in findings.items()]
return (
f"Write the '{section}' section of a monthly marketing report.\n"
"Use only the findings below. End every sentence with the ids of the "
"findings it relies on in square brackets, before the full stop, "
"for example: Clicks rose [F1].\n"
"If the findings do not support a statement, leave it out.\n\n"
+ "\n".join(lines)
)
def check_citations(draft, findings):
"""Return (reason, sentence) pairs for sentences that fail the check."""
problems = []
for sentence in re.split(r"(?<=[.!?])\s+", draft.strip()):
ids = CITE.findall(sentence)
if not ids:
problems.append(("no citation", sentence))
for fid in ids:
if fid not in findings:
problems.append(("unknown id " + fid, sentence))
return problems
draft = (
"Click-through rate was 1.67% [F1]. "
"Cost per click was $0.84 [F2]. "
"Conversions will double next month. "
"Cost per acquisition fell [F9]."
)
print(build_prompt("Summary", FINDINGS))
print(check_citations(draft, FINDINGS))The final line prints:
[('no citation', 'Conversions will double next month.'), ('unknown id F9', 'Cost per acquisition fell [F9].')]The check catches two different failures. A sentence with no citation is a claim the model made on its own. A sentence citing F9 points at evidence that does not exist, which is worse because it looks grounded.
What the citation check does not prove
A passing check shows that each sentence cites something real. It does not show that the sentence says what the finding says. "Cost per click was $0.84 [F1]" passes, yet F1 is the click-through rate.
Three further checks narrow that gap:
- Run the number check from the verification chapter on the drafted section. It flags any figure that does not appear in the findings.
- Compare each sentence against the cited finding's text, either by a second model call that answers only "does the finding support the sentence?" or by a human spot check.
- Reject causal language unless a finding states the cause. Words such as "because", "driven by" and "due to" are the usual place where a model adds an explanation nobody measured.
Retry with feedback, then stop
When a section fails the check, send it back with the specific problems. A generic "try again" wastes a call, while "the sentence 'Conversions will double next month.' has no citation; remove it or cite a finding" usually fixes the draft in one step.
Limit retries to two or three, as you limit steps in the agent loop. If the section still fails, do not ship it. Replace it with a plain note such as "This section could not be generated automatically" and flag the report for a reviewer. A visible gap is better than text no one can trust.
Control tone and length with structure
Prompts such as "be concise" give inconsistent results. Constraints you can enforce work better:
- Ask for each section as JSON with fixed keys, for example
{"sentences": [{"text": "...", "cites": ["F1"]}]}. Then you no longer parse citations out of prose, and you can check thatcitesis non-empty directly. - Set a maximum number of sentences per section and reject drafts above it.
- State the audience in one line, for example "readers are account managers who know the metrics". This stops the model from explaining what CTR means in every report.
- Keep the same section order and heading names, so reports from different months can be compared side by side.
Assemble the report in code
The model writes section text. Your code writes everything around it: the title, the date range, the data sources, the tables of numbers, and the footer. Tables especially should come straight from the metrics output, not from the model, so every figure is exactly the one the tool computed.
A good report footer lists the sources and the time the data was pulled, for example "Data: adsexport2026-10, pulled 2026-11-26 18:50 UTC". When someone questions a number a month later, this line tells them where to look.
Common mistakes
- Letting the model round or reformat numbers. Format in code and pass strings into the prompt.
- Passing the raw data to the report step. The model then recomputes things. Pass findings only.
- Generating the whole report in one call. Failures are harder to locate and retries cost more.
- Treating a passing citation check as verification. It is one layer. The numbers check and the human review still apply.
- Dropping sections silently when they fail. Readers should see that something is missing.
Exercise
A drafted sentence reads "Spend efficiency improved because the new creative resonated with younger audiences [F2]." The finding F2 says "Cost per click was $0.84". Does it pass check_citations, and what should the pipeline do with it?
Show answer
It passes check_citations, because the sentence has a citation and F2 exists. It should still be rejected. F2 only states a cost per click, so it supports neither "improved" (there is no comparison) nor the causal claim about creative and audiences. A support check against the cited finding, or a rule against unsupported causal words, would catch it. The retry feedback should say to remove the explanation or move it to a labeled hypotheses list.
Exercise
Why does the chapter recommend one model call per section, with a retry limit, instead of one call for the whole report?
Show answer
A separate call per section makes failures local: you can check, retry or replace one section without redrafting the others, and the feedback you send back is specific. The retry limit prevents unbounded cost, and a failed section is replaced by a visible placeholder and flagged for review.