Skip to content
/

One step after another

Code in chargeChapter 2 of 155 min read

A contract goes through three steps, and the last one can fail, so this chapter answers: how do we keep what one step made when the next one fails?


Follow the arrows: each step hands its result to the next one.

Drawing diagram…
ToolResultThree steps, and what each one hands on

The steps here are stand-ins that call no AI, so every run is free. On the real desk, the review is the step we pay for.

Set up#

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

Terminal
$ cd document-desk
$ source env/bin/activate
(env)$ pip install crewai==1.15.45

On Windows, the second line is env\Scripts\activate.

Way 1: call each step yourself#

Your code calls the steps one after another, so an error in the last step ends the whole program.

Drawing diagram…
ToolResultPlain calls: one error ends everything

Let's save this as demo.py:

demo.py
# demo.py
def read_document():
    return "# Software subscription agreement"


def review_contract(text):
    print("review finished")
    return "risk summary of: " + text


def check_summary(summary):
    raise ConnectionError("no internet")


text = read_document()
summary = review_contract(text)
checked = check_summary(summary)
Terminal
(env)$ python demo.py
Output
review finishedTraceback (most recent call last):...ConnectionError: no internet

The review finished, but the error ended the program, and the summary went with it.

Way 2: let a flow call each step#

A CrewAI flow calls every step for you, and keeps each result as it goes. Each step gets a note: a line starting with @, just above it.

  • @start(): marks the first step.
  • @listen(step): runs this step with the result of step.
  • kickoff(): starts the flow.
  • method_outputs: the list of results the flow has kept so far.
Drawing diagram…
InputToolResultA flow: the library calls each step and keeps its result

Change demo.py to this:

demo.py
# demo.py
from crewai.flow.flow import Flow, listen, start


class Desk(Flow):
    suppress_flow_events = True

    @start()
    def read_document(self):
        return "# Software subscription agreement"

    @listen(read_document)
    def review_contract(self, text):
        return "risk summary of: " + text

    @listen(review_contract)
    def check_summary(self, summary):
        raise ConnectionError("no internet")


desk = Desk()
try:
    desk.kickoff()
except ConnectionError as error:
    print("stopped:", error)
print(desk.method_outputs)

A class groups the steps, and (Flow) gives ours everything a CrewAI flow has. Each step takes self, the desk itself, which Python passes in for us. suppress_flow_events = True hides CrewAI's progress boxes.

Terminal
(env)$ python demo.py
Output
Error executing listener check_summary: no internetstopped: no internet['# Software subscription agreement', 'risk summary of: # Software subscription agreement']

The first line is CrewAI reporting the error, and the second is our except. The check still failed, but the flow had already kept the text and the summary.

The notes decide the order#

Each step's note names the step it waits for, so the order in the file doesn't matter.

Drawing diagram…
InputToolThe notes, not the file, set the order

Let's write the steps from last to first, and let the check work. Change demo.py to this:

demo.py
# demo.py
from crewai.flow.flow import Flow, listen, start


class Desk(Flow):
    suppress_flow_events = True

    @listen("review_contract")
    def check_summary(self, summary):
        return "checked " + summary

    @listen("read_document")
    def review_contract(self, text):
        return "risk summary of: " + text

    @start()
    def read_document(self):
        return "# Software subscription agreement"


print(Desk().kickoff())

The names are in quotes because Python hasn't read those steps yet.

Terminal
(env)$ python demo.py
Output
checked risk summary of: # Software subscription agreement

read_document ran first though it's written last, and kickoff() gave back the last step's result.

Calling the next step yourself#

It can look natural for the review to call the check. Change review_contract in demo.py:

Python
    @listen("read_document")
    def review_contract(self, text):
        return self.check_summary("risk summary of: " + text)
Terminal
(env)$ python demo.py
Output
checked checked risk summary of: # Software subscription agreement

The check ran twice. Follow the two arrows into it:

Drawing diagram…
InputToolTwo calls reach the check

Let's remove the program. CrewAI stays installed for the next chapter.

Terminal
(env)$ rm demo.py

The two ways side by side#

Compare what is left when the check fails:

Drawing diagram…
InputToolResultWhat is left after the check fails

In a flow, each step says what it waits for, and the flow makes every call.

To choose, answer the questions from the top:

Drawing diagram…
InputResultPlain calls or a flow?

What comes next#

The real check must compare the summary with the contract, but only the summary reaches it:

Drawing diagram…
ToolThe check needs the text, but gets only the summary

So where can a later step find what an earlier step made?