โ† Back to blog

How to Actually Test AI Agents Before They Break in Production

Most teams ship AI agents with zero evaluation. Here is the 4-layer testing stack to catch agent failures before customers do.

Amit Kumar6 min read

Only about 1 in 10 AI agent pilots ever reaches production. I have watched that number play out firsthand: 14 agents running on a single Hetzner VPS, a self-hosted stack that replaced $1,200/month in API bills, and still, the thing that nearly took the whole system down was not the model. It was a tool call that silently started returning the wrong shape at 2am.

We caught it. Barely. And only because I had wired up the unglamorous thing nobody writes blog posts about: tests.

This is the guide for the part of agent development that tutorials skip. Not the demo. The verification. If you are shipping agents that touch real money, real customer data, or real infrastructure, testing AI agents properly is the difference between a demo and a product.

Why agent testing is different from testing software

The old playbook assumes deterministic inputs and outputs. You call a function with x, you assert it returns y. Agents break that contract on purpose. The same prompt can route to a different tool, call an API that is having a bad day, or decide at 3pm that today is the day it skips a validation step.

Gartner flagged this in their 2026 strategic tech outlook: multi-agent systems are being adopted faster than the governance and testing practices needed to keep them safe. The Stanford AI Index 2026 reports that enterprise AI adoption keeps climbing while the share of models that are independently evaluated for reliability has barely moved. The gap is not capability. It is verification.

So you need a testing stack that assumes the agent will be wrong sometimes, and catches it before a human does.

Layer 1: Unit-test every tool, not the agent

The single highest-impact thing you can do is stop trusting your tools. Every tool an agent calls is a function with a contract. Write a unit test for that contract.

When I built the agent stack that runs on one Hetzner box, I wrote a test for each tool that checks three things:

  • The input schema is what the agent thinks it is.
  • The output shape is stable, even when the upstream API returns an error.
  • The tool fails loudly, never silently.

That 2am failure I mentioned? The tool started returning a 200 with an empty array instead of a 404. A normal agent would have shrugged and moved on. The unit test failed in CI the next morning, and the assertion told me exactly which field changed. Without it, the agent would have been confidently wrong in production for a week.

The rule: if a tool is not unit-tested, the agent is not allowed to use it. Full stop.

Layer 2: Build a golden dataset of real conversations

Unit tests cover tools. They do not cover judgment. For that you need a golden dataset: a set of 30 to 50 real or realistic agent tasks, each with the expected path and expected outcome.

I keep mine in a flat file. Each row is a scenario:

  • A user request that actually came in.
  • The tools the agent should call, in order.
  • The final answer or action that counts as correct.

Every time I change the system prompt or swap a model, I re-run the whole set. If accuracy drops below my threshold (I hold mine at 90% on the critical paths), the change does not ship. This caught a regression last month where a model update made the agent skip a confirmation step on refund requests. No customer noticed. The golden dataset did.

Start with 10. You do not need 500. Ten real scenarios beat a thousand synthetic ones because real requests have the weird phrasing and missing context that breaks agents.

Layer 3: Run in shadow mode before you trust it

Shadow mode is the cheapest insurance in agent development. You deploy the agent, it processes live input, it produces output, but nothing it decides actually happens. You log the divergence between what the agent would have done and what your old system did.

I ran my self-hosted agent in shadow for 7 days before flipping it on for real customer traffic. In that week it tried to call a tool it had no business calling 3 times. In shadow, that is a log line. In production, that is an incident.

The pattern: shadow for at least a full business cycle. A week caught the weekday patterns. It missed the Monday morning spike that was the real stress test. Two weeks would have been better. Plan for two.

Layer 4: Replay traces when something breaks

When an agent does something stupid, you need to reproduce it. That means logging every step: the prompt, the tool calls, the responses, the decision. Store them as traces.

When the 2am tool-change incident happened, I pulled the trace from the night before, replayed it against the new tool behavior, and watched the failure happen on demand. Fix took 40 minutes instead of 3 days of guessing. If you are not storing traces, you are debugging blind.

This is where MCP helps too. A proper tool layer gives you a consistent interface to log every call. If your agent is calling tools through a tangle of custom integrations, you cannot trace them. Standardize the interface, then trace it.

The honest part

None of this is exciting. Tool unit tests, golden datasets, shadow mode, trace replay. There is no chart that makes this look like a breakthrough. The Wavestone 2026 technology trends report puts AI measurement and governance at the top of what enterprises say they need and the bottom of what they have. Everyone wants the agent. Nobody wants the test suite.

But here is the math that changed my mind. My first agent pilot cost $1,497, took 3 weeks, and involved 4 people. It shipped to real users with no evaluation harness. It broke in front of a customer on day 9. The second agent, built with the four layers above from day one, cost about the same to build and has not had a customer-facing failure in 4 months.

The testing did not slow me down. The lack of testing did.

A checklist you can steal

If you take one thing from this post, take the minimum viable test suite:

  1. Every tool has a unit test for input schema, output shape, and loud failure.
  2. You have at least 10 real scenarios in a golden dataset, re-run on every prompt or model change.
  3. New agents run in shadow mode for at least one full business cycle before going live.
  4. You store traces and can replay any failure on demand.

That is it. Four things. You can stand this up in an afternoon if your tools are already clean.

What this is not

This is not a replacement for monitoring. Tests catch what you thought to test for. Monitoring catches what you did not. You need both. It is also not a reason to skip shipping. The point of testing is to ship with confidence, not to ship never.

If your agent touches real money or real data and you have none of these four layers, you are not running a product. You are running a demo with the safety off. Build the harness, then turn it on. Your future self at 2am will thank you.

+0

...

CLAP_TO_APPRECIATE

More writing

Read on Substack

Get the next build note before it becomes a blog post.

Founder notes, product experiments, and practical AI systems breakdowns from the workbench.

Build logsAI agentsGrowth systems
Subscribe on Substack