Petri-Pilot now has a new tool: code-to-flow. Paste source code in any language, and it produces a validated Petri net model — places, transitions, arcs — ready for visualization, ODE simulation, and code generation.
The idea is simple: Claude reads your code, identifies the states and actions, and outputs a formal Petri net. Not a diagram — a model that can be executed, analyzed, and verified.
Claude analyzes the code's structure with a low-temperature prompt tuned for Petri net extraction. The system prompt enforces strict rules: every place must connect to at least one transition, every transition needs input and output arcs, and the model must be minimal — capturing essence, not implementation detail.
The output is validated structurally (no dangling nodes, proper arc connectivity) and behaviorally (reachability, deadlock detection, liveness). If something's off, we get warnings and errors rather than a broken model.
Different code calls for different lenses. Code-to-flow offers four focus modes:
Auto-detect examines the code and picks the best fit. Or we can force a specific lens to see the same code from different angles.
Consider a simple order handler:
func processOrder(order Order) error {
if err := validate(order); err != nil {
return err
}
charge(order.Payment)
ship(order.Address)
notify(order.Customer)
return nil
}
Code-to-flow extracts a state machine:
pending, validated, charged, shipped, completed, failedvalidate, charge, ship, notifyThe result is a Petri net we can simulate. ODE analysis shows flow rates through the pipeline. We can check for deadlocks (what if charge fails but we still try to ship?). We can generate a full application from the model.
We could use pflow.xyz to draw the net manually. Code-to-flow solves a different problem: existing code. Most systems already have state machines buried in conditionals, switch statements, and handler chains. Extracting them by hand is tedious.
The LLM is good at this kind of structural pattern recognition. It reads the code as a human would — identifying what the states are, what triggers transitions between them, what resources get consumed — and produces a model that makes the implicit explicit.
This also works as a comprehension tool. Paste unfamiliar code, get back a Petri net that shows the flow. The visual model is often easier to reason about than the source.
Web UI — Visit pilot.pflow.xyz/code-to-flow/ and paste code directly. Two-panel layout: code on the left, model on the right. Copy the JSON or open it in the visual editor.
HTTP API — POST /api/code-to-flow with {"code": "...", "language": "go", "focus": "state-machine"}. Returns the full model with validation results and a preview URL.
MCP Tool — The petri_code_to_flow tool is available for Claude Desktop, Cursor, and other MCP clients. Ask Claude to analyze code and it calls the tool directly, returning a Petri net in conversation.
Code-to-flow closes a loop in the pflow ecosystem. Previously the pipeline was:
design model → generate code → deploy
Now it's:
existing code → extract model → analyze → improve → regenerate
We can take legacy systems, extract their state machines as Petri nets, prove properties about them (bounded? live? deadlock-free?), and then generate clean implementations from the verified models.
The model remains the source of truth. Claude is the bridge between informal code and formal nets.
Try it: pilot.pflow.xyz/code-to-flow/
See also: Introducing Petri-Pilot for the full tutorial platform, and Declarative Differential Models for the ODE analysis theory.