Skip to content

Take the model out of the monthly comparison

Turn the comparison you asked the agent for every month into a tested function and a pipeline step that runs without the model.

2 parts · 2 and 14 min · part 2 on a computer

Free course. Your Maestro, an AI, .

On the previous page, you sorted the seven tasks in agent_routine.md by who should do each one. Step 3, comparing each region's total with last month's and listing any region that moved more than 20%, belongs to a deterministic tool. Arithmetic over the prior month's and this month's totals has one right answer once the rule is fixed.

Until now, the analyst handed that step to the agent by pasting agent_prompt.md into a chat every month: compare reports/history.csv, list every region whose moved more than 20% up or down rounded to one decimal, and say if a region has no total last month. That reads as if the rule is already decided, yet it leaves three gaps that a model can settle one way in September and another way in October:

  • The 20% boundary: Does "moved more than 20%" include a region that moved by exactly 20% (such as from 100 to 120), or only changes strictly above 20%?
  • A missing or zero baseline: When a new region appears (or had a total of 0 last month), dividing by last month's total fails. What exact values should replace the number so the next step can read the result?
  • Rounding and order of operations: Does the change get compared against the 20% threshold before rounding to one decimal or after, and how is the rounded value stored (such as 37.1 for West's move from 350.00 to 480.00)?

Turning a repeated agent request into code that runs without the model takes three moves. If you want to talk through how the prompt's wording maps to each condition in Python, ask your Maestro.

  1. Settle what the prompt left vague

    Write the rule as code in compare.py so every edge has one answer: a move counts only when abs(change) > threshold on the unrounded ratio, a region missing or at 0 last month gets "change": None and "note": "no total last month", and flagged moves store round(change * 100, 1) sorted by region.

  2. Pin the rule with known-answer tests

    Before you trust the code, check it in test_compare.py against cases you can work out by hand: the closed September and October totals, a region that moved by exactly 20% (from 100 to 120), and a new region with no row in the prior month.

  3. Run it as a pipeline step that stops on failure

    Call the tests at the top of run_month.py before opening reports/flags.csv. If a test fails, the script exits with status 1 and writes nothing; only when all tests pass does it write the flags for the next step.

Common mistake: Asking the agent to write the script is only half the move

You can ask an agent to draft compare.py for you, and in your own work you often will. Promotion happens when you read the rule it wrote and pin its choices with known-answer tests. Accepting an agent-written script without tests only moves the guesswork from the monthly chat into a file nobody checked.

A good place to stop. Up next: What stays with the agent, and how to use this on your own report · 14 min · on a computer

Try it

Open the workbench below and look at how the files fit together before you edit anything. reports/history.csv holds the closed totals from modules 3 and 4: between September (2026-09) and October (2026-10), East moved from 650.00 to 710.00 (+9.2%), North from 1200.00 to 800.00 (-33.3%), South from 800.00 to 810.00 (+1.3%), and West from 350.00 to 480.00 (+37.1%). Only North and West moved past the 20% threshold.

In test_compare.py, each checks one part of the rule: test_september_to_october checks those four regions, test_exactly_twenty_percent_is_not_flagged checks the 20% where North moves from 100 to 120, and test_new_region_is_flagged checks a Central region that appears in October with no September row.

Run uv run run_month.py once before changing compare.py. Because flag_moves starts as a stub that returns [], you will see test_september_to_october and test_new_region_is_flagged print FAIL, while test_exactly_twenty_percent_is_not_flagged prints PASS (since an empty list happens to match expecting no flags). run_month.py then stops and prints that no flags were written.

Now complete flag_moves in compare.py:

  1. Build two dictionaries mapping region to float(r["total"]): one for rows where r["month"] == previous and one for rows where r["month"] == month.
  2. Loop through sorted(now) so regions come back in alphabetical order.
  3. If a region is missing from last month's dictionary or had a total of 0, append {"region": region, "change": None, "note": "no total last month"} and continue to the next region.
  4. Otherwise compute change = (now[region] - before[region]) / before[region]. When abs(change) > threshold, append {"region": region, "change": round(change * 100, 1), "note": "moved more than 20%"}.

Run uv run run_month.py again. You are done when all three tests print PASS, the terminal prints North: -33.3% (moved more than 20%) and West: 37.1% (moved more than 20%), and the writes reports/flags.csv. If a test still fails after you edit compare.py, share the failure line with your Maestro.

Your turn

Read agent_prompt.md, then complete flag_moves in compare.py so it does what the prompt asked the agent for. Run uv run run_month.py. You're done when all three tests pass and the script prints the flags for October and writes reports/flags.csv.

Check yourself, 1 of 2: In run_month.py, why does test_compare.run_all() run before the script opens reports/flags.csv?

Check yourself: Name one phrase in agent_prompt.md that left a decision open for the agent each month, and explain how compare.py now settles it.

Check yourself, 2 of 2: Name one phrase in agent_prompt.md that left a decision open for the agent each month, and explain how compare.py now settles it.

Ctrl+Enter or ⌘+Enter sends it.

Answer it yourself

What stays with the agent, and how to use this on your own report

Taking the model out of step 3 does not remove the agent from the monthly routine. Instead, it gives each part of the routine the job it fits:

  • The tool (run_month.py) runs the tests, computes the regional changes from reports/history.csv, and writes reports/flags.csv the same way every month without opening a chat.
  • The agent (step 4) reads reports/flags.csv and drafts the two sentences for finance explaining North's -33.3% drop and West's 37.1% rise, without doing any of the arithmetic itself.
  • You (step 7) read those two sentences against reports/flags.csv and decide whether this month's report goes to finance.

Whenever you notice yourself pasting the same comparison, filter, or check into an agent chat every week or every month, apply these same three moves: write down the rule and settle the edge cases your wording left open, pin those choices with two or three known-answer tests, and wrap them in a script that exits before writing output if a test fails. Here in the sandbox, the monthly scheduler is simulated by running uv run run_month.py yourself; on your own machine, a real scheduler runs that exact command alongside the file-arrival, environment, and alert checks you listed at the end of module 4.

The reading ends here; your Maestro carries on.