Why an AI agent must never quietly retry after a 412
A step-by-step guide to ETag and If-Match that teaches an agent when to stop, including on APIs that bend the HTTP standard.
In brief
- An agent reads data, thinks, then writes. That gap is long enough for a user to edit the same resource, and If-Match blocks the lost update that follows.
- On a 412, never swap in the new ETag and resend the old version: doing so switches off the protection.
- Every API differs: GitHub does not support conditional writes unless an endpoint's docs say otherwise, Calendar does not apply them to inserts, Dataverse uses weak ETags.
Google Calendar’s API rule is brief. You send the etag from your previous read in an If-Match header. If nobody has touched the event since, your edit is saved; if someone has, you get a 412 (Precondition Failed). That 412 is what stops your agent from overwriting a meeting the user has just moved by hand.
Agents differ from scripts in one respect: they take time. An agent reads a record, calls a model, sometimes waits for another tool to return, and only then writes.
All the while, the user still has the app open and keeps editing. MDN calls the resulting failure a lost update: an upload silently erases a change made after the client fetched the resource.
For an FDE deploying agents into a client’s calendar, CRM or records system, this bug never shows up in the demo. It shows up in week three, when a department head asks why the meeting room they just rebooked has reverted. This guide shows how to prevent it from the start.
What you will build, and what you need first
You will build a pair of agent tools, read_event and update_event, with the constraint that update_event cannot run without an ETag. You will also add a 412 branch that hands the decision to the user. If you own the server as well, there is a section on server-side checks.
You need an API that returns ETags and accepts If-Match, such as the Google Calendar API. You also need to be able to read HTTP response headers, plus any agent framework that lets you define tools. The code below is simplified: http_get and http_put stand in for your HTTP client wrapper, and the paths are illustrative.
Step 1: the ETag must travel with the data the agent read
One approach looks sensible and is wrong: have update_event re-read the resource just before writing, grab the fresh ETag and send it straight away.
That protects only the few milliseconds between two requests. The agent made its decision from a read minutes earlier, so the window where conflicts are most likely goes uncovered.
So the read tool must return the ETag to the agent, and the write tool must require the agent to hand back that same ETag:
# Simplified for illustration
def read_event(event_id):
resp = http_get(f"/events/{event_id}")
return {"etag": resp.headers["ETag"], "event": resp.json()}
def update_event(event_id, etag, new_event):
if not etag:
raise ValueError("update_event requires the etag from read_event")
return http_put(f"/events/{event_id}", json=new_event,
headers={"If-Match": etag})
To verify, log every call the agent makes to update_event and compare the ETag in the log with the one read_event returned on the read the agent relied on. The two values must be identical.
Step 2: what does a conditional write look like?
At the protocol level, you add a single header:
PUT /events/abc123
If-Match: "etag-from-previous-read"
Content-Type: application/json
{ ...the full event after the agent's edit... }
There are two outcomes. If the resource is unchanged, the server writes and returns success. If it has changed, Google Calendar returns 412. To test it, run the agent, wait until it has finished reading, edit the event by hand in the calendar UI, then let the agent write. If you do not get a 412, your tool is sending the wrong ETag or none at all.
Step 3: what to do after a 412
A backend engineer’s reflex is to wrap the call in a retry loop. With an agent, that is a mistake. TianPan’s piece on an agent and a user editing the same calendar event is explicit that the bad pattern is letting the tool layer silently catch a 412 or 409 and quietly run again.
A post on DEV Community makes a similar point: swapping in the new ETag and resending the old document defeats the protection.
Microsoft’s Dataverse documentation points to a better course: fetch the latest version, then reconsider whether the operation still makes sense. For an agent, that means returning the conflict to the model along with the fresh data, so the model or the user can decide:
# Simplified
resp = update_event(event_id, etag, new_event)
if resp.status == 412:
latest = read_event(event_id)
return {"status": "conflict",
"latest": latest,
"note": "The event was just modified. Review it before writing."}
Take a hypothetical case. The agent plans to move a meeting to 3pm while the user has just changed the room. On re-reading, the agent sees that only the room changed and nobody touched the time. It can propose “move to 3pm, keep the new room” and wait for the user to confirm, instead of pushing the stale version with the old room.
Step 4: if you also write the server
If you build your own intermediary API for the agent, there are three jobs. First, compare ETags correctly: RFC 9110 requires origin servers to use strong comparison for If-Match, and according to MDN a weak ETag with the W/ prefix will never match under that algorithm.
Next, checking the version and writing must be one atomic operation. The DEV Community post warns that a server which checks the version, yields to another request and then writes unconditionally still loses updates. The neatest fix (shown in simplified form) is to put the condition directly into the write statement:
UPDATE events
SET body = :new_body, version = version + 1
WHERE id = :id AND version = :expected_version;
-- 0 rows affected => return 412
Finally, force clients to send a precondition using the 428 Precondition Required status defined in RFC 6585. The RFC recommends that a 428 response explain how to resubmit correctly, for example “re-read the resource and send the ETag in If-Match”, so the agent can fix its own call.
Does the client’s API actually support it?
Before trusting If-Match, read each API’s documentation. These three systems show how widely they vary:
| API | Conditional writes | What to remember |
|---|---|---|
| Google Calendar | Yes, send the etag in If-Match; a mismatch returns 412 | No conditional support for insert operations |
| GitHub REST | Not for POST, PUT, PATCH, DELETE | Unless the endpoint’s documentation says otherwise |
| Microsoft Dataverse | Yes, on PATCH | ETags are weak, e.g. If-Match: W/"470867" |
Dataverse shows that practice does not always follow the RFC. Under RFC 9110 a weak ETag never matches, yet Microsoft’s documentation sends exactly such a weak ETag in If-Match on PATCH.
With GitHub and other APIs without conditional writes, the agent has no protection at the HTTP layer. In that case, build your own guardrail, such as requiring user confirmation before every write, and state the limitation plainly to the client from the outset.
Three common first-time mistakes
The first is fetching the ETag just before the write instead of using the one from the read the agent relied on, as described in Step 1. The request still carries If-Match and still passes a simple test, but the wait while the model thinks goes unprotected. How to catch it: compare the logged ETags between the read and the write.
The second is using If-Match: * as if it were version control. In a Dataverse upsert PATCH, this header only prevents a new record being created: if the record does not exist, you get a 404. It does not compare versions, so it does nothing against overwrites.
The third is writing a shared library that rejects every ETag with W/ because of an overly rigid reading of the RFC. Such a library breaks the moment it meets Dataverse, where weak ETags are the documented practice. Make the comparison rule depend on each API and spell it out in the integration configuration.
Where this skill shows up on the job
At a client, the first discovery session should include one question: which systems will the agent write to, and will anyone be editing them at the same time? If so, add “supports ETag/If-Match?” to the integration checklist, next to authentication and rate limits.
On a CV, instead of “built an agent that calls APIs”, write “designed an agent write-tool layer with optimistic concurrency via ETag/If-Match, routing 412 conflicts to the user for a decision”. In a system design interview, walk through all three steps: read with the ETag, write conditionally, and on a 412 stop and ask.
On APIs that return 412, an agent that knows to stop and ask will not quietly overwrite a user’s work. On APIs that do not, that guardrail is your job.
Was this article useful?
Thanks for the feedback!
8 sources
- If-Match header (MDN)
- RFC 9110: HTTP Semantics
- RFC 6585: Additional HTTP Status Codes
- Get specific versions of resources | Google Calendar
- Best practices for using the REST API (GitHub Docs)
- Perform Conditional Operations Using the Dataverse Web API · 2026-10-29
- The Dual-Writer Race: When Your Agent and Your User Edit the Same Calendar Event · 2026-04-26
- Stop Stale Editors Overwriting Changes with If-Match (DEV Community)