Payment intent retry loop detection, scored
TL;DR: key takeaways
- A retry loop is scored on earlier identical payments in a five-minute window, so the first payment in a loop never counts itself.
- The points scale with the number of repeats up to a cap, and are discounted when the repeats were denied rather than settled.
- On its own, a loop of settled repeats in the worked run scored 28 and was allowed. It is one signal among several, not a verdict.
- The fix for most loops is at the client: a persisted idempotency key and a capped, jittered retry. The check is the backstop.
Payment intent retry loop detection starts with a failure that looks like nothing went wrong. The agent calls a paid API, the response is lost to a timeout, the client tries again, and the merchant charges again. Each request is individually sensible. The total is not. This post explains how a pre-payment check can recognise that shape, what it scores, and, using real output from Spend7's engine, what the score does and does not decide.
It is the detection side of a problem that is mostly solved at the client. Both matter, and the second is the cheaper one to fix.
What a retry loop looks like to a check
A payment intent reaches the check with an agent identifier, a merchant, an amount in minor units and a currency. On its own, the intent says nothing about whether the agent has sent it before. The check has to be given the agent's recent payments as context, and the decision depends on that context.
Spend7's engine defines a retry loop narrowly. Two earlier payments are identical to the current one when they share the same agent, the same merchant and the exact same amount, and were created within the last five minutes. The window is burstMinutes: 5 in the ruleset. The match is deliberately strict. A loop that changes the amount by a cent is a different problem, and it usually shows up in the amount-anomaly signal instead.
Two details about the counting matter when you read the output.
First, the engine counts earlier payments in the history it was given. It does not count the intent being checked. So the threshold of three means three earlier repeats, which makes four attempts in total before the signal fires.
Second, only payments inside the window count. The history can hold a month of payments, and the loop signal looks at the last five minutes of them. A sixth repeat that happened six minutes ago does not count, and the worked run below shows that edge.
How the points are computed
The retry-loop signal has a maximum weight of 30 points in the ruleset. The points are scaled by the number of repeats, up to twice the trigger count, and discounted when none of the repeats settled:
| Earlier identical repeats in window | Settled (not denied) | Retry-loop points | Total score | Decision |
|---|---|---|---|---|
| 2 | 2 | none (below the trigger of 3) | 3 | allow |
| 3 | 3 | 15 | 18 | allow |
| 5 (a sixth was outside the window) | 5 | 25 | 28 | allow |
| 5 (all denied) | 0 | 15, after the 0.6 discount | 43, with 25 from denial history | allow |
The formula is 30 × min(1, repeats ÷ 6), multiplied by 0.6 when no repeat settled. That is why the denied row shows 15 rather than 25. The engine treats a loop of denials as less likely to have moved money, so it scores it lower, and it still records that the loop happened.
The total score is the sum of every signal that fired, so the rows above include the small background signals too. The two-repeat row scores 3 from a category prior alone. The loop contributes nothing until the third repeat.
A worked run
The runs below used a single agent, a $19.00 payment to api.example.com, and an earlier history of twenty settled payments to a different merchant spaced three hours apart. The only thing that changed was the repeats. The engine returned these details, copied from its output:
For three earlier repeats, the retry-loop detail reads: "3 intents for exactly $19.00 to api.example.com in the last 5 minutes, 3 of which were not denied." The score is 18 and the decision is allow. The loop signal contributed 15 points, and a small category prior contributed 3.
For six earlier repeats, one a minute apart, the detail reads: "5 intents for exactly $19.00 to api.example.com in the last 5 minutes, 5 of which were not denied." Five of the six fell inside the window, because the sixth was six minutes old. The loop contributed 25 points and the total was 28. The decision was allow.
For six earlier repeats that were all denied, the loop again counted five in the window, but none had been allowed. The loop contributed 15 points. A separate merchant denial history signal added 25, because all six of the account's attempts to that merchant had been denied. The total was 43, and the decision was still allow.
Read those three results together and two things follow. A loop of settled repeats, with nothing else wrong, is allowed. And the loop is one contributor to a score, not a verdict on its own.
Why a loop alone is allowed
The decision thresholds are 50 for flag and 80 for deny. A retry loop has a maximum of 30 points, so on its own it can never reach deny, and it can reach flag only in combination with other signals. The worked run above did not reach flag.
That is intentional, though it can surprise a team that expected a loop to stop a payment. A repeat of the same amount to the same merchant is also what a legitimate subscription, a metered API call and a second attempt after a genuine failure look like. The check holds a prior about the shape, not proof of intent. Denying every repeat would stop real work, and the ruleset does not do that.
What raises the score is company. A burst of more intents than twelve in the same window adds the velocity signal. A merchant the agent has never paid before adds first-seen merchant points. A large amount against the agent's own baseline adds the amount signal. This post has not tested the combined case with a velocity burst, so it does not state the total for that combination. Run your own history through the check to see it.
If a loop must stop, the stop belongs in a rule you set rather than in the default score. A per-payment ceiling is the cleanest one: it caps the damage of each repeat regardless of how the others scored. The AI agent budget post works through how a ceiling and a rolling cap interact.
Stop the loop where it starts
The check sees a loop after the first few repeats have happened. The client is the place to prevent it. Two standard practices do most of the work.
Stripe's documentation for idempotent requests says that a create or update can be repeated after a connection error without creating a second object, as long as the same key is sent. Its guidance for retries across process restarts is to generate and persist a key for the operation and supply it to every attempt. The keys apply to POST requests, and Stripe keeps them for at least 24 hours. If your agent calls a paid endpoint that accepts an idempotency key, persist one key per logical payment and reuse it on every retry. The loop then collapses into one payment at the merchant, and the check never sees the repeats.
The AWS Builders Library article on timeouts, retries and backoff with jitter makes the same point from the other side. Retries are selfish, because each one spends capacity the server needs to recover, and a retry without backoff creates retry waves. The guidance is to retry only errors a retry can fix, to confirm the operation is idempotent first, to cap the number of attempts, and to add jitter to the delay. The AWS Well-Architected reliability guidance on retries says the same thing.
Both sources point at the same design. A payment should be retried only when the client can prove the repeat is the same payment. Without that proof, the safe default is to stop and surface the failure, not to try again.
What the check cannot tell you
The check scores payments that reach it. A loop that bypasses the check is invisible to it, and a payment sent from a second client that never registered its history is not in the count. The score is only as good as the history it is given, which is one reason to route every payment through the same path.
The check also cannot know the merchant's side. If the merchant processed the first attempt and dropped the response, the agent's history records one payment and the merchant records one charge, and the loop is real but the check sees one intent. Reconciliation against the merchant's own records is the only way to catch that.
Finally, a loop score is a probability-shaped judgement with fixed weights. It is explainable, because each signal names its inputs, and every decision records the ruleset version and a hash of its constants. It is not a guarantee of fraud prevention, and a post that claimed it was would be overselling a pre-payment decision.
Testing it yourself
The check accepts a history as context, so you can test the detector without storing anything. Build a small known history: three identical intents two minutes apart, then a fourth. Confirm that the loop signal fires with the points in the table above, and that a history of two repeats does not. Then add a sixth repeat six minutes old and confirm that it is ignored.
Do this before you rely on the signal, and keep the test history in your repository. A detector you have not run against a known loop is a guess about the agent's behaviour, and a loop is exactly the case where a guess is most expensive. The API reference has the request shape, including the context object that carries history.
For the wider picture of what a budget should do when the agent is wrong, read the overspend prevention post. For how the check behaves when there is no history at all, which is a different and equally quiet failure, read the payment history cold-start post.
Frequently asked questions
- How do I detect a retry loop in agent payments?
- Match each payment intent on the agent, the merchant and the exact amount, then count earlier matches inside a short window. Three earlier identical intents in five minutes is the threshold in Spend7's ruleset. Score the count, weight repeats that settled above repeats that were denied, and record both numbers so the decision can be reviewed.
- How many repeated payments make a retry loop?
- In Spend7's ruleset, three earlier identical intents in the five-minute window is the trigger. The points rise with the count and reach their maximum at twelve, the count that would make the signal saturate. Two earlier repeats score nothing from this signal, which is deliberate: two identical payments in a few minutes are often a legitimate second attempt.
- Why does an AI agent pay the same invoice twice?
- Usually because the client retried a request whose first attempt succeeded but whose response was lost, and it had no idempotency key tying the attempts together. The second payment is a new object at the merchant. Fix it in the client by persisting one key per logical payment and reusing it on every retry, as Stripe describes for its own API.
- Does the risk check stop a retry loop?
- Not by itself. The check scores a payment and returns allow, flag or deny. A loop of settled repeats can still be allowed when nothing else is wrong, which is why the per-payment ceiling and the rolling cap in a budget matter as the backstop.
Score a payment before it settles
Spend7 returns allow, flag or deny in one call, with the signals that produced it. The free tier covers a single agent, its spend caps and its full decision log.