Skip to main content

Reza Neyzan

Making AI-Triggered Paid Actions Safe: Leases, Fencing and At-Most-Once Execution

Isometric illustration of an AI operations hub where tasks flow between an AI assistant, a human reviewer's desk, servers and a payment system

When an AI assistant only writes text, a mistake costs a revision. When it can trigger something that costs money, a duplicated or half-finished action becomes a real problem.

This is the case study of how I made paid AI actions safe inside Neyzan, my private operations hub, where two chat AIs (ChatGPT and Claude) draft and review content for my brands. The specific action was paid image generation for website articles, but the pattern applies to any AI agent that can spend money, change records or call external systems.

The problem: AI agents behave like unreliable distributed workers

Neyzan’s Content Studio prepares a website article end to end: text, SEO, images, an independent review by the second AI, my approval, then a WordPress draft. Images come from a paid image API through a server-side key, and both AIs work through Neyzan’s MCP tools from ordinary chat sessions.

That setup has the failure modes of a distributed system, without any of its machinery:

  • A chat session can end mid-task: usage limits, a closed tab, a lost connection.
  • A tool call can time out after the provider has already charged for the image, so the AI cannot tell whether it worked.
  • Two AIs, or two chats of the same AI, can pick up the same job.
  • An AI can report that a step is done when it is not.

Before this work, the state of a job lived in chat history. If a session died after a paid call, nobody knew reliably whether to retry, and a retry could pay twice.

The constraints

The solution had to fit how I actually run Neyzan:

  • No additional text-model API worker spend. I use existing ChatGPT and Claude subscriptions through MCP, without introducing paid autonomous model workers or extra model subscriptions. Paid image-generation calls are separately budgeted and explicitly approved. A task therefore runs only when an AI chat claims it; nothing executes unattended.
  • I approve the money. Every paid task carries a cost ceiling that I confirm when I create it, on top of a monthly cap.
  • Evolve, don’t rewrite. The existing Node/Express cockpit and PostgreSQL database, behind a feature flag, without breaking the legacy Studio paths.
  • Any AI can continue another’s work. If one AI hits its limit, the other must resume from the recorded state, not from memory.

The design

Durable task records. I create tasks from Content Studio: a parent “prepare images” task and one child task per image slot. Each task is bound to the exact article revision (by content hash) and to the exact reference image (by SHA-256). If either changes, the task is cancelled as stale instead of producing images for content that no longer exists.

Claims, leases and fencing. An AI claims a task and receives a lease token valid for about 20 minutes. Every checkpoint renews it. A paid call, checkpoint or completion presented with an expired or replaced token is refused. That is the fencing rule: a session that silently died cannot come back and act on a task someone else has since claimed.

At most one internally dispatched paid call per task attempt. Before calling the image API, the server derives an operation key from the task, the slot, the reference image, the image parameters and a hash of the instruction text, plus the attempt number. If a job with that key exists, it is reused rather than dispatching the same request again. This prevents automatic duplicate requests from this system; it cannot guarantee exactly-once billing or execution by the external provider. Changing the instruction within an attempt is refused: a different instruction is a new paid attempt, which needs my Retry.

Unknown is not failed. If the provider times out or returns a server error, the task stops in an outcome-unknown state, the cost is booked at the estimate, and nothing is retried automatically. A person reconciles it against provider usage before any retry. This is the one place where I deliberately chose a stuck task over a possible double charge.

Money is reserved before it is spent. The estimated cost is reserved against the task ceiling and the monthly cap inside the same database transaction that records the job. If the reservation does not fit, the call never happens.

Completion is proven, not reported. An AI cannot simply mark a task complete. The server checks the proof: an accepted image rendition produced by this task’s own job, from the selected reference, still accepted, and, for the parent task, a new article revision that actually contains those images.

Flow diagram of a paid AI task moving through claim, checkpoints, one paid call, review and verified completion
Each paid task moves through claim, checkpoints, a single paid call, review and server-verified completion.

What independent review caught

Neyzan separates the AI that builds from the AI that independently reviews. I keep final approval for deploying changes, spending money and publishing. ChatGPT reviewed the actual code diff before deployment, and that review earned its place.

  • A deadlock waiting to happen. Selecting a reference image and reserving a paid call locked the same rows in opposite orders. Under concurrency, two sessions could block each other. The fix was one global lock order for every path, plus tests that reproduce the old deadlock and show the new code avoiding it.
  • Proof that was too loose. The first completion check could have accepted an image rendition that was not tied to the task’s own job and selected reference. It was tightened to exact bindings.
  • The instruction was not part of the operation identity. A changed prompt could have slipped through as the same operation. It is now part of the key.
  • Legacy paths had to stay inert. With the feature switched off, the old Studio paths now run no task queries at all.

At the reviewed fix commit, Claude reported 1,015 automated checks passing and none failing; ChatGPT independently inspected the corrections. This is evidence for that tested revision, not a guarantee against every future failure.

The rollout

I deployed in stages, each with its own approval:

  1. Deploy switched off. I backed up the code and database, applied one additive migration, deployed with the task feature disabled and verified that nothing changed for the existing workflows.
  2. Activate with conditions. I switched it on separately, limited to one low-stakes article, no concurrent untasked image work, every paid call at or below the ceiling I had confirmed, and a rule to switch it off again on any anomaly.
  3. Pilot on a real article. A JobIntaker career article that needed a cover and two inline images.

The pilot

The pilot ran four tasks: one parent and three image slots. Claude claimed each slot, recorded its instruction, made one paid image call per slot, built the final image files and reviewed each one visually before accepting it.

  • In this controlled pilot: 3 paid calls, $0.163 total, against a $0.75 ceiling. There were no retries or duplicate paid calls in the recorded pilot.
  • One rendition validation catch: the server’s focal-box check rejected an initial cover crop because its category badge overlapped the subject. Claude then reviewed the output and rebuilt the crop from the same paid master without another paid API call; the replacement passed validation.
  • Server-verified completion for all four tasks.

The article then went through ChatGPT’s independent review, my approval, a WordPress draft created by a user that cannot publish, my own click on Publish, and a final check through the WordPress API confirming the live post matched the approved revision.

Illustration of a person at a desk reviewing an approval checklist on screen before an automated step continues
Human approval stays in the loop: every output is reviewed before the article moves on.

What I would tell anyone giving AI agents real actions

  • Design for the agent disappearing. Durable state, leases and fencing are not overkill when the worker is a chat session.
  • Put intent in the idempotency key. The instruction, the inputs and the attempt number together define the operation.
  • Treat unknown as its own state. Automatic retries are how unknown outcomes become double charges.
  • Verify completion on the server. An agent’s own report is not proof.
  • Ship dark, then activate deliberately. A feature flag and a separate activation decision kept every step reversible.
  • Separate building, independent review and human approval. The opposing-lock-order defect was caught during independent review and reproduced by targeted tests. I kept final approval.

I also decided what not to build yet. Server-side AI workers that run without a chat session would make the system faster, but they would add API costs and an unattended execution path. For a one-person operation, an explicit claim by an AI I am working with is the better trade for now.

You may also like these: