# Testing failures

Prove your retries, timeouts and fallbacks work before production finds out.

## Retries on rate limits [#retries-on-rate-limits]

```ts
test("retries after a 429", async () => {
  await using llm = await mockLLM();
  llm.on({ turn: 1 }).reply(sequence(error(429, { retryAfter: 1 }), text("OK")));

  const answer = await agent.run("Hello", { baseURL: `${llm.url}/v1` });

  expect(answer).toBe("OK");
  expect(llm).toHaveBeenRequestedTimes(2);
});
```

## Streams that die halfway [#streams-that-die-halfway]

```ts
llm
  .on(/report/)
  .reply([text("Here is part one of the report and"), fault("abort", { afterChunks: 4 })]);
```

Check that the UI shows a retry action and doesn't save a half answer as final.

## Timeouts [#timeouts]

```ts
llm.on(/slow/).reply(fault("timeout", { afterMs: 30_000 }));
```

Run with fake timers to keep the test fast.

## Bad tool calls [#bad-tool-calls]

Models sometimes call a tool that doesn't exist, or with arguments that don't fit its schema:

```ts
llm.on(/refund/).reply(toolCall("refundd", { id: 42 }));
```

Your agent should return a tool error to the model rather than crash.

## Refusals and length limits [#refusals-and-length-limits]

`finish("content-filter")` and `finish("length")` cover the two finish reasons UIs most often forget. Each maps to the provider's own value: `content_filter` and `length` on OpenAI, `refusal` and `max_tokens` on Anthropic.

```ts
llm.on(/essay/).reply(text("The history of"), finish("length"));
```