The first time my AI agent tried to explain four failed test runs, it got all four wrong. Two weekends and five small fixes later, it gets 75% of 24 failures right, against 50% for a simple one-prompt version of the same model.

This post is the story of those fixes. None of them was a clever prompt. Most were the kind of thing a QA engineer does every day: read the logs, find where the tool breaks, and stop trusting what you haven't checked.

It is written for testers curious about AI agents, and for anyone who wants to see what building one actually looks like, failures included. The code, the test data and every result are public on GitHub.

What the agent does

When an automated test run fails in CI, someone has to answer one question before anything else: whose problem is this? I call it Test Failure Analyzer, and it sorts every failure into one of five buckets:

Category Meaning Who fixes it
product_bug The application really is broken Developers
test_bug The test itself is wrong: a stale locator, a bad expected value Test authors
flaky Fails sometimes, passes sometimes, nothing changed Whoever owns stability
environment A missing setting, a database that's down Ops or platform
infra The CI machinery itself broke CI owners

There are two ways to ask a language model this question. The simple way, which I call the baseline, pastes a summary of the failure into one prompt and asks for an answer. The agent way gives the model tools and lets it investigate: rerun results, what changed in this run, the failing test's code, the application's code, a log search. It decides what to look at, looks, and only then answers.

The model loops between choosing a tool and reading its result, then calls finish and answers. Plain code checks that answer before anyone sees it.

Everything runs on my laptop: Ollama with qwen3:8b, an 8-billion-parameter open model, on a CPU with no graphics card. That makes it slow, but free and private, and it keeps me honest. A small model can't hide a badly designed agent.

How I keep score

You can't improve what you can't measure, and you can't measure a failure analyzer without failures whose real cause you know for certain. Real CI history is no good for this: nobody wrote down the true cause, and half the time people guessed.

So I built a small shop to break on purpose, tfa-failure-lab. It's a FastAPI web store with a Postgres database and a Playwright + pytest end-to-end suite. A GitHub Actions workflow takes switches, and each switch breaks it in one known way:

  • Product bugs: tax rounded the wrong way, sorting by price as text, removing a cart item by name instead of by id, a checkout form that stops validating.
  • Test bugs: a renamed locator, a wrong expected message, a reversed sort assertion, a typo in the test password.
  • Flaky: the cart API returns HTTP 500 on some calls; the product page is sometimes too slow.
  • Environment: a required setting removed; the database moved to a port where nothing listens.

Because I caused each failure, its label is known by construction. Twelve scenarios, each run twice, give 24 labelled CI runs. The two runs of a scenario differ only in an unrelated change (a CSS tweak, a footer edit), the way real commits carry noise.

Two rules keep the score honest. The analyzer only sees what a real CI run produces: the JUnit report, the logs, a rerun of the failed tests, the run's diff and a snapshot of the source. And a leak check rejects any case whose files contain a word that hints at the answer. It caught more leaks than I expected, including the word "flaky" sitting in a README line that one of my noise changes happened to touch.

On this set, always guessing the most common category scores 33%. The one-prompt baseline scores 50%. That's the bar the agent had to beat.

Version 1: zero out of four

I wrote the agent loop by hand, with no framework, so I could see every step. The loop is short. Send the model the failure and a list of tools. If it asks for a tool, run it and send back the result. Repeat until it answers, or until a step limit stops it. Then ask for a final verdict as JSON and check that it's valid.

I tried it on four cases, one from each category. It got all four wrong. The verdicts weren't even the interesting part. The traces, the step-by-step record of what the model did, showed three separate problems:

  1. It talked about tools instead of using them. The model would write "I will now check the rerun results" as plain text and stop. No tool call, so nothing happened, and the loop treated the text as its final answer.
  2. It called tools with the wrong arguments. Extra parameters, slightly different names, and the tool rejected the call with an error.
  3. When it did get evidence, it ignored it. It would read a result that pointed one way, then answer the other way.

The fixes:

  • Be lenient about arguments. The tools now drop parameters they don't take instead of failing.
  • Nudge, then require an explicit end. A reply with no tool call gets a short reminder to call a tool, up to twice. The model now ends by calling a finish tool, so finishing is a decision rather than an accident.
  • Hand over the cheap facts up front. The first message now already contains the rerun result and a summary of the application log. The model no longer has to remember to ask for the obvious.
  • Spell out decision rules. The final prompt lists, in order, what makes a failure environment, flaky, a test bug or a product bug.

The lesson that surprised me: an 8B model treats a tool list as optional. Anything that must happen, the loop should make happen.

Version 2: two out of four

With those fixes the same four cases went from 0 to 2 correct. It now got the test bug and the missing setting right. The traces showed why the other two still failed, and the first reason was my tool, not the model.

The model asked for a test's source code by its short name, like test_sort_by_name_za. My tool expected the full id, tests.test_suites.test_catalog::test_sort_by_name_za[chromium], and answered with an unhelpful error. The model then tried the same call again. And again. In one case it spent three of its six steps on the same failing request.

Any tester knows this bug. The tool's contract was stricter than its real users, and its error message didn't say how to fix the call.

  • Accept what callers actually send. The lookup now matches a short name, a full id or a name with [chromium] on the end.
  • Refuse a repeat of a failed call. The loop remembers calls that failed. If the model repeats one exactly, it's told to try something else or finish.
  • Put the decisive evidence in front. One product bug was a database query in the shop that had been changed to delete a cart item by name. The model never asked for the diff, so it never saw the change, and it blamed the test. Now the first message includes the run's diff and the failing test's source, both capped in size.

This is the most reusable lesson in the whole project. For a small model, what you put in the first message matters more than which tools you offer. Tools are for digging further. The facts that decide most cases shouldn't depend on the model remembering to ask.

The model wrote the answer, then picked another one

One of version 2's misses was a flaky case: the cart API answered HTTP 500 on some calls. Both failed tests passed when the CI rerun them a moment later, and the model called it an environment problem.

My guess was that it couldn't tell "sometimes fails" from "always fails". The log summary listed the 500 errors but not how many calls succeeded. So for version 3 I changed the summary to show an error rate for each endpoint:

Case What the summary now says
Flaky cart API POST /api/cart/{item}: 7 of 17 calls failed with a server error
Database down GET /inventory.html: 29 of 29 calls failed with a server error

Version 3 got three of four, but it still called this case environment. Then I read its final answer properly. In its own evidence list, the model had written:

The test passed on rerun, suggesting the issue is intermittent but consistent in the environment.

It had found the deciding fact and written it down, and it still didn't act on it. More evidence wasn't going to fix this, because the evidence was already there.

So I stopped asking the model to make that call. A failed test that passes on an immediate rerun, with no change in between, is how CI tools define a flaky test. That's a rule, so code can enforce it. I added a guardrail: after the model answers, if any failed test passed on rerun and the verdict isn't flaky, the verdict becomes flaky. The model's root cause and evidence stay. A line saying "2 of 2 failed tests passed when rerun" is added at the top of the evidence. The guardrail stays out if there's no rerun, or if the rerun didn't cover every failed test.

Two details matter for honesty. First, the eval records the model's own answer as well as the final one, so every report shows both scores. Second, I checked the rule against all 24 cases before trusting it. Every flaky case had at least one test pass on rerun, and no other case had any. That separation is cleaner than real CI will be, because my lab's bugs fail the same way every time.

The division of labour I ended up with: the model investigates and explains; deterministic code enforces the signals we already trust.

The full run: 75% on 24 failures

Four cases make a smoke test, not a result. So the last step was all 24 cases, for both the baseline and the agent, overnight on the laptop.

Version Accuracy Time per case Prompt tokens per case
Always guess the most common category 33% – –
One-prompt baseline 50% 127 s ~900
Agent, the model's own answer 62% 924 s ~14,000
Agent + rerun guardrail 75% 924 s ~14,000

What got better:

  • Test bugs went from 2 of 8 to 6 of 8. Reading the diff and the test code fixed the renamed locator, the reversed sort assertion and the password typo, all of which the baseline missed.
  • Flaky went from 2 of 4 to 4 of 4. The model alone got only 1 of the 4. The guardrail fixed the other three and changed no other case.

What didn't, and what got worse:

  • Product bug vs test bug is still the weak spot. Four of the six misses sit on that line. Two were the same broken checkout validation, where the page never shows its error message and the test times out waiting. The model blamed the test's waiting logic instead of the app.
  • The agent got four cases wrong that the baseline got right. More investigation means more chances to talk itself into a wrong story.
  • The model isn't consistent. In three scenarios the two near-identical runs got different verdicts. They differ only in an unrelated change, so that's instability, not insight.
  • It's expensive. About 7 times slower and 15 times more prompt tokens than the baseline. On a CPU that's 15 minutes per failure. A GPU or a hosted model would cut that a lot, but the ratio is the cost of investigating.

Every answer, and a step-by-step trace of every agent run, is in the repo's evals/results/ folder.

What a tester brings to building agents

Looking back, none of the fixes needed AI expertise. They needed QA habits:

  1. Build the test data before the product. Labels by construction and a leak check are what made every number in this post believable, including the bad ones.
  2. Read the traces, not just the score. Every fix came from reading what the model actually did, step by step. A score tells you that something is wrong. The trace tells you what.
  3. Treat the model like an unreliable component. Make the important steps happen in code, validate its output, and enforce known rules after it answers.
  4. Report the uncomfortable numbers too. The model-only score, the cases that got worse and the cost are what make the headline number worth trusting.

Next I'm giving the agent a memory of past failures: when a new failure looks like one already resolved, it can see how that one turned out. That should help with exactly the inconsistency above, as long as the eval never lets a case look up its own twin.

The code is on GitHub: test-failure-analyzer and tfa-failure-lab. If you've built something similar, or have a failure your team argues about every week, I'd like to hear about it in the comments.