← All articles
How-to guideWorkflow testing

How to inject timeouts and rate limits into an AI agent's tool calls

Wrap each tool in a function that returns scripted faults in a fixed order, then assert on what your agent did to the world. To inject the same faults on your real code without changing it, run npx agenttwin.

In this article
Conceptual illustration · AgentTwin journal

Illustrative test design

Illustrative test design: one required record for the request, one required notification for that record, and unrelated state left unchanged.
Check the effects, not only the reply. Illustrative test design; this image is not a run result.

Wrap each tool in a function that returns scripted faults in a fixed order (a timeout after the write, a 429 with retry-after, a 500), then assert on what your agent did to the world afterwards. That tests your retry code. To inject the same faults at the network level on your real code, without changing it, run npx agenttwin.

A scripted fault wrapper

// Wrap a tool so each call follows a fixed plan, then repeats.
function withFaults(tool, plan, log = []) {
  let call = 0;
  return async (...args) => {
    const step = plan[call++ % plan.length];
    log.push(step);
    if (step === "ok") return tool(...args);
    if (step === "timeout-after-write") {
      await tool(...args);                       // the write happens...
      throw Object.assign(new Error("timed out"), { code: "ETIMEDOUT" }); // ...the reply is lost
    }
    if (step === "429") throw Object.assign(new Error("rate limited"), { status: 429, retryAfter: 2 });
    if (step === "500") throw Object.assign(new Error("server error"), { status: 500 });
  };
}

const refunds = [];
const refund = withFaults(async (id) => { refunds.push(id); }, ["timeout-after-write", "ok"]);

Run your agent with refund as its tool, then check refunds. If your retry sends the same call again after the first timeout, refunds has two entries for one intended refund, and you have found a duplicate. Seed the plan so failures reproduce, and keep each failing plan as a regression test.

What the wrapper can't test

It only covers calls that go through your wrapper. It doesn't exercise the real client library, the SDK's own retries, webhooks delivered to your routes, or the services your agent's tools call underneath.

Injecting faults at the network level

npx agenttwin starts twins of the services your code calls, sends your app's outbound calls to them, and reruns each workflow under these failures:

Failure What the twin does
Timeout after write Saves the write, then answers too late, so your code retries something that already happened
Lost reply Saves the write and drops the connection before answering
Server error Answers 500 to the first request; nothing is saved
Outage Answers 500 to every attempt, longer than your client's own retries
Slow reply Applies the request but answers after your client's timeout
Duplicate delivery Delivers the triggering webhook or request twice
Model rate limit OpenAI or Anthropic answers 429 with its own error body and retry-after, for the whole run and again after the first call

After each run it compares every service's final state with the clean run and reports what would have happened (refunded twice, told the customer about a refund that failed), an estimated cost per incident, and the file and line. See How to simulate timeouts and duplicate event delivery for a worked example.

Why "after the first call" matters

A model that is rate-limited from the start fails before it does anything. A rate limit that hits after the first call shows what the agent had already written to other services before it gave up, and whether a retry repeats those writes.

Continue readingAgentTwin vs WireMock →
Evaluate one workflow

Can your workflow pass the same check after a failure?

Bring the entry point, dependency operations and result you need to preserve. Discuss whether AgentTwin supports the test, what preparation is needed and the scope and costs before proceeding.

Discuss your workflow Try the sample first →← Back to all articles