Skip to content
/

A crew in a step

Agents as stepsChapter 9 of 156 min read

The contract's review needs a memo, and someone to check the memo's dates, so this chapter answers: how do two agents work together inside one step?


review_contract runs a writer, then a checker, and keeps what the checker hands back:

Drawing diagram…
InputAgentResultTwo agents inside review_contract
  • Task: one piece of work for one agent, with what it should hand back.
  • Crew: a list of agents and tasks, run in order, each task seeing the ones before it.
  • {text}: a blank in a task's words, filled in by kickoff(inputs=...).

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 inbox are still there from chapter 8. Download the contract into the inbox:

Terminal
(env)$ curl -o inbox/acme-saas-agreement.md https://textview.ai/learn-files/introduction-to-agentic-systems/acme-saas-agreement.md

It starts on March 1, 2026, runs for 12 months, and renews unless notice comes at least 60 days before the end.

Way 1: one agent writes and checks#

One agent is asked to write the memo, then check its own dates. 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

load_dotenv()


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()

    @listen(read_document)
    def review_contract(self):
        writer = Agent(
            role="Contract analyst",
            goal="Tell a manager what could cost them money",
            backstory="You read contracts for a small company.",
        )
        answer = writer.kickoff(
            "Write a memo of at most 60 words about the risks in this contract. "
            "Give the last day to send notice that stops the renewal, as a full date. "
            "Then check every date in your memo against the contract.\n\n" + self.state.text
        )
        self.state.memo = answer.raw


desk = Desk()
desk.kickoff()
print(desk.state.memo)
Terminal
(env)$ python demo.py
Output
...MEMO:Risks: Automatic renewal could obligate a new 12-month term and higher fees (up to 7%). Fees are non-refundable except for breach. Service credits must be requested within 15 days of month-end. Notice to stop renewal must be sent by December 31, 2026, to avoid renewal. All dates have been checked against the contract.

CrewAI draws a box as the agent starts and ends, and ... stands for it. Your agent's words will differ. The memo says its dates were checked, but you only have its word for it:

Drawing diagram…
InputAgentResultOne answer, one agent

The check happened inside the same answer, by the agent that wrote the date. Nothing shows what it checked, or whether it did.

Way 2: a writer and a checker#

A Task gives each agent its own job, and a Crew runs the tasks in order. The check task sees the draft, because a task sees the tasks before it. Change the import in demo.py:

Python
from crewai import Agent, Crew, Task

Change review_contract in demo.py:

Python
    @listen(read_document)
    def review_contract(self):
        writer = Agent(
            role="Contract analyst",
            goal="Tell a manager what could cost them money",
            backstory="You read contracts for a small company.",
        )
        checker = Agent(
            role="Fact checker",
            goal="Make every date and number match the contract",
            backstory="You check memos against their sources.",
        )
        draft = Task(
            description="Write a memo of at most 60 words about the risks in this contract. "
            "Give the last day to send notice that stops the renewal, as a full date.\n\n{text}",
            expected_output="A short memo",
            agent=writer,
        )
        check = Task(
            description="Work out every date in the memo again from this contract, and correct the memo:\n\n{text}",
            expected_output="The corrected memo, and nothing else",
            agent=checker,
        )
        crew = Crew(agents=[writer, checker], tasks=[draft, check])
        result = crew.kickoff(inputs={"text": self.state.text})
        print("draft:", draft.output.raw)
        self.state.memo = result.raw
Terminal
(env)$ python demo.py
Output
draft: MEMO: Risks in Software Subscription Agreement
- Annual fee ($18,000) auto-renews unless you give written non-renewal notice at least 60 days before term ends—last day to send notice is January 1, 2027....MEMO: Risks in Software Subscription Agreement
- Annual fee ($18,000) auto-renews unless you give written non-renewal notice at least 60 days before term ends—last day to send notice is January 30, 2027....

... stands for lines that didn't change. draft.output.raw is what the writer handed on, and the memo is the checker's. The draft named January 1, 2027, and the checker changed it to January 30:

Drawing diagram…
InputAgentResultThe crew inside review_contract

Now each job has its own agent, and you can see what each one handed on.

Is the date right?#

Let's work the deadline out in Python. The term ends on February 28, 2027, and notice must come 60 days before that:

Terminal
(env)$ python
>>> from datetime import date, timedelta
>>> date(2027, 2, 28) - timedelta(days=60)
Output
datetime.date(2026, 12, 30)

Neither memo said December 30. Way 1 was a day late. In Way 2, the checker moved the draft's wrong date a month further away:

Drawing diagram…
AgentToolResultThree dates, one right answer

The checker reads well and sounds sure, like the writer. Both are guessing at a sum Python does exactly. Your run's dates may differ, but in our runs neither agent named December 30.

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 you get back:

Drawing diagram…
InputResultWhat review_contract gets back

When a step's work has parts that need different jobs, give each part a task and run them as a crew inside the step.

Drawing diagram…
InputResultOne agent or a crew?

What comes next#

Both agents wrote clear memos, and both got the deadline wrong:

Drawing diagram…
InputResultA memo that reads well

The memo reads well. Is it right?