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.
- 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:
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.
Let's save this as demo.py:
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.
Here are the two kinds of answer our real AI gave for the trip schedule. Change demo.py to this:
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.
Let's test it on four answers, including the two odd ones from Way 2. Change demo.py to this:
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:
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.
The three ways side by side#
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.
- Does every input need the same steps? Use a fixed order.
- Can you list the kinds of input ahead of time? Let the AI name the kind, and let your code choose the step.
- 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.