Payments are where the case for agent automation and the fear of it meet. Paying invoices, issuing refunds and settling deposits are repetitive, rule-bound work. They are also actions where a small error costs real money and may be hard to take back.
No control makes an agent infallible, and “safely” here has a narrow meaning. A payment agent is safe enough when a wrong request is limited by rules, a large request needs a person, a failure leaves the books in a known state, and somebody can later prove what happened. This article goes through the controls that deliver that.
Start by removing the agent’s payment credentials
The first control is structural. The agent should not hold a key that can move money.
If it does, every other safeguard depends on the agent choosing to use it. A confirmation step in the agent’s code does not help when a bug or a manipulated instruction calls the payment API directly.
Put a gateway in front of the payment system, give the gateway a credential scoped to the operations it needs, and revoke the agent’s own access. The agent can then ask for a payment and cannot make one. The system of record sees the gateway’s service account as the actor, and the gateway’s record links each change to the agent, the user and the approver.
Make the payment a typed operation
An open-ended tool such as “call this API” gives a policy little to work with. A typed operation does. It names the action and fixes its fields: the amount as an integer in minor units, a currency from an allowed list, a reference to the order being paid.
Validation then becomes strict and boring. A quantity or amount of the wrong type is refused with the path of the field, and it is never coerced. A malformed request stops before anyone evaluates it.
Decide on live facts
Limits on a payment usually depend on something that changes: the remaining budget, the state of the invoice, whether the vendor is active. A rule checked against a cached copy can pass a payment that today’s numbers would refuse.
So read the facts from the system of record when the decision is made, and read them again immediately before the call. A plan may wait hours for an approver, and the budget can move in that time.
If a fact cannot be read, refuse. An unavailable budget service must not count as enough budget. The same applies when a policy fails to evaluate: that outcome is a denial.
Keep the decision itself deterministic. A model can propose a payment, but it should not be the thing that authorises it. Written rules can be tested, reviewed and replayed against past requests before a change goes live.
Hold the payments that need a person
A sensible starting policy lets small, routine, reversible actions through and holds anything large or irreversible.
Approval is only as good as its link to what runs. If an approver agrees to “pay the Northwind deposit” and the amount can change afterwards, the approval is worth little. Bind the decision to a hash of the exact plan: steps, inputs, requester and policy version. A decision carrying a different hash is refused.
Three more rules are worth having. The person who asked should not approve their own request. An agent should never be able to approve. And the approval should expire, so that an old request cannot be released by accident weeks later.
Never pay twice
Networks lose answers. The most expensive mistake in payment automation is retrying a call that already succeeded.
Each payment attempt needs an idempotency key that the payment system honours, so that a repeat returns the original result instead of creating a second charge. Where the system offers no such mechanism, the operation must not be repeated automatically after an ambiguous failure.
The agent needs the same protection one level up. If it resubmits a request with the same key, it should get the existing execution back. If it reuses the key with a different amount, it should be refused.
Treat a lost answer as its own state
A timeout after sending does not mean the payment failed. It means nobody knows. Three tempting reactions are all wrong in some cases:
- Assuming failure and rolling back can leave a real payment attached to a cancelled order.
- Assuming failure and retrying can pay twice.
- Assuming success can tell a supplier that money is on the way when it is not.
If the system offers a way to look the payment up, use it. If that gives no definite answer, stop. In Sefra the step becomes unknown, the execution enters needs_intervention, and, if the alert webhook is configured, an operator is alerted. Nothing is undone and nothing is repeated until a person has checked the system of record and settled the step.
This means a person is occasionally interrupted. We think that is the right price for not guessing about money.
Plan the way back before the first payment
Most payments sit inside a longer workflow, and a failure at the end leaves earlier steps in place. Decide in advance what undoes each step.
A card charge can often be reversed with a refund. A bank transfer may be final. So declare each step as either compensable or irreversible, and allow at most one irreversible step in a plan, in last position. If the final payment is refused, everything before it can still be undone. If a plan puts an irreversible step first, refuse it before it runs.
Keep evidence someone else can check
After a payment, a controller may need to show who approved it, under which rule, and what the system was told. Ordinary application logs prove only what the application says about itself.
A stronger record chains each event to the one before it by hash and is written before the call it permits. A separate verifier can then detect an edit. This has limits: someone with full control of the database could rewrite the whole chain, so keep a copy of the latest hash somewhere that person cannot reach.
What this does not solve
These controls do not make an agent’s judgement correct. If a rule permits a payment to the wrong supplier, that payment will execute. They do not stop invoice fraud that looks legitimate to the rules. And they do not replace your bank’s or payment provider’s own controls, which should stay in place.
Start narrow. Pick one payment workflow, run it in a test environment, and read every held request and failure case before widening it. Sefra’s bundle for Stripe is built for this stage and runs in test mode only. A pilot with us uses one workflow, in a test environment, with no production writes.