Do you really need an AI agent? Start with a state machine
Most business workflows already have a shape. Write that shape down as states and transitions, then give the model the two or three steps that actually need it.
Contents
The pull toward agents
A lot of AI projects now start with the same sentence: “we'll build an agent for it.” The appeal is easy to see. You describe the job, give a model some tools, and let it work out the steps. For a demo, that often works on the first afternoon.
Anthropic's engineering team draws a useful line here. In their write-up on agent design, workflows are “systems where LLMs and tools are orchestrated through predefined code paths,” while agents are systems where the model directs its own process and tool use. Their advice is to find “the simplest solution possible, and only increasing complexity when needed.” They also note that autonomy brings “higher costs, and the potential for compounding errors.”
We agree, and we would go a step further for business workflows. If the steps are known in advance, write them down as a state machine before you write a single prompt. The model then becomes a component inside a structure you control, rather than the thing deciding what the structure is.
Draw the workflow first
A state machine is a short list of named states and the moves allowed between them. It sounds old-fashioned, and it is. That is the point. Every state has a name you can show a customer. Every move has an owner: code, a model, or a person. Anything not on the list can't happen.
Our first application turns an ENT doctor's spoken consultation into a structured note. The interesting part is a single proposed finding, such as “right tympanic membrane: perforated.” Before that line can enter the record, it moves through these states:
| State | Who moves it on | What has to be true |
|---|---|---|
| Proposed | Model | A model has suggested a finding from the transcript |
| Evaluating | Code | The proposal is in the pipeline and nothing else may touch it |
| Rejected | Code | The finding isn't in the vocabulary, or has no source at all |
| Evidence attached | Code | A real finding object exists, with the speaker's own words |
| Hold | Code | The words are hedged or contradict the value, or something required is missing |
| Review required | Code | The words partly match. A clinician has to decide |
| Eligible | Code | The words support the value and nothing required is missing |
| Authorized | Person | A named clinician with authority accepted it |
Look at the middle column. The model proposes. Everything after that is ordinary code, and the last step is a person. You can read the whole decision in one function, and you can write a test for each row. We have one for most of them.
if support in (CONTRADICTS, INSUFFICIENT, DOES_NOT_SUPPORT, UNKNOWN):
decision = HOLD
elif support is PARTIAL:
decision = REVIEW_REQUIRED
elif not sufficient:
decision = HOLD # name the missing fact and ask for it
else:
decision = ELIGIBLE # a clinician may now accept itNothing in that block is clever. It is a table written as code. When a doctor asks why a finding didn't make it into the note, the answer is one of four branches, and each one comes with a reason code and an audit row.
Where the model goes
Once the states are written down, the question changes from “what should the agent do” to “which steps actually need a language model.” Leaving aside speech-to-text, our answer is one step, plus one we wrote and haven't switched on.
The first is extraction. Turning “right drum's got a small central perf” into a structured finding needs a model. Rules would drown in the ways doctors actually speak. The second is a model's second opinion on whether a quote supports a value. The code for it exists, but the app doesn't use it yet. If we turn it on, a deterministic check still runs first and always, and the model only gets a say when that check didn't already find a problem.
Everything else is code: checking the vocabulary, finding the quote in the transcript, spotting hedge words like “patient kept moving,” checking that the side was actually spoken, and deciding the state. These steps have one right answer. A model would give you that answer most of the time, slower and at a cost, and you would never be fully sure which time it didn't.
What the model cannot do
The model has no tool that writes to the record. Accepting a finding is a separate call, and it checks two things before it changes anything: the proposal is in a state that allows it, and the person making the call has authority.
def authorize(self, proposal, actor):
if proposal.status not in (ELIGIBLE, REVIEW_REQUIRED):
raise ValueError("only an eligible or review-required proposal may be accepted")
self.authority_engine.require(actor) # raises if this person can't sign
proposal.status = AUTHORIZEDCompare that with an agent that has an update_note tool. You can ask it nicely not to write unsupported findings, and it will usually listen. But “usually” is a property of the model on that day. The state machine makes it a property of the program. A held proposal can't be accepted, whatever any prompt says.
Reasoning does not create facts
One branch in our pipeline is worth calling out. When the deterministic check has already found the quote hedged or contradictory, the code skips the model's second opinion entirely. The comment above that line reads: “More reasoning cannot create a missing clinical fact.”
This is the habit agents find hardest. Faced with a gap, an agent tends to reason its way across it. If the doctor never said which ear, a model can often guess well from context. But a good guess written into a medical record is still a guess, and the next reader will treat it as an observation. The state machine handles this the boring way. It holds the step and asks the doctor which side.
What this costs you
A state machine is not free. You have to know your workflow well enough to list its states, and that takes real conversations with the people who do the work. When a new case appears, someone has to change code rather than a prompt, and then update the tests that pin each branch.
Deterministic checks are also blunt. Our hedge detector is a list of phrases. It catches “can't really tell” and misses phrasings nobody has added yet. We accept that, because a miss on that list sends a finding to the next check rather than straight into the record. But it is a real limit, and we name it in the interface as a hedge detector, not a clinical verifier.
When you do need an agent
Agents make sense when you genuinely can't list the steps in advance. Research tasks, open-ended coding, or exploring a messy data source are good examples. The path depends on what the model finds along the way, and a fixed graph would either be huge or wrong.
Even then, the parts with consequences can stay fixed. An agent can explore freely and still hand its result to a state machine that decides whether anything gets written, sent or paid. That split, open-ended work inside a fixed boundary, is the design we keep coming back to.
Before you build an agent
- Can someone who does this job today list its steps? If yes, write them as states.
- For each step, does it need reading or writing language? If not, use code.
- Which step changes something other people will rely on? Make sure no model owns it.
- What should happen when information is missing? Make that a named state, not a prompt.
- Can you explain any outcome by pointing at one branch? If not, the design is too loose.
References
- Anthropic. Building effective agents. Engineering blog, 19 December 2024. www.anthropic.com/engineering/building-effective-agents
About the author
Written by the team building ENT Clinical OS. Code excerpts come from that repository, which is private for now. Where we have not measured something, the article says so.