Module 14 Lesson 3: Using LLMs as State Transitions
The bridge. How to use an LLM to classify user intent and trigger a specific state change in your FSM.
LLMs as Transitions: The Intent Trigger
Now we bring the two worlds together. If an FSM is the "Map," and the LLM is the "Driver," how does the driver tell the map where to go?
The answer is Intent Classification.
1. The Classifier Node
Instead of letting the LLM "Run a Tool," we let the LLM "Choose a State."
- User: "I'd like to return this shirt."
- LLM: Classifies the intent as
INIT_RETURN. - FSM: Validates that a return is allowed for the current user. If yes, it moves to the
RETURN_STARTEDstate.
2. Why this is 10x More Reliable
In a pure agent, the LLM might decide to:
- Call
check_refund_policy. - Call
search_email. - Call
generate_label. ...in any order it wants.
In a hybrid system, the LLM is restricted to a specific list of intent categories.
3. Visualizing the Hand-off
graph LR
Input[User: 'Cancel my sub'] --> Brain[LLM Classifier]
Brain -->|Intent: CANCEL| FSM[State Machine]
FSM -->|Current State: ACTIVE| NodeA[Process Cancellation]
FSM -->|Current State: ALREADY_CANCELLED| NodeB[Explain Status]
NodeA --> Result[Success Response]
4. Reducing Token Costs
Classifying an intent into 5 categories is much "Cheaper" than asking an agent to "Think" and "Plan."
- You can use a tiny model (Llama-3 8B or Phi-3) for the Classifier.
- You only use the big, expensive model for the Actual Content (writing the email).
5. Code Example: Triggering a transition
# 1. LLM output
intent = llm.call("Classify this message: 'I want to pay'. Categories: [LOGIN, PAY, LOGOUT]")
# 2. Logic check
if intent == "PAY":
try:
fsm_engine.trigger('pay') # This will fail if not logged in!
except Exception:
return "You must login before paying."
Key Takeaways
- The LLM acts as the dynamic interpreter of human language.
- The FSM acts as the deterministic guard of business rules.
- Intent Classification is the most robust way to trigger state changes.
- This pattern allows for smaller, faster models to handle the navigation.