Skip to content
/

What the steps remember

Code in chargeChapter 3 of 155 min read

The check must compare the summary with the contract, so this chapter answers: where can a later step find what an earlier step made?


Our contract says "Cancel 60 days before it ends.", and the review's summary says 30, wrong on purpose. Follow what happens if somebody believes the summary:

Drawing diagram…
InputToolResultWhy the check needs the contract
  • State: the flow's own memory, a dictionary that every step can read and change.
  • Key: the name a value is kept under in the state, such as "contract".

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

Way 1: hand the contract along#

A new stand-in step, shorten_summary, sits between the review and the check, and needs only the summary. Look at what each arrow has to carry:

Drawing diagram…
ToolEvery step carries the contract

Let's save this as demo.py:

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 "Cancel 60 days before it ends."

    @listen(read_document)
    def review_contract(self, contract):
        summary = "Cancel 30 days before it ends."
        return contract, summary

    @listen(review_contract)
    def shorten_summary(self, both):
        contract, summary = both
        return contract, summary[:60]

    @listen(shorten_summary)
    def check_summary(self, both):
        contract, summary = both
        if summary in contract:
            return "the summary matches the contract"
        return "the summary is not in the contract"


print(Desk().kickoff())
Terminal
(env)$ python demo.py
Output
the summary is not in the contract

The check found the mistake, but shorten_summary had to take the contract and hand it on without ever using it.

Way 2: put it in the state#

Each step puts what it makes in self.state, and any later step reads it from there:

Drawing diagram…
ToolResultThe steps share one state

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):
        self.state["contract"] = "Cancel 60 days before it ends."

    @listen(read_document)
    def review_contract(self):
        self.state["summary"] = "Cancel 30 days before it ends."

    @listen(review_contract)
    def shorten_summary(self):
        self.state["summary"] = self.state["summary"][:60]

    @listen(shorten_summary)
    def check_summary(self):
        if self.state["summary"] in self.state["contract"]:
            return "the summary matches the contract"
        return "the summary is not in the contract"


desk = Desk()
print(list(desk.state))
print(desk.kickoff())
print(list(desk.state))
Terminal
(env)$ python demo.py
Output
['id']the summary is not in the contract['id', 'contract', 'summary']

CrewAI made the state with the desk. Before kickoff(), it held only id, which CrewAI puts in for every desk; after it, the contract and the summary were there too.

Each step now takes only self, and shorten_summary never touches the contract. Nobody had to hand the contract to check_summary. It was in the memory the whole time.

Why every step sees the same state#

All the steps belong to one desk, so self in each of them is that desk:

Drawing diagram…
InputToolResultOne desk, one state

A key spelled wrong#

A dictionary takes any key, so a typo makes no error where it's written. Change review_contract in demo.py:

Python
    @listen(read_document)
    def review_contract(self):
        self.state["sumary"] = "Cancel 30 days before it ends."
Terminal
(env)$ python demo.py
Output
['id']Error executing listener shorten_summary: 'summary'Traceback (most recent call last):...KeyError: 'summary'

Follow the key from where it's put to where it's read:

Drawing diagram…
InputToolResultThe typo, and where the error shows

Let's remove the program. The folder stays for the next chapter.

Terminal
(env)$ rm demo.py

The two ways side by side#

Compare who has to carry the contract:

Drawing diagram…
InputResultWho carries the contract

Three other places seem to work too, and each one breaks somewhere:

  • A variable outside the class: with two desks at once, the second contract replaced the first, 5 times of 5, with no error.
  • self.contract: works while the program runs, but it isn't part of the state, so the saving in a later chapter leaves it out.
  • self.method_outputs[0]: works until a step is added in front, and the places in the list move.

Return what only the next step needs, and put in the state what a later step needs.

Drawing diagram…
InputResultReturn it, or put it in the state?

LangGraph, another library for building programs with AI, also gives all its steps one shared state.

What comes next#

The state takes any key and any value, so self.state["notice_days"] = "sixty" puts in a word where a number belongs, with no error.

So what stops a step from putting the wrong thing in?