Agents, Tool Use & MCP·Question 14 of 90Basic

Your agent charged a customer's card twice. The trace shows one tool call. What happened, and how do you prevent it?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

One tool call in the trace and two charges means the execution was retried even though the model only asked once. The classic path: your HTTP client called the payment API, the request succeeded server-side, the response timed out or the connection dropped, your retry logic saw a failure and fired again. The payment provider saw two valid requests.

Full worked answer with diagram

One tool call in the trace and two charges means the execution was retried even though the model only asked once. The classic path: your HTTP client called the payment API, the request succeeded server-side, the response timed out or the connection dropped, your retry logic saw a failure and fired again. The payment provider saw two valid requests.

The fix is idempotency keys. You pass a stable key per logical operation, and the downstream API returns the original result instead of performing the action twice.

def execute(call, run_id):
    # Key derived from the model's call id, not generated per attempt,
    # so every retry of this operation carries the same key.
    key = f"{run_id}:{call.id}"
    return payments.charge(
        amount=call.arguments["amount"],
        idempotency_key=key,
    )

The critical detail is where the key comes from. Generate a fresh UUID inside the retry loop and you have achieved nothing, because every attempt looks like a new operation. Derive it from something stable: the tool call ID the model emitted, scoped by run ID.

There is a second, sneakier version that interviewers often push toward. The agent crashes after executing the tool but before appending the tool result to the message history. You resume from a checkpoint, the model sees an unanswered tool call, and it calls again. Same double charge, no retry logic involved. The same key defends against it, plus writing the tool result durably before you acknowledge the step as complete.

The general principle: an agent is a distributed system with at-least-once delivery, and the model is an unreliable client that may repeat itself. Every tool with a side effect needs to be idempotent, natively or through a key. Reads are free to retry; writes are not. If a downstream API offers no idempotency support, wrap it yourself: record the key and result in your own store and check before calling.

Worth sketching. The timeline makes it obvious that the second request is your retry, not the model's second call, which is the misdiagnosis the question is testing.

sequenceDiagram
    participant R as Runtime
    participant P as Payment API
    R->>P: charge, key run_7:call_1
    P->>P: charge succeeds server-side
    P->>R: response lost, connection drops
    R->>R: retry logic sees a failure
    R->>P: retry, same key run_7:call_1
    P->>R: replays original result, no second debit

What a good answer covers

  • One tool call in the trace and two charges means the execution was retried even though the model only…
  • The classic path: your HTTP client called the payment API, the request succeeded server-side, the…
  • The payment provider saw two valid requests
  • You pass a stable key per logical operation, and the downstream API returns the original result instead…
  • The critical detail is where the key comes from

Key terms: idempotency keys

Interviewer follow-ups to expect

  1. The model itself decides to call charge twice because it forgot it already did. Does an idempotency key save you?

  2. How do you handle a tool that is not idempotent and cannot be made so?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep