Unit 06.02: Combining a flow with a crew
The useful architecture is a flow that owns the sequence and a crew that owns only the steps needing judgement.
Flow outside, crew inside
@start() and @listen() define the order in code. The flow's state is a Pydantic model, so the fields are typed and visible.
The code builds a two-step flow where routing is deterministic and only the second step would call a crew.
import contextlib
import io
from crewai.flow.flow import Flow, listen, start
from pydantic import BaseModel
class State(BaseModel):
ticket: str = ""
team: str = ""
draft: str = ""
class SupportFlow(Flow[State]):
@start()
def route(self):
"""Deterministic: no model, no tokens."""
self.state.team = "billing" if "charge" in self.state.ticket else "general"
return self.state.team
@listen(route)
def handle(self, team):
"""Where a Crew would be invoked -- one model step, not five."""
self.state.draft = f"[{team}] reply drafted for: {self.state.ticket}"
return self.state.draft
flow = SupportFlow()
# kickoff prints large decorative banners; redirect them so the lesson output
# stays readable. You will see them when you run this yourself.
with contextlib.redirect_stdout(io.StringIO()):
result = flow.kickoff(inputs={"ticket": "I was charged twice"})
print("routed to :", flow.state.team)
print("result :", result)
# The flow owns the routing and the sequence. The crew owns only the step that
# needs judgement. That split is what makes a run reproducible.
kickoff() prints large decorative banners, so the example redirects stdout to keep the lesson output readable - you will see them when you run it yourself. That is worth knowing before you wire a flow into anything that parses its output.
The split is the point. Routing costs nothing and is identical every run. One step calls a model. A run's behaviour is therefore determined by the flow plus one uncertain step, rather than by five.
The mistake this prevents
The mistake is putting a crew at every step because the flow makes it easy. Each crew invocation is a model call and an error rate; the flow's value comes from how few of them it needs.
Takeaway
Let the flow own the sequence and the routing, and give the crew only the steps that need judgement. Fewer model steps means fewer places a run can differ from the last one.
