AgenticFSM
Entity lifecycles declared once and advanced the same way by code, a model or an agent.
An order, a ticket or an application sits in a state for days and is moved along by whatever knows something new about it. AgenticFSM keeps the allowed moves in one list, records every move in an append-only log, and raises on a move the list doesn't have.
Install
pip install agentic-fsm # the library; brings Pydantic AI for steps a model decides
pip install "agentic-fsm[django]" # with the Django state backend
A machine
States are declared like Django model fields. Each one carries its definition.
from agentic_fsm import States, State, InitialState, FinalState
class Order(States):
pending = InitialState("Placed, not paid yet")
paid = State("Payment cleared, waiting to ship")
shipped = State("Handed to the carrier")
delivered = FinalState("The carrier delivered it")
cancelled = FinalState("Cancelled before it shipped")
A step is a function of the entity. It returns the next state, or None for "not now".
def cancel(order):
return Order.cancelled if order.cancel_requested else None
def payment_cleared(order):
return Order.paid if order.payment.cleared else None
def ship(order):
return Order.shipped if order.tracking_number else None
def carrier_delivered(order):
return Order.delivered if order.carrier_status == "delivered" else None
One list holds the whole graph, one row per edge.
from agentic_fsm import Machine
TRANSITIONS = [
# from to step
((Order.pending, Order.paid), Order.cancelled, cancel),
(Order.pending, Order.paid, payment_cleared),
(Order.paid, Order.shipped, ship),
(Order.shipped, Order.delivered, carrier_delivered),
]
orders = Machine(Order, TRANSITIONS, load=get_order)
load(id) fetches the entity from your own tables. The library stores the state, never the
entity. Ids are stored as text, so load receives a string.
Machine(...) checks the declaration and raises a MachineError naming the state or the row:
exactly one InitialState, every state reachable from it, no rows leaving a FinalState, no
other state without a way out, every row's states belonging to the machine, every step a named
function or a Decide, every Decide deciding among this machine's states, no two steps sharing a
name.
A step a model decides
A Decide is a step that puts the question to a model. It has a name, a prompt function and a
schema: a Decision whose to is typed as your states, plus any fields you want logged with
the move.
from agentic_fsm import Decide, Decision
class MessageRead(Decision):
to: Order
reason: str
read_message = Decide(
name="read_message",
prompt=lambda order: order.message and f"Where does this message put the order?\n{order.message}",
schema=MessageRead,
)
TRANSITIONS = [
...
(Order.pending, Order.cancelled, read_message),
(Order.paid, Order.cancelled, read_message),
]
- The model is offered only the step's rows out of the order's state, each with its description, so the prompt never has to explain what a state means.
- A prompt that returns nothing means "not now": no call is made and the order stays.
- The whole
Decisionis logged with the move, and the move'sdecided_byisllm:<model>.
For tools, another model, retries or output validators, declare a
Pydantic AI Agent and pass it as agent= in place of schema=.
Its output type is the schema, and the entity is passed as deps, so each tool reads it from
ctx.deps.
from pydantic_ai import Agent
reader = Agent(
"anthropic:claude-haiku-5-5",
deps_type=OrderRow,
output_type=MessageRead,
instructions="You read messages from a shop's customers.",
tools=[past_orders],
)
read_message = Decide(name="read_message", agent=reader, prompt=...)
A function step may also return a Decision in place of a state, for code that calls
read_message.run(order) itself or decides by rule and wants its reason logged.
A session in the model's place
A coding agent or a person can decide a Decide step with no model key set.
brief = orders.brief(42, "read_message")
# brief.instructions, brief.prompt, brief.choices, brief.schema, brief.tools
orders.fire(42, "read_message", answer={"to": "cancelled", "reason": "They asked for a refund"})
answer= takes the fields as a dict or a JSON object, or a bare state value when the schema is
a plain Decision. The prompt function doesn't run again, and the move's decided_by is
agent. An answer the schema or the step's rows don't allow raises TransitionError with the
reason; nothing is written, and the session answers again.
Moving entities
| Call | Does |
|---|---|
orders.start(42) |
puts an entity in the initial state |
orders.fire(42, payment_cleared) |
runs one step on one entity |
orders.fire(42, payment_cleared, entity=order) |
the same, on the order already in hand instead of load(42) |
orders.fire(42, "read_message", answer=...) |
decides a Decide step in the model's place |
orders.brief(42, "read_message") |
what a Decide step would give the model |
orders.progress(42) |
moves one entity until it rests |
orders.progress_all() |
tries the steps out of each unfinished entity's state: at most one move each |
orders.state_of(42) |
the entity's current state |
orders.in_state(Order.paid) |
the ids of the entities in a state |
orders.history(42) |
every move logged for the entity |
orders.graph() |
the states and rows as data, for review |
- Every row in the log is a move that happened. A step's targets are the rows it sits on:
if
payment_clearedreturns anything butOrder.paid, that's a bug,fireraisesTransitionError, and nothing is written. Whatever a step can meet (a bounce, a failed payment) is a state with its own row. firereturns the loggedMove, orNonewhen the step said "not now".progress_alltries steps in list order and stops at the first that doesn't returnNone, so each entity makes at most one move per call. When to call it is up to you: a cron job, a scheduler, a script.progressgoes on until the entity rests: a final state, or one whose steps all say "not now". It loads the entity afresh after each move, commits each move, and stops after a move that brings the entity back to a state it already left, so it never loops.- Two callers can't both move an entity.
firelocks the entity from the read of its state to the write of the move; a second caller waits, then reads the state the first left. With the Django backend the step's own writes commit with the move. - A step with an outside effect should be safe to run twice. If it sends an email and the process dies before the move commits, the next call runs it again.
At the command line
agfsm makes the same moves from a shell, so a coding agent or a script can do every move your
code can and no other. Each command calls one of the methods above.
| Command | Calls |
|---|---|
agfsm show [--mermaid] |
orders.graph(): the states with their descriptions, then the rows |
agfsm start order:42 |
orders.start(42) |
agfsm list paid |
orders.in_state(Order.paid) |
agfsm fire order:42 payment_cleared |
orders.fire(42, "payment_cleared") |
agfsm fire order:42 read_message --agent |
orders.brief(42, "read_message") |
agfsm fire order:42 read_message --answer '{...}' |
orders.fire(42, "read_message", answer=...) |
agfsm progress order:42 |
orders.progress(42) |
agfsm progress_all |
orders.progress_all() |
- The machine is named as
--machine package.module:name(a module in the working directory works), or by theMACHINEsetting:export AGENTIC_FSM_MACHINE=shop.lifecycle:orders. - An entity is
<machine name>:<id>, and the name must be the loaded machine's. --jsonprints the result as data: theMove,BrieforGraph, with times as ISO text.- The exit code is 0 for a move made or "not now", 2 for one that can't be made (an unknown step, an entity not started, an answer that doesn't validate, no machine), with the reason on stderr.
- With
DJANGO_SETTINGS_MODULEset,agfsmsets the Django project up before it loads the machine, asmanage.pywould.
--agent then --answer lets a session decide a Decide step with no model key: the first prints the
instructions, the prompt, the choices and the answer's schema; the second applies the answer.
State backends
Where state is kept is a backend. The core imports no database; each backend is its own package, chosen by its dotted path, the way Django chooses a database engine.
export AGENTIC_FSM_BACKEND=agentic_fsm.backends.django
or Machine(..., backend="agentic_fsm.backends.django"). A machine with neither raises.
| Backend | Keeps state in | Needs |
|---|---|---|
agentic_fsm.backends.django |
a Django project's database | agentic-fsm[django] |
agentic_fsm.backends.locmem |
this process's memory, for tests | nothing |
For the Django backend, add its app and migrate:
INSTALLED_APPS = [..., "agentic_fsm.backends.django"]
It writes through Django's own connection, so wrapping fire in transaction.atomic() commits
your rows and the move together.
A backend of your own is a package whose base module has a StateBackend subclassing
agentic_fsm.BaseStateBackend.
Settings
Every default is in agentic_fsm/conf/global_settings.py, the way Django keeps its own.
| Setting | Default | Is |
|---|---|---|
BACKEND |
None |
the state backend of a machine built without backend= |
DECIDE_MODEL |
"anthropic:claude-haiku-5-5" |
the model of a Decide built without agent= |
MACHINE |
None |
the machine agfsm loads without --machine, as package.module:name |
A setting takes the first value found in settings.configure(BACKEND=...), the environment
variable AGENTIC_FSM_<NAME>, the module named by AGENTIC_FSM_SETTINGS_MODULE, then the
default.
from agentic_fsm import settings
settings.configure(BACKEND="agentic_fsm.backends.django")
Development
uv venv && uv pip install -e ".[dev]"
pytest
Licence
Apache-2.0. See LICENCE.md.
Metadata
Release files for agentic-fsm 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| agentic_fsm-0.1.7.tar.gz | 39.7 kB | With provenance | Details for agentic_fsm-0.1.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| agentic_fsm-0.1.7-py3-none-any.whl | Python 3 | none | any | Details for agentic_fsm-0.1.7-py3-none-any.whl |
Total release size: 71.9 kB
Release files / agentic_fsm-0.1.7.tar.gz
| Download URL | agentic_fsm-0.1.7.tar.gz |
|---|---|
| Size | 39.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2eac2008d1ccff166e826d9931ae5fc57bd13d1ef62ba80b650a02db241b3d65
|
|
BLAKE2b-256 checksum How to use checksums |
f6bc6c97cadd680003809fcdfed83a11eb8934a5e4e71d376a391e8ba5783964
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.
Transparency logRelease files / agentic_fsm-0.1.7-py3-none-any.whl
| Download URL | agentic_fsm-0.1.7-py3-none-any.whl |
|---|---|
| Size | 32.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c8a801b0784dc9dca39293d546e9d83beb947ed8b18a7c9e0154ca8d7ad6cb12
|
|
BLAKE2b-256 checksum How to use checksums |
7fcd07fe24fe98b87dbc40cbcc0f79e354fe03db7e696b4b72c53390837f5c43
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.
Transparency logSecurity for agentic-fsm 0.1.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for agentic_fsm-0.1.7.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.