Skip to content
/

Pick up where it stopped

Ready for real workChapter 13 of 155 min read

A run can stop halfway: the terminal closes while it waits for a person, or a step fails. The state lived only in the program, so the next run starts again and pays for the memo again. This chapter answers: how does a run pick up where it stopped?


The state is saved after every step, and the next run loads it by the run's id:

Drawing diagram…
InputToolThe state outlives the run
  • @persist: saves the flow's state to a file after every step.
  • Run's id: the name the state is saved under. kickoff(inputs={"id": ...}) loads that state back.

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

This chapter needs no model. print("writing the memo") stands in for the agent from chapter 9, so you can see each time it would be paid for.

Way 1: the state lives in the program#

file_report writes into a folder called filed, which doesn't exist yet, so the first run stops there. The last line already gives the run an id, the document's name. Let's save this as demo.py:

demo.py
# demo.py
from pathlib import Path

from crewai.flow.flow import Flow, listen, start
from pydantic import BaseModel, ConfigDict


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

    memo: str = ""


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

    @start()
    def review_contract(self):
        if not self.state.memo:
            print("writing the memo")
            self.state.memo = "Send notice by December 31, 2026."

    @listen(review_contract)
    def file_report(self):
        place = Path("filed/acme-saas-agreement.md")
        place.write_text(self.state.memo)
        return str(place)


print(Desk().kickoff(inputs={"id": "acme-saas-agreement"}))
Terminal
(env)$ python demo.py
Output
writing the memo...FileNotFoundError: [Errno 2] No such file or directory: 'filed/acme-saas-agreement.md'

Let's make the folder, and run it again:

Terminal
(env)$ mkdir filed
(env)$ python demo.py
Output
writing the memofiled/acme-saas-agreement.md

... stands for the lines Python prints about where the error happened. The first run wrote the memo, then stopped at file_report. The second run wrote the memo again, though the first had already done it. With an agent in that step, you'd pay for that memo twice:

Drawing diagram…
InputResultThe memo is lost with the run

The id did nothing here: nothing was saved under it.

Way 2: save the state, and load it back#

@persist goes on the class, and SQLiteFlowPersistence("desk.db") says where to save: a file in the folder. The state is saved under the run's id, the document's name, so the next run for that document finds it. Add the import to demo.py:

Python
from crewai.flow.persistence import SQLiteFlowPersistence, persist

Change Desk in demo.py:

Python
@persist(SQLiteFlowPersistence("desk.db"))
class Desk(Flow[DeskState]):
    ...

Let's remove the folder, so the first run stops again:

Terminal
(env)$ rm -r filed
(env)$ python demo.py
Output
No flow state found for UUID: acme-saas-agreementwriting the memo...FileNotFoundError: [Errno 2] No such file or directory: 'filed/acme-saas-agreement.md'
Terminal
(env)$ mkdir filed
(env)$ python demo.py
Output
Loading flow state from memory for UUID: acme-saas-agreementfiled/acme-saas-agreement.md

The first run found no state saved under acme-saas-agreement, wrote the memo, and stopped as before. @persist had already saved the state when review_contract finished. The second run loaded it, found the memo, and went straight on to file_report. "From memory" is CrewAI's wording; the state came from desk.db:

Drawing diagram…
InputToolResultThe second run starts from the saved memo

A step that doesn't look first#

review_contract writes the memo only if the state has none. Let's take that check out. Change review_contract in demo.py:

Python
    @start()
    def review_contract(self):
        print("writing the memo")
        self.state.memo = "Send notice by December 31, 2026."
Terminal
(env)$ python demo.py
Output
Loading flow state from memory for UUID: acme-saas-agreementwriting the memofiled/acme-saas-agreement.md

CrewAI loaded the saved state, and the memo was written again anyway. @persist brings the state back, but it doesn't skip any step: every step runs, and only a step that looks in the state first can tell its work is done:

Drawing diagram…
InputToolResultThe state is back, and the step runs anyway

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

Terminal
(env)$ rm demo.py desk.db
(env)$ rm -r filed

The two ways side by side#

Compare what the second run starts from:

Drawing diagram…
InputResultWhat the second run starts from

When a run can stop halfway, save its state with @persist under an id you can find again, and let each costly step look in the state first.

Drawing diagram…
InputResultSave the state?

What comes next#

The desk now reads one document, waits, and goes on to the next:

Drawing diagram…
InputOne at a time

One document at a time is slow.