Exactly-Once: Your agent shouldn't pay the same invoice twice
Wrap the payment. It runs once across retries, crashes, resumes, and replays. exactly-once is a Python library that makes a side effect run a single time. Wrap the function that pays an invoice or sends an email, or submits a transaction and it executes once per key, then replays its stored result on every later call. Here is the whole integration: from exactly_once import once , Store , current_key store = Store . sqlite ( " effects.db " ) @once ( store , key = lambda inv , ** _ : f " pay: { inv . id } " ) def pay_invoice ( inv ): return payments . transfer ( inv . vendor , inv . amount , idempotency_key = current_key ()) Call pay_invoice(invoice) and it pays the vendor. Call it again from a retry, a resumed run, a replay, or a second worker and it returns the recorded result. The vendor is paid once. The crash it's built for An agent pays an invoice. The transfer reaches the provider and succeeds. The process dies in the moment between the provider's 200 OK and the line that records the result. The agent restarts and reaches the same step again. exactly-once writes a record the instant the agent enters the call. pay_invoice claims the key pay:{invoice.id} , and the store marks it IN_FLIGHT . When the result returns, the store marks it COMMITTED and saves that result. After the crash the record reads IN_FLIGHT with an empty result the library knows a payment started and holds no proof it finished. So it quarantines the key. The agent leaves that payment for a decision and moves on. You give @once a prober that asks the payments API whether a transfer with that idempotency key exists: the library commits the key when the provider confirms the payment, and releases it when the provider confirms none. Until an answer arrives, the held payment stays in the ledger where you can see it: store . list ( state = " in_flight " ) # every payment awaiting a verdict How the guarantee holds Three states, one atomic operation: FRESH ──claim──▶ IN_FLIGHT ──commit──▶ COMMITTED clai