libpetri

Model concurrent, time-aware workflows as Petri nets, run them, and check them before they ship.
libpetri is a Coloured Time Petri Net engine for Java, TypeScript, Rust and Python. You describe a workflow as
places that hold typed tokens and transitions that do the work. The runtime executes that net, the exporter
draws it, and the verifier proves properties of it or returns the run that breaks them.
The net above is one turn of an LLM agent. The rest of this page reads it, runs it, checks it, and composes it
into larger nets. Every diagram, verdict and trace on this page is generated from code by
scripts/readme-images.ts.
The problem
An agent turn looks simple: ask the model, maybe call a tool, feed the result back, answer. In code the
coordination spreads across callbacks, retry counters, time-outs and error handlers. The failures show up late.
A turn waits forever for a permit that was never returned. A tool result arrives after its turn gave up. A
retry loop has no bound. libpetri makes that coordination the program: each state a turn can be in is a place,
each step is a transition, and the arcs say exactly what a step needs, what it blocks on and what it produces.
An agent turn as a net
Circles are places, boxes are transitions. A token in thinking enables call-model. Its action asks the model
and puts the turn into either toolCall or answer (an XOR output). run-tool needs a tool call and one
permit from toolBudget. If the tool does not answer within 10 seconds, the timeout branch sends the turn back
to thinking. give-up fires only while toolBudget is empty (an inhibitor arc) and moves the turn to
failed.
import {
PetriNet, Transition, forwardInput, one, outPlace, place, timeout, transformFrom, xor,
} from 'libpetri';
type Turn = { question: string; notes: string[] };
type Reply = { tool: string } | { answer: string };
const request = place<string>('request');
const thinking = place<Turn>('thinking');
const toolCall = place<Turn>('toolCall');
const toolResult = place<{ turn: Turn; result: string }>('toolResult');
const toolBudget = place<null>('toolBudget'); // k permits, never refunded
const answer = place<string>('answer');
const failed = place<Turn>('failed');