---
title: "Write endpoints an agent will call twice"
description: "Assume the agent repeats a create or a charge. Return the first object on the second call. Do not perform the write again."
canonical: "https://www.agentrail.co/blog/idempotent-api-for-ai-agents?h=9VVPH9X57WTWN00M"
author: "Harsh Raj"
published: "2026-11-12T12:09:00Z"
last_updated: "2026-11-24T07:24:19.960Z"
---

# Write endpoints an agent will call twice

An idempotent API for AI agents treats a repeated write as the same operation. Store the first result, return it on retry, and reject a reused key sent with different parameters.

An idempotent API for AI agents assumes the same write will arrive twice. The second call returns the first object. It must not create a second customer, charge, or ticket. Agents retry when a timeout hides the first response, when a harness retries, or when the model asks whether the write landed and sends it again.

How many of those repeats occurred stays on [the agent retry loop](https://www.agentrail.co/blog/agent-retry-loop?h=9VVPH9X57WTWN01J).

> **Key Takeaways**
>
> Idempotency for a write means one side effect and many deliveries of the same request.
>
> The caller generates the key. You store the first result and return it when the same parameters arrive again.
>
> The same key with different parameters is an error. Keep the first object, and do not apply the new parameters.
>
> Publish how long you keep the key. Stripe's 24-hour window applies to Stripe.

## The second identical write returns the first object

Idempotency for a write is the property that repeating the same request does not repeat the side effect.

The caller supplies the key, often in an `Idempotency-Key` header. Generate it before the first attempt, and send that same value on every retry. The harness has to keep the key across a timeout. If the agent mints a new key because the first call looked failed, you create a second object.

The second POST comes from a timeout that hid the 201, or from a harness retry without a parsed success. It also comes from a new step, when the agent doesn't trust the first result.

Anthropic's eval note separates the outcome in the environment from the transcript. A booking agent can say the flight is booked when no reservation row exists ([Demystifying evals for AI agents](https://www.anthropic.com/engineering/demystifying-evals-for-ai-agents), 9 January 2026). A second create, sent because the agent was unsure, leaves two rows.

Put the header name in the tool description and in the OpenAPI operation. If the agent never sees the header, the retry won't send it.

## Store the first response after execution begins

On [Stripe's idempotent requests](https://docs.stripe.com/api/idempotent_requests), the client generates the key and Stripe uses it to recognize a retry of the same request. The Stripe details below are Stripe's contract.

Stripe says not to put sensitive data, such as an email address, into the key. Stripe suggests a UUID version 4, or another random value with enough entropy to avoid collisions. Stripe's keys are up to 255 characters. If your cap differs, publish it next to the header name.

After a write begins, save three things against the key: a hash or canonical copy of the parameters, the status code, and the response body. Stripe's API v1 saves that status code and body only after endpoint execution begins. A retry with the same key returns the saved response, including a saved 500. If incoming parameters fail validation, or the request conflicts with another request still executing, Stripe does not save an idempotent result, because no endpoint execution began. Those calls can be retried.

The agent can correct a validation error and try again with a new key. After execution has begun, return the saved result on a dropped connection, including a 500, so the agent doesn't open a second object on a guess.

Stripe's API v2 does not repeat a successful operation on retry. It retries failed or partly failed work when it can, and returns an updated response. Publish whether you save the first response or return an updated one. An unstated rerun creates a second charge.

Stripe removes v1 keys after they are at least 24 hours old. A reuse after removal is a new request. That window is Stripe's. Publish yours beside the header if it differs.

## Reject a reused key when the parameters differ

Stripe's idempotency layer compares incoming parameters with the original request and returns an error when they differ. Do the same. Use a stable name such as `idempotency_key_reused`, and say the parameters don't match the first request. Don't create a second object under that key. Don't return the first object while applying the new parameters. Say that a changed amount needs a new key. Otherwise the agent drops the reused key and sends a second charge.

If the first request is still running, don't start a second execution for that key. You can hold the retry and return the saved body when the first execution finishes, or reject the overlap and let the client retry after a result exists. Use one of those rules on every unsafe route.

## Put keys on creates and charges, not on reads

Stripe accepts idempotency keys on POST requests, and says not to send them on GET or DELETE, because those methods are already idempotent. When your verbs differ, match the key to the side effect.

| Method class | Key | Why |
| --- | --- | --- |
| Create that inserts a row | Required | A retry must not insert another row |
| Charge, refund, or send | Required | A retry must not move money or mail twice |
| POST that only searches | Optional | There is no side effect to protect |
| GET | Do not require | A repeated read does not create a second object |
| DELETE of a specific id | Usually no | Deleting an id that is already gone should be safe |
| PUT that replaces the whole object | Often no | The same body sent twice leaves the same state |
| POST that increments, appends, or toggles | Required | The call can look like an update and still be unsafe |

A PUT that means "add one seat" still needs a key.

If the agent omits the key, reject the unsafe write and name the header in the body. Don't mint a fresh key on the server during the retry and still call the operation idempotent. A key the agent never received can't match the next attempt.

## Return the original resource id on the replay

The success body on a retry includes the original resource id. If the first delivery created `cus_1`, the second returns `cus_1`. A new id means the promise failed.

Keep both deliveries on one [agent journey analytics](https://www.agentrail.co/blog/agent-journey-analytics?h=9VVPH9X57WTWN02G) task, and check that they carried the same key and the same resource id. Whether the task finished after that write is [agent task completion rate](https://www.agentrail.co/blog/agent-task-completion-rate?h=9VVPH9X57WTWN03E). The change to what you store against the key belongs in [agent experience optimization](https://www.agentrail.co/blog/agent-experience-optimization?h=9VVPH9X57WTWN04C).

Confirm the single object in your system of record.
