What happens when an AI tool writes twice?

The database opened a ticket. The agent missed the reply. A four-case SQLite lab shows how a retry can open a second ticket and how to prevent it.

Before you start

For developers building tool calls and architects reviewing how writes recover. Python is optional.

Evidence

Locally executed SQLite lab. Read the method and limits below.

On this page
The first attempt and retry share an operation ID and resolve to one ticket.

Your agent asks a tool to open an operations ticket. The database creates ticket 1. The reply never reaches the agent, which tries again. Without a way to recognize the second request, the tool opens ticket 2. From the agent’s side, both attempts look reasonable.

Give each intended write one operation ID and reuse it across retries. The component that owns the side effect should record that ID with the result. A timeout tells the caller that it did not get an answer. The write may already have happened.

What ran

We ran four cases against local SQLite. Each case starts with an empty database. The program discards the first reply after the insert commits, or starts two callers together to exercise the same operation. It counts stored tickets and captures the tool’s replies. There are no model calls, external services, credentials or real tickets.

Read the results below, then download the Python script and run it with Python 3.10 or newer. The script verifies the expected outcomes and prints them as JSON.

python3 tool-retries.py
Recorded SQLite outcomes
CaseObserved repliesTickets stored
No operation IDCreated ticket 22
Same operation IDReplayed ticket 11
Concurrent attemptsCreated ticket 1, replayed ticket 11
Changed inputCreated ticket 1, rejected changed input1

Inspect the JSON results

Put the guarantee where the write happens

In the lab, the caller creates op-1 once and sends it again after a timeout. The tool looks it up inside the same SQLite write transaction that can create the ticket. If the operation already exists with the same input, the tool returns the first ticket ID. If the destination changes under that ID, it rejects the request. The next genuine ticket needs its own operation ID, even when the destination is identical.

For an agent, generate the ID in application code and persist it before the first attempt. If a human approved a write, bind the approved arguments to that operation. When the model replans or the process restarts, the operation’s identity should survive. A per-attempt trace ID can still help you debug, but it answers a different question.

Scope an ID to the tenant and operation type in a shared system, and check authorization on retries before returning a stored result. Make the deduplication window longer than the longest retry or redelivery you support. Keep the original result, or enough information to reconstruct it under the tool’s documented contract. The AWS Builders’ Library treatment of retries explains why caller intent and changed parameters matter.

When a tool calls another service

SQLite can commit the ticket and its operation ID together because both are stored in one database. A ticket in a remote service introduces a second failure boundary. Your process can lose the downstream reply after the remote system has written the ticket. A local “done” flag alone cannot settle that outcome.

If the remote API supports an idempotency key, pass the operation ID through and follow its actual retention and retry rules. For example, Stripe’s idempotency documentation describes which responses each API version reuses. If the remote system offers a lookup by a unique external reference, you can reconcile an uncertain outcome before sending another write. If neither exists, surface the uncertain result for review. The right trade-off depends on what a duplicate would cost.

What to test before shipping

Drop the response after commit. Run two attempts for the same operation simultaneously. Restart the caller before it can record the result. Change the arguments on a retry, and exercise the moment when the deduplication record expires. In each case, count effects in the destination system and check what the user sees. A clean application log can coexist with two tickets.

This lab exercises one table in one database. It does not test network failures, job queues, tenant authorization or a remote provider. Its result gives you a failure case to reproduce at your own tool boundary.

Suggest a correction
Keep exploring

Understand the choices around a model.

All articles