Skip to content
/

Who decides the next step

Code in chargeChapter 1 of 155 min read

Every document needs its own work, so a program must first answer one question: who decides which step comes next?


In this course, we'll build a document desk: a Python program that gives each document in a company's inbox the work it needs.

Drawing diagram…
InputToolThe document desk
  • Step: one piece of work, such as reviewing a contract.
  • Route: the choice of which step a document goes to.

Three things can make that choice: nobody, the AI, or your own code. Let's try each one.

Set up#

Let's make a folder for the course, with an environment for the packages that later chapters install:

Terminal
$ mkdir document-desk
$ cd document-desk
$ python3.13 -m venv env
$ source env/bin/activate

CrewAI, which we'll install later, needs Python 3.10 to 3.13. On Windows, the last two lines are py -3.13 -m venv env and env\Scripts\activate.

Way 1: nobody decides#

Every document goes through the same steps, in the same order.

Drawing diagram…
InputToolA fixed order

Let's save this as demo.py:

demo.py
# demo.py
def review_contract(document):
    return "risks found in the " + document


inbox = ["contract", "invoice", "trip schedule"]
for document in inbox:
    print(review_contract(document))
Terminal
(env)$ python demo.py
Output
risks found in the contractrisks found in the invoicerisks found in the trip schedule

Nothing asked what each document was, so all three were reviewed as contracts.

Way 2: the AI decides#

The AI picks a step by writing its name, and the program runs whatever step it names.

Drawing diagram…
AgentToolResultThe AI picks the step

Here are the two kinds of answer our real AI gave for the trip schedule. Change demo.py to this:

demo.py
# demo.py
steps = {
    "review_contract": "reviewed the contract",
    "check_invoice": "checked the invoice",
    "hand_to_person": "handed it to a person",
}

ai_answers = [
    "hand_to_person",
    "I have handed the complete offsite agenda to the necessary person as instructed.",
]
for answer in ai_answers:
    if answer in steps:
        print("step ran:", steps[answer])
    else:
        print("no step ran")
Terminal
(env)$ python demo.py
Output
step ran: handed it to a personno step ran

The second answer says it handed the schedule over, but it names no step, so nothing ran.

In ten real runs, the schedule reached a person 7 times. In the other 3, the AI wrote that second sentence, and no error appeared.

Way 3: the AI names it, your code decides#

The AI only says what the document is, in one word, and an if in your code picks the step.

Drawing diagram…
AgentToolThe AI names it, your code routes it

Let's test it on four answers, including the two odd ones from Way 2. Change demo.py to this:

demo.py
# demo.py
def choose_step(word):
    if word == "contract":
        return "review the contract"
    elif word == "invoice":
        return "check the invoice"
    else:
        return "hand it to a person"


answers = [
    "contract",
    "invoice",
    "Other.",
    "I have handed the complete offsite agenda to the necessary person as instructed.",
]
for word in answers:
    print(choose_step(word), "<-", word)
Terminal
(env)$ python demo.py
Output
review the contract <- contractcheck the invoice <- invoicehand it to a person <- Other.hand it to a person <- I have handed the complete offsite agenda to the necessary person as instructed.

else has no condition, so it catches every answer the lines above it miss.

With a real AI, we ran a contract, an invoice and the trip schedule ten times each: all 30 went to the right step.

Checking for other instead of using else#

Checking for the word other looks tidier, so let's try it. Change the else in choose_step to this:

Python
    elif word == "other":
        return "hand it to a person"
Output
review the contract <- contractcheck the invoice <- invoiceNone <- Other.None <- I have handed the complete offsite agenda to the necessary person as instructed.

Other. isn't other to Python, so no return ran and the function gave back None, with no error.

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

Terminal
(env)$ rm demo.py

The three ways side by side#

Who decidesThe trip schedule, in 10 runsWhy
NobodyReviewed as a contract 10 timesNothing asks what it is
The AITo a person 7 times, lost 3 timesA sentence can claim a step that never ran
The AI names, your code decidesTo a person 10 timeselse sends every answer to a step

Let the AI say what a document is, and let your code decide which step comes next.

When to use which#

The first course called these a workflow, where code sets the path, and an agent, where the AI does: see Agents and workflows.

  1. Does every input need the same steps? Use a fixed order.
  2. Can you list the kinds of input ahead of time? Let the AI name the kind, and let your code choose the step.
  3. Can nobody write the steps down in advance? Let the AI choose.

What comes next#

Under if word == "contract":, the desk will review the contract and then check the review. If the internet drops during the check, the review we paid for is lost.

So how can we keep what one step made when the next one fails? The next chapter answers that with CrewAI's flow.