Let an agent propose a plan, then freeze it as a signed routing slip: validated once against a step registry, advanced one step at a time, compensated newest first.
Where the plan lives after the agent writes it
At Belgavi's Book Store the help-desk agent can propose a refund for a damaged delivery. For case CASE-4127 it proposes five steps: verify order ORD-5521, check the return window, reserve a refund of 42, notify the customer, issue the refund. Each step is owned by a different service. The question this guide answers is not whether the agent may propose the plan; it is where that plan is kept once it has been proposed, and who is allowed to change it.
There are two classic answers. A central orchestrator (a process manager, in the vocabulary of *Enterprise Integration Patterns*) keeps the plan and the state in one place and sends each service a command. A routing slip, the other pattern in the same book, attaches the itinerary to the message itself: each component does its step, then forwards the message to the next address on the slip. Hohpe and Woolf describe it for a sequence that is not known at design time and may vary for each message: a component at the start computes the list of steps and attaches it, and each step passes the message to the next one on the list.
An agent-proposed plan fits that description surprisingly well. The model decides the sequence once; afterwards nothing should be improvised. This guide shows how to make a routing slip safe enough to carry an agent's plan, what must stay deterministic, and when you should use an orchestrator or plain code instead. The companion code is a small Python implementation with tests.
Validate once, then freeze the itinerary
The agent's output is a proposal. Before anything runs, deterministic code checks it against a step registry: the list of steps that exist, the arguments each needs, which steps must come before it, and policy limits. In the example, reserve_refund requires verify_order and check_return_window earlier on the slip, refunds above 150 must go to a person, and no plan may exceed six steps.
| Check | Example failure | Why the model cannot be trusted with it |
|---|---|---|
| Step exists in the registry | delete_account | the model can name tools that are not offered |
| Prerequisites come first | issue_refund before reserve_refund | ordering is the whole point of the plan |
| Argument names match | amount missing | a step must not guess its inputs |
| Policy limits | refund of 900 | a limit enforced in a prompt is a suggestion |
| Length limit | 40 steps | a runaway plan is also a cost problem |
If any check fails, the plan goes back to the agent with the list of problems, or to a person. If all pass, the itinerary is signed. The example uses HMAC-SHA256 over canonical JSON (sorted keys, no whitespace), with a key that only the issuing service and the step processors hold. From this moment the itinerary is read-only.
def issue(itinerary, key=None):
problems = validate(itinerary)
if problems:
raise SlipError('; '.join(problems))
it = copy.deepcopy(itinerary)
sig = hmac.new(_key(key), _canonical(it), hashlib.sha256).hexdigest()
return {'itinerary': it, 'signature': sig, 'cursor': 0, 'results': [], 'status': 'running'}The signature is what makes a routing slip acceptable when one of its authors is a model. Without it, any hop that can edit the message can edit the plan: a step that was told to reserve 42 could forward a slip that issues 420, and the next service would comply. With it, a change to any argument or the order of any step fails verification at the next hop.
Each step may only advance the cursor
A step processor does four things, and only these four. It verifies the signature. It checks that the cursor points at its own step name. It runs its handler with the arguments on the slip. It appends its result and moves the cursor forward by one.
def process(slip, me, handler, key=None):
verify(slip, key)
if slip['status'] != 'running':
raise SlipError(f'slip is {slip["status"]}')
step = slip['itinerary'][slip['cursor']]
if step['step'] != me:
raise SlipError(f'{me} received the slip but the cursor points at {step["step"]}')
out = copy.deepcopy(slip)
...The cursor check stops a misrouted or replayed slip from running a later step early. In the example, sending the slip to issue_refund while the cursor is on notify_customer raises issue_refund received the slip but the cursor points at notify_customer. The results list is append-only, so the slip carries its own history: which steps ran, what they returned, and where it stopped.
Two details matter in practice. First, the processor returns a new slip and never edits the one it received, which makes retries safe to reason about: if forwarding fails, the step can re-send the same output. Second, each step must be idempotent on its own inputs, because a forward can be delivered twice. Key the side effect on the case and the step, for example CASE-4127:reserve_refund, so a duplicate delivery returns the original result.
When a step fails
A routing slip has no central coordinator to run a saga's compensations, so the slip itself must say how to undo. The registry records a compensating action for each step that changes something: reserve_refund is undone by release_refund, notify_customer by send_correction, issue_refund by reverse_refund. Read-only steps such as verify_order need none.
When a handler raises, the processor marks the slip failed, records the error, and stops forwarding. A small deterministic component, the slip's return address, receives failed slips and walks the completed results newest first. In the example, a failed notify_customer after three completed steps produces exactly one compensation, release_refund, because the two earlier steps had no side effects. If issue_refund fails after notify_customer succeeded, the plan is send_correction then release_refund.
This is the main weakness of the pattern. There is no single place to ask "which slips are stuck?" unless you build one. In production, each processor should also write the slip version it forwarded (case id, cursor, status, signature) to a durable store, so an operator can find slips that stopped moving and the return address can retry compensations that themselves failed.
What stays deterministic, and what the agent still does
| Part | Owner | Notes |
|---|---|---|
| Proposing the sequence and arguments | agent | the only part that needs a model |
| Registry, prerequisites, limits | code | versioned with the services |
| Signing and verification | code | one key per environment, rotated |
| Cursor movement and results | code | append-only, one step at a time |
| Compensation order | code | from the registry, newest first |
| Re-planning after a failure | agent, then code | a new slip, validated and signed again |
If the plan needs to change mid-flight, for example because the customer replies that the book was a gift, the agent writes a new itinerary. That new itinerary goes through validate and issue like the first one, and it carries a reference to the old slip so the return address can reconcile what already ran. An in-place edit is never accepted, because an edit is exactly what the signature exists to reject.
Migrating an existing flow
Most teams start with a hard-coded workflow and want the agent to choose among variants. A safe sequence:
- Write the registry first, from the steps the services already expose. Do not add steps the agent wants but no service owns.
- Have the agent propose slips in shadow, and run only
validateon them. Count validation failures by kind for a few weeks. - Turn on signing and the cursor check with the existing code path still choosing the sequence, so processors are tested before the model is.
- Let the agent's slips run for low-value cases, with the refund ceiling well below the policy limit.
- Add the durable slip log and an alert on slips with no progress for longer than the slowest step's timeout.
When a routing slip is the wrong choice
Use a central orchestrator or durable workflow when steps run in parallel and must be joined, when the plan needs long waits for people, or when you need one place to query and cancel running work. A slip is a linear itinerary; forcing branches into it produces nested slips that nobody can debug. Use plain code when there are only two or three possible sequences: a match on the case type is easier to review than a model choosing between them, and you do not need an agent at all.
The slip is the right fit when the sequence varies per case, every step is owned by a different service, and you want each service to make its own local decision with a clear, verifiable record of what it was asked to do.
Verifying it
The companion tests cover the properties that make the pattern safe: a valid plan runs to done; validation reports ordering, unknown steps and limit breaches; a step cannot skip ahead; changing any argument breaks the signature, as does verifying with another key; a failure stops the slip and returns the compensation plan newest first; processing never mutates the input slip; and a missing key is an error rather than an unsigned slip. Run them against your own registry before letting an agent propose anything, and keep a test that tries to raise an amount in transit, because that is the attack the signature exists to stop.