Skip to content
/

Check it in code

Agents as stepsChapter 10 of 156 min read

The memo read well, and its deadline was wrong, so this chapter answers: how do you check an agent's work in a way no argument can change?


A step after the review works the deadline out in Python, and compares it with the memo:

Drawing diagram…
InputAgentToolResultA check in code after the review
  • Check in code: a step that works a fact out with Python, and compares the agent's answer with it.
  • Finding: one sentence that says what's wrong.

Set up#

If you closed the terminal, go back into the folder and switch the environment on:

Terminal
$ cd document-desk
$ source env/bin/activate

.env and the contract are still in the folder from chapter 9.

Way 1: ask an agent if it's right#

The memo below is wrong, but it argues for its date. It counts the term to March 1, 2027, a day too far. A checker agent reads the memo and the contract, and gives a typed answer. Let's save this as demo.py:

demo.py
# demo.py
from crewai import Agent
from crewai.flow.flow import Flow, listen, start
from dotenv import load_dotenv
from pydantic import BaseModel, ConfigDict, Field

load_dotenv()

MEMO = (
    "Risk: the contract renews on its own for another 12 months. "
    "The term ends on March 1, 2027, the anniversary of the Effective Date, "
    "so under Section 2.2 notice is due 60 days before that: by December 31, 2026."
)


class Verdict(BaseModel):
    right: bool = Field(description="Is the notice deadline in the memo right?")
    reason: str


class DeskState(BaseModel):
    model_config = ConfigDict(validate_assignment=True)

    text: str = ""
    memo: str = ""


class Desk(Flow[DeskState]):
    suppress_flow_events = True

    @start()
    def read_document(self):
        with open("inbox/acme-saas-agreement.md") as file:
            self.state.text = file.read()
        self.state.memo = MEMO

    @listen(read_document)
    def check_memo(self):
        checker = Agent(
            role="Fact checker",
            goal="Make every date and number match the contract",
            backstory="You check memos against their sources.",
        )
        answer = checker.kickoff(
            "Is the notice deadline in this memo right?\n\n"
            "Memo: " + self.state.memo + "\n\nContract:\n" + self.state.text,
            response_format=Verdict,
        )
        return answer.pydantic


print(Desk().kickoff())
Terminal
(env)$ python demo.py
Output
...right=True reason="Section 2.2 states the agreement renews automatically at the end of each 12-month term unless notice is given at least 60 days before the end of the then-current term. The Effective Date is March 1, 2026, so the first term ends on March 1, 2027. Sixty days before March 1, 2027 is December 31, 2026. The memo's deadline matches the contract."

... stands for the boxes CrewAI draws. Your agent's words will differ. The checker followed the memo's own sum, and called the wrong date right:

Drawing diagram…
InputAgentResultThe memo's argument wins

The memo gave a reason, and the agent took it. In our five runs before this one, it said right=True every time.

Way 2: work it out in Python#

The term starts on March 1, 2026, and lasts 12 months, so its last day is February 28, 2027. timedelta(days=60) counts 60 days back from there. Add the import to demo.py:

Python
from datetime import date, timedelta

Change check_memo in demo.py:

Python
    @listen(read_document)
    def check_memo(self):
        deadline = date(2027, 2, 28) - timedelta(days=60)
        written = f"{deadline:%B} {deadline.day}, {deadline.year}"
        if written in self.state.memo:
            return "the memo has the deadline"
        return f"Notice is due by {written}, and the memo doesn't say so."
Terminal
(env)$ python demo.py
Output
Notice is due by December 30, 2026, and the memo doesn't say so.

The same sum runs every time, so the memo's argument never reaches it:

Drawing diagram…
InputToolResultThe check works the date out itself

A right date, written another way#

Let's fix the memo, and write the date the way much of the world does. Change MEMO in demo.py:

Python
MEMO = "Risk: the contract renews on its own for another 12 months. Send notice by 30 December 2026."
Terminal
(env)$ python demo.py
Output
Notice is due by December 30, 2026, and the memo doesn't say so.

The memo is right now, and the check still reports it. The check only looks for December 30, 2026, letter for letter:

Drawing diagram…
InputToolResultThe right date, not found

Let's look for each way the date is likely written: 30 December 2026, and 2026-12-30, which is what str(deadline) gives.

Change check_memo in demo.py:

Python
    @listen(read_document)
    def check_memo(self):
        deadline = date(2027, 2, 28) - timedelta(days=60)
        written = f"{deadline:%B} {deadline.day}, {deadline.year}"
        forms = [written, f"{deadline.day} {deadline:%B %Y}", str(deadline)]
        if any(form in self.state.memo for form in forms):
            return "the memo has the deadline"
        return f"Notice is due by {written}, and the memo doesn't say so."
Terminal
(env)$ python demo.py
Output
the memo has the deadline

Let's remove the program. The folder, .env and the inbox stay for the next chapter.

Terminal
(env)$ rm demo.py

The two ways side by side#

Compare what decides the answer:

Drawing diagram…
InputResultWhat decides if the memo is right

When a fact can be worked out with a sum, a date or a rule, check the agent's answer in code, not with another agent.

Drawing diagram…
InputResultAn agent or code to check it?

What comes next#

The desk now has agents, a crew and a check. Each run of this chapter called the model and cost money:

Drawing diagram…
InputAgentEvery run calls the model

How do you know all this works, without paying for a run?