The PRG Pattern for AI Agents

AI agents are repeating a 25-year-old mistake.

In the 90s, web forms had a major bug. A user would place an order, hit refresh, and the browser would send the order again. This led to two charges and one angry customer.

The fix was the Post/Redirect/Get (PRG) pattern. It works like this:

  • User sends a POST request to submit data.
  • Server processes the data.
  • Server sends a redirect to a new URL.
  • Browser performs a GET request to show the result.

If the user hits refresh, they only replay the GET request. The dangerous POST request is gone.

AI agents have brought this bug back.

When an agent calls a tool to charge a card or create a record, the network might drop. The agent does not know if the action worked. It tries again. Now the customer has two charges.

You can fix this by using idempotency keys. An idempotency key acts like a redirect. It separates "doing the thing" from "showing the result."

The pattern for agents should look like this:

  • The agent generates a unique key before the first attempt.
  • It uses a stable value like a user ID and a cart ID.
  • It sends this key with every tool call.
  • The server checks the key. If it sees the key again, it returns the previous result instead of running the action twice.

For long tasks, you need more than just a key. You need checkpointing. You must save the state at every step. This ensures a restart picks up where it left off.

Follow these rules for any agent tool that changes data:

  • Every mutating tool must accept an idempotency key.
  • Derive the key from user intent, not a timestamp.
  • Generate the key before the first attempt.
  • Reuse the same key on every retry.
  • For long tasks, checkpoint every intermediate step.

The layer changes, but the logic stays the same.

Source: https://dev.to/ravikiran438/the-prg-pattern-for-ai-agents-a-25-year-old-fix-coming-of-age-in-a-new-era-23fh

Optional learning community: https://t.me/GyaanSetuAi