← Lesson
State Machines for Agents
Joeven
Run
Reset
Python loads on first run
ALLOWED = { "gather": ["search", "get_ticket", "handoff"], "apply": ["refund", "handoff"], "done": [], } GUARDS = { ("gather", "get_ticket"): lambda obs: obs.get("status") == "open", ("apply", "refund"): lambda obs: obs.get("amount", 999) <= 50, } def transition(state, tool, obs): if tool not in ALLOWED[state]: return state, "illegal tool " + tool key = (state, tool) if key in GUARDS and not GUARDS[key](obs): return state, "guard failed" nxt = {"get_ticket": "apply", "refund": "done", "search": "gather", "handoff": state} return nxt.get(tool, state), "ok" print(transition("gather", "refund", {})) print(transition("gather", "get_ticket", {"status": "closed"})) print(transition("gather", "get_ticket", {"status": "open"})) print(transition("apply", "refund", {"amount": 12})) print(transition("apply", "refund", {"amount": 80}))
Run to execute this in your browser. Nothing is sent to a server.