DEV Community

Ravi
Ravi

Posted on

Field note: same error, opposite truth

What an AI agent can't know after a dropped connection, and the one fix that held up in every case I tested.

An agent calls a refund tool. The connection drops. The agent gets an exception.

Did the refund happen?

If it did, retrying refunds the customer twice. If it didn't, giving up means it never happens. The agent has to pick one, and the only evidence it has is the error.

I wanted to know how much that error actually tells it. So I built a small testbench, broke the connection on purpose in different places, and counted what really happened in a database.

The setup

A fake refund tool, issue_refund, served over MCP (the protocol agents use to call tools). Every refund is written to a local PostgreSQL database, which is the ground truth: it says how many refunds really happened, whatever the agent reports.

Between the client and the server sits a small proxy that can cut the connection at a chosen moment:

  • after the commit: the refund is written, then the reply is cut, so nothing reaches the client;
  • before the request: the client's call never reaches the server.

I ran the reference Python MCP client and server (mcp 2.3.0) with three retry strategies: no idempotency key, a new key per attempt, and one stable key per order. Each combination ran 10 times, on Windows and on Linux: 480 trials per machine in the whole experiment, which also included a mid-stream cut and uncut controls.

What I saw

Both cuts raised the same exception. In every trial of both cases, the client's first attempt failed with the same RemoteProtocolError.

The outcomes were opposite.

  • Cut before the request: nothing had happened, so even a naive retry refunded exactly once (120 of 120 trials per machine).
  • Cut after the commit: the refund had already happened, so the retry refunded again. Without a key: two refunds in 40 of 40 trials per machine.

The agent's own report was wrong in those duplicates. In all 80 duplicate trials per machine, the client reported one successful refund while the database held two. If you judge an agent by what it says, you never see this bug.

A new key per attempt doesn't help. It sends a different key with each retry, so the server sees two different refunds. It duplicated exactly as often as sending no key.

One thing worked every time: one idempotency key per operation (derived from the order, not the attempt), sent with every retry and enforced by the database. One refund in 40 of 40 trials per machine at both cut points.

The two machines agreed on every verdict. They differed only in timing. Timing did differ between the cuts, but only because of how I built the tool, so an agent can't rely on it in general.

Why "retry on error" and "never retry" are both wrong

With the same error and opposite truths, any rule based on the error alone is wrong in one of the two cases:

  • "retry a dropped connection" refunds twice when the commit already happened;
  • "never retry it" loses the refund when the request never arrived.

The fix has to make the retry harmless, not decide whether to retry. That's what an enforced idempotency key does: the second call gets the stored result back instead of a second refund.

Four ways to break it

The testbench now covers four failures, each judged from the database:

  1. The reply is lost after the refund commits.
  2. The connection is cut before the request arrives.
  3. The connection is cut mid-call, after a progress update, before the commit. Seeing progress is not evidence that the refund happened.
  4. The agent's own process is killed after the commit and restarted. Anything it kept in memory, like an attempt counter or a key generated at start-up, is gone.

The fourth one caught a pattern that passes the others: an agent that looks the refund up after its own call times out refunds twice after a restart, because the restarted process has seen no timeout.

A quiet default in the OpenAI Agents SDK

I also wired the testbench to agents built with Pydantic AI, LangGraph and the OpenAI Agents SDK, using scripted models so no API key is involved.

One detail stood out. By default, the OpenAI Agents SDK (0.26.0) doesn't raise when an MCP call fails. It hands the model the text "An error occurred while running the tool." Nothing then stops the model from calling the tool again. If the model resends the call without an idempotency key, and the first call had gone through, that's a double refund. The SDK isn't doing anything wrong here; it means the safety has to come from the tool and its key, not from the framework.

A tester found three bugs, in my tool

The first independent tester ran five agents through three scenarios and found three bugs, all in my reporting:

  • a WARN headline that claimed "exactly one refund per order" when nothing had been refunded;
  • a caveat copied from the wrong scenario;
  • an example agent that never triggered the mid-stream cut, so a "PASS" in my guide had never actually been run.

All three are fixed in 0.5.0. The test also showed why one scenario isn't enough: an agent that says "done" after any error passed the lost-reply test, only because the refund happened to land, and failed both cut tests, where it claimed refunds that never happened.

Limits

This is narrow on purpose: one simulated tool, four fault types, one reference client and server, and results that repeat under the stated settings. They're not rates. The proxy cuts both sides at once; a silent network drop, or a cut while the tool is mid-write, wasn't tested. The framework examples use scripted models, so they show the harness working with each framework, not how real models behave.

Try it on your agent

It's open source (MIT), runs locally, and touches nothing real:

pip install "agent-crashtest[local]"
crashtest selftest
crashtest run refunds --agent your_agent.py
Enter fullscreen mode Exit fullscreen mode

If you run it against your own agent, I'd like to hear what happened, pass or fail.

Top comments (0)