Skip to content
News

How to stop AI agents from doing the same work twice

Follow an agent write that lost its acknowledgment. Learn which duplicates to prevent when queuing work, claiming a Run, and retrying a change.

Bright green Romanesco florets form repeating spirals around a small metal instrument.

An agent opens a warehouse investigation after an hourly exception report flags a delayed shipment. The ticketing service accepts the request. Before the agent can record the ticket number, the connection drops. Later, a second agent picks up the unfinished work. Should it open a ticket?

A shared task queue helps agents avoid picking up the same work at the same time. A lease gives one worker temporary ownership of an execution attempt. The destination still needs its own way to recognize a repeated action. Keeping those three responsibilities separate is what makes a workflow safe to resume.

Give the job a stable identity

Start with the work the team wants done: open one investigation for a specific shipment exception. The same exception may appear in an hourly report and in a separate message. Give it a stable identity based on the shipment and the exception episode, then check for an existing job when either source reports it. The next occurrence should have its own identity if it represents a new issue.

This check needs to hold up when two requests arrive together. If your integration first searches for a match and then writes independently, both callers can see an empty result. Enforce uniqueness where the job is recorded or use a queue that supports deduplication for the same request. Record the key before the first submission and check the applicable retention period.

Pale ceramic trays hold separate precision instruments on a work surface.

A claim should confer ownership for a limited time

Two agents can read the same backlog and both announce that they are taking the next task. The important step is when the queue grants a claim. A worker should wait for that grant before beginning the covered work.

With a lease, the claim has an expiration. Renew it while working. If renewal fails, stop issuing new actions and check the work's current state. A later worker needs to read the current assignment and recovery policy, then check the evidence already saved. It may be able to resume the work, or it may need a new attempt or a human decision. An expired lease does not undo anything the previous worker already did.

Swarm's connected agent lifecycle separates the saved Task from the Run that attempts it. An eligible agent can claim queued work, start the Run with its current lease, and renew, release, or complete it as appropriate. That lease is authority for the Run. If two separate Runs were created for one shipment exception, each still represents distinct queued work. Preventing that duplicate belongs at submission.

Reconcile the write before you retry

Return to the missing acknowledgment. The second worker knows the first attempt went quiet. It does not know whether the destination changed. The best evidence is the destination's ticket history or the operation receipt the integration saved.

What you knowWhat to do next
A matching investigation already exists.Record its reference on the original job. Do not open another.
There is no result yet, but the destination supports a replay with the original key.Follow the destination's retry rules for the same payload and key.
The destination rejected the request.Record why. Correct the cause only within the allowed scope before making a new attempt.
The effect is unknown and replay is not supported.Pause the write and send the case to someone who can verify the outcome.

An idempotency key identifies the intended effect so a service can recognize the same request again. It should remain stable if another worker takes over the same action. A new chat or execution attempt ID is a poor choice for that key because it changes at the moment the service needs to recognize the retry. Check the destination's documentation for supported operations, how it handles different payloads under the same key, and how long it retains the result. AWS's guide to making retries safe explains why a caller supplied identifier is useful here.

A raised channel turns through the middle of a translucent block.

Test the moment your first worker disappears

Use a test destination and a synthetic exception. Design two exercises before handing more autonomy to the agents:

  1. Give two active workers one queued job. Confirm that only the holder of the current claim proceeds on that Run and that the other worker moves on or waits.

  2. Simulate a lost response after the destination accepts the write. Start the recovery path and verify from the destination that the second attempt did not create a second record. Confirm a returning worker cannot use the expired claim to complete the original Run.

Save the source event, job identity, Run references, confirmed destination result, and the remaining decision in a private Swarm Space. If an outcome stays uncertain, make that visible. The AI agent audit trail guide shows how to investigate the gap between an execution record and a destination's evidence.

White salt crystals branch across a dark stone surface.

Before queuing a second job, make sure you can recover the first one from the record alone. A successful recovery leaves one investigation with a clear owner and a verifiable result. The team should never have to count duplicate tickets to find out what happened.

Swarm | How to stop duplicate work across AI agents