Sub topic 10
Turning findings into a report
This chapter assumes you can read basic Python, know what impressions, clicks, conversions and spend mean, and have seen JSON. It also assumes you have already computed metrics with code (see the previous chapter) and have a prompt that returns structured output.
The goal: a report built from facts
A report is the last step of the agent's work. The model should not invent anything here. It should turn a set of verified findings into readable text. So the design rule is simple: compute first, then write. The report step receives numbers. It does not produce them.
From findings to sections
Start with a fixed report shape. A consistent shape makes reports easy to compare month to month, and it makes checking easier. A typical marketing report has these sections:
- Summary: two or three sentences with the main result.
- Key metrics: a table of impressions, clicks, spend, conversions, CTR, CPC and CPA.
- What changed: differences from the previous period.
- Risks and gaps: missing data, small samples, zero denominators.
- Recommended next steps: marked as suggestions for a person to decide.
Keep the numbers out of free text where you can. Put them in a table that your code builds, and let the model write only the short prose around it.
Pass structured findings to the model
Give the model a JSON object of findings and ask for prose in a fixed set of fields. Asking for fields keeps each section separate, so your code can place them in the template.
import json
findings = {
"period": "2026-10",
"metrics": {"ctr": 0.0167, "cpc": 0.84, "cpa": 17.5},
"previous": {"ctr": 0.0150, "cpc": 0.90, "cpa": 19.0},
"gaps": ["No conversion data for campaign C-12"],
}
def build_prompt(findings):
return (
"Write a marketing report summary. Use only the numbers in the "
"findings below. Do not calculate new numbers. If something is "
"missing, say so.\n"
"Return JSON with keys: summary, what_changed, next_steps.\n\n"
+ json.dumps(findings, indent=2)
)
print(build_prompt(findings))The instruction "do not calculate new numbers" matters. Percentage changes are arithmetic, so compute them in code and add them to the findings. Otherwise the model may state a change that no tool produced.
Assemble the report in code
Your code, not the model, builds the final document. It inserts the model's text into a template and renders the metrics table directly from the data.
def render_report(findings, prose):
m = findings["metrics"]
lines = [
"# Campaign report " + findings["period"],
"",
"## Summary",
prose["summary"],
"",
"## Key metrics",
"| Metric | Value |",
"|---|---|",
"| CTR | " + str(m["ctr"]) + " |",
"| CPC | " + str(m["cpc"]) + " |",
"| CPA | " + str(m["cpa"]) + " |",
"",
"## Risks and gaps",
]
lines += ["- " + g for g in findings["gaps"]]
lines += ["", "## Suggested next steps", prose["next_steps"]]
return "\n".join(lines)
prose = {
"summary": "CPA fell from 19.0 to 17.5 while CTR rose.",
"next_steps": "Review campaign C-12 tracking.",
}
print(render_report(findings, prose))Because the table comes from your data, the model cannot misquote a metric there.
Common errors
- Letting the model write the table. It may round, reorder or change values.
- Hiding gaps. If a campaign has no conversion data, the report must say so. A missing value is not zero.
- Mixing periods. Label every number with its period.
- Presenting suggestions as conclusions. Mark next steps as proposals.
The next chapter covers checking the finished draft before anyone reads it.
Exercise
Why should code, not the model, compute the percentage change in CPA between two months and render the metrics table?
Show answer
The model can make arithmetic or rounding mistakes and may state numbers no tool produced. Code gives the same result every time, and values in the table can be traced to the data. The model then only writes prose around verified numbers.
Exercise
A campaign has zero conversions, so CPA is None. How should the report handle it?
Show answer
It should list this under risks and gaps, show the CPA as not available rather than 0, and avoid any prose that implies a cost figure. A zero would suggest free conversions and mislead the reader.