deskthebuildAI AGENTS
← All notes · 5 min read

Our agent repeats tool calls and creates duplicate side effects. How would you stop it?

Imagine a hypothetical support agent that can issue refunds. The model proposes a refund, the tool completes, but the network response times out before the agent records success. The workflow retries and issues the same refund again. A similar failure can duplicate emails, tickets, orders, deployments, or database updates.

The model did not necessarily make the same decision twice. The system lost the distinction between “the action failed” and “the action may have succeeded but acknowledgement was lost.”

Why the common answer is incomplete

Most candidates answer: “Tell the model not to call the same tool twice.”

Prompt instructions can reduce obvious repetition but cannot resolve an ambiguous network outcome or survive a process crash. The model may also produce slightly different arguments for the same business action, making text comparison unreliable. Side-effect safety must be enforced by deterministic application and service boundaries.

A production design gives every logical action a stable identity, records its state durably, and makes retries return the original outcome rather than repeat the effect.

A production approach

1. Classify tools by side-effect risk

Separate read-only tools, reversible writes, and high-impact or irreversible actions. Reads can often retry safely, while a payment, message, permission change, or deployment needs stronger controls. Encode the classification in tool metadata and policy code. Do not rely on the model to decide whether an operation is idempotent or whether the current user is authorized to repeat it.

2. Create an action identity outside the model

When the workflow accepts a logical action, generate an idempotency key tied to workflow ID, step ID, tenant, tool, and business intent. Reuse that key across network retries and process restarts. Do not ask the model to invent it. The key should identify one approved effect; changing the recipient, amount, or resource creates a different action that requires a new decision and possibly new approval.

3. Persist intent before execution

Write an action record with normalized arguments, authorization context, approval state, policy version, and status such as pending before calling the external service. Use a uniqueness constraint on the idempotency key. This durable intent lets another worker discover whether an action is new, running, completed, failed, or uncertain after a crash.

4. Enforce idempotency at the closest service boundary

Pass the key to the downstream API when it supports idempotent requests. If the service is internal, implement a deduplication table and transaction that couples the business mutation with the recorded outcome. The agent orchestrator should also deduplicate, but the service performing the effect is the strongest boundary because other clients or retries may bypass the original worker.

5. Return stored outcomes for repeated requests

If a completed action key appears again, return the original normalized result without performing the effect. If the same key arrives with different arguments, reject it as a conflict instead of guessing which request is authoritative. If an action is still running, return or wait on its state within a bounded deadline rather than launching another concurrent attempt.

6. Handle ambiguous outcomes with reconciliation

A timeout after sending the request produces an unknown state, not a definite failure. Query the downstream service using its operation ID, idempotency key, or business reference before retrying. When no lookup exists, route high-impact actions to manual review. Blindly retrying an unknown payment is unsafe; permanently failing an action that actually succeeded is also incorrect.

7. Bind approvals to exact normalized arguments

For actions requiring confirmation, show the user the tool, resource, recipient, amount, and other material fields. Store the approved argument hash with the action. If the model later changes any material value, invalidate the approval and create a new action identity. A general approval such as “handle the refund” should not authorize arbitrary amounts or recipients.

8. Give the agent state, not raw retry freedom

After every tool attempt, return a structured status such as completed, pending, conflict, denied, or requires_reconciliation. The orchestrator decides which transitions are allowed. Cap model replanning and tool attempts. The agent may propose a recovery, but deterministic workflow code controls whether an action can execute again.

Engineering trade-offs

Idempotency records require durable storage and retention. Keeping them too briefly can allow a delayed retry to repeat an old effect; keeping them indefinitely increases storage and privacy obligations. Transactional deduplication is strongest inside one service, while external APIs may offer only eventual lookup or limited idempotency windows.

Strict argument matching prevents accidental mutation but complicates benign retries when timestamps or non-material metadata change. Normalization must distinguish material business intent from transport fields. Human review protects ambiguous high-impact actions but increases latency and operational cost.

Common mistakes

  • Depending on prompt text to prevent repeated side effects.
  • Generating a new idempotency key for every retry attempt.
  • Marking a timeout as failure without checking whether the action completed.
  • Deduplicating only in agent memory, which disappears after a restart.
  • Reusing one key after changing material tool arguments.
  • Recording completion separately from the business mutation without a transaction.
  • Allowing a model to retry a denied or conflicting action with rephrased arguments.

Failure handling and observability

Trace workflow ID, step ID, action key, normalized argument hash, authorization decision, approval version, attempt count, downstream operation ID, and state transition. Monitor duplicate-key hits, argument conflicts, unknown outcomes, reconciliation latency, repeated model proposals, and actions stuck in running. Alert on any logical action producing more than one downstream effect.

If the orchestrator crashes, a new worker should recover pending actions from durable state. If the downstream service is unavailable, keep the action pending until its deadline rather than changing its identity. If reconciliation cannot determine the result, stop automation and expose the ambiguous state to an operator or user. Never convert uncertainty into an automatic duplicate write.

Interview insight

The interviewer is not testing whether you can prompt an agent to be careful. They are testing whether you understand idempotency, durable intent, ambiguous outcomes, and exact authorization. A staff-level answer assumes retries and crashes will occur and ensures one logical action produces at most one business effect.