# How to build evals for an agent, starting with its traces

Canonical HTML URL: https://www.zenml.io/evals/how-to-build-evals-for-an-agent

A step-by-step guide to building evals for an AI agent from its traces: design cases, read traces, name failure modes, write evaluators, and test them.

Note: this is a repo-owned Markdown mirror for agents and readers. It omits visual-only UI, decorative media, navigation, analytics, and other browser chrome.

- **Author:** Hamza Tahir
- **Published:** 2026-11-23
- **Tags:** kitaru, agents, evaluation
- **Reading time:** 18 mins

---

**TL;DR:** This guide walks through building evals for a working agent, from its first trace to an evaluator you can trust. I set up a [small expense review agent built using LangGraph](https://github.com/zenml-io/expense-policy-review) and the traces it produced. In this guide, I show which requests to test, what I noticed when I read the traces, and how I turned those observations into evals. But the primary goal of the article is to explain how to find out whether your eval itself is wrong.

If you're a visual learner, go through [this interactive walkthrough](https://www.zenml.io/artifacts/evals-walkthrough) instead.

Recently, this [tweet](https://x.com/htahir111/status/2122374847804461527) of mine took off. So obviously there are a lot of people who care (as I do) about writing good evals for their agents. We’re working on that bigger series soon, but I still wanted to get something out this week, as I have been spending way too much time with evals since starting [Kitaru](https://www.zenml.io/product/kitaru).

[![Post on X by Hamza Tahir announcing a series on how to do evals and asking who is interested, with 39K views and 1.1K likes](https://assets.zenml.io/content/evals/how-to-build-evals-for-an-agent/df6fd1ca/evals-series-tweet.avif)](https://x.com/htahir111/status/2122374847804461527)

Most teams building agents have evals running. Usually that’s an LLM judge scoring replies from 1 to 5, or a checklist asking whether the agent called the right tools. The scores look fine, and users still get wrong answers.

The problem starts with where those evals come from. They get written before anyone reads what the agent actually did, so they test what was easy to imagine and miss what breaks. Few teams then check whether the eval itself is right.

To illustrate the flow, I built a small expense-review agent, read every trace it produced, and turned the failures I found into evals. I show how I tested those evals (two of them were wrong), and how I replay the agent after a fix to see what it fixed and what it broke.

## Would your evals catch issues in your agent?

What is the first thing you would look at to know whether the agent you built is working accurately in production?

To answer this, I created an expense review agent that reviews employee expenses against the company’s written policy and recommends actions like approving, denying, or sending something for human review.

Let me show this with one request the agent got: an employee submits a $320 hotel bill for one night in Port Selene. Your policy allows $250 a night in a standard city and $350 a night in Port Selene. Your agent sends the claim to someone for review because the amount check flagged a $250 cap.

The night is under the Port Selene cap, so the flag is wrong. You wouldn’t have known that before looking into the trace. What would you check then?

![Annotated Kitaru session trace for the Port Selene claim: the expense lookup returns one night at $320, the policy search returns the $350 Port Selene lodging rule, check\_amount\_limit returns the standard $250 cap, and the agent flags a stay that was within policy](https://assets.zenml.io/content/evals/how-to-build-evals-for-an-agent/d68b1a90/port-selene-trace-annotated.avif)

- **Did it call the right tools?** Yes. It looked up the expense and searched the policy.
- **Did it retrieve the right policy?** Yes, the lodging section.
- **Did it cite a real section?** Yes, by its ID.
- **Did the employee get a good reply?** Yes, the reply included both the decision and a reason.

Every one of the above checks passes, but a valid $320 claim still lands in a reviewer’s queue.

So where do you start with your agent? The answer is evals.

## What is an eval?

An eval is a **repeatable** check of **one behavior** you care about, with evidence for deciding whether it passed.

**Repeatable** means you can run the same check on the same cases after changing the prompt, a tool, or the model.

**One behavior** means you can say exactly what you’re checking. For example: *When the agent said it flagged an expense, did that expense reach the reviewer’s queue?*

**Evidence** is the record someone can inspect to verify the answer.

I’ve seen people use **evals** and **LLM-as-a-judge** interchangeably.

Building evals is a whole process that starts with finding where your agent goes wrong and testing whether your checks catch those failures. Once you trust a check, you can then use it to verify a fix and catch a regression. This helps you decide if a change is ready to ship.

Here are the steps I follow when building evals. Steps 1 and 2 happen once. The rest repeat every time you change the agent, because each rerun gives you new traces to read.

![Diagram of the seven steps for building evals from traces: define the job and design cases happen once, then capture and read traces, identify failure modes, choose an evaluator, test the evaluator, and change and rerun repeat for every change](https://assets.zenml.io/content/evals/how-to-build-evals-for-an-agent/7889b254/eval-loop-seven-steps.avif)

1. **Define the job:** Who relies on the agent, and what does it need to do for each of them?
2. **Design cases:** Decide which situations your agent should handle, then write synthetic requests that cover them.
3. **Capture and read traces:** Record each run, then read the traces from the request through to the outcome.
4. **Identify failure modes:** Note what went wrong in each trace, then group similar mistakes.
5. **Choose an evaluator for each failure mode:** Use a code-based evaluator when you can check the evidence directly. Use an LLM judge when the check needs judgment.
6. **Test the evaluator:** Check that it fails a trace you know is wrong and passes one you know is right.
7. **Change the agent and rerun the cases:** Check which failures the change fixed and what it broke.

Now I will go into the details of these steps.

**Note:** I will use a single LangGraph agent running `gpt-5-nano` with five tools. It reviews one expense at a time against a ten-section policy and recommends approval, denial, or human review. The code is in [zenml-io/expense-policy-review](https://github.com/zenml-io/expense-policy-review).

## Step 1. Define the job

Before you start checking an agent, write down who relies on it and how the agent can help those users. Getting the right answer is one of several obligations for most agents, and often the easiest one to check. The expense agent serves three parties.

| Who relies on the agent | What they need                                          | What to check in the trace                                          |
| ----------------------- | ------------------------------------------------------- | ------------------------------------------------------------------- |
| The employee            | A decision and an accurate reason                       | Compare the reply with the expense record and the applicable policy |
| The finance team        | A saved decision for the right expense                  | A `recommend_decision` call and its receipt                         |
| The human reviewer      | The flagged expense and a clear reason for reviewing it | A `flag_for_review` call and the handoff reason                     |

Here is the path one expense review takes through the agent and its five tools:

![Diagram of the expense-review agent: an employee request passes through a LangGraph agent with five tools (get\_expense\_details, search\_policy, check\_amount\_limit, recommend\_decision, flag\_for\_review), and the employee, the finance team and the human reviewer each rely on a different output](https://assets.zenml.io/content/evals/how-to-build-evals-for-an-agent/21ded9b7/expense-agent-path.avif)

The agent's instructions ask for `recommend_decision` exactly once per review, and `flag_for_review` as well when the decision is a flag. The agent can still skip or repeat those calls. When it does, the employee gets a reply while finance gets no matching record. That happened in 23 of my 126 runs.

For your agent. List the people and systems downstream of it. For each, name one outcome you could point to in a log. If an obligation leaves no evidence, the first fix is to make the agent leave some.

## Step 2. Design cases before you write queries

If your agent isn’t in production yet, you won’t have user traces to learn from. Running it on synthetic queries lets you spot problems before users encounter them.

I have seen teams make the mistake of generating all 100-150 synthetic queries in one go using a coding agent. But if you review the queries closely, you will find lots of similar queries covering the same case, just with different wording, and the edge cases are missing.

Therefore, start by deciding the dimensions instead. A **dimension** is a detail you deliberately vary across cases. For my expense-review agent, some dimensions come from the person making the request, some from the policy, and some from what the agent can do with its tools.

A coding agent can help with this in two passes.

**In the first pass**, give it the agent’s instructions, policy, and tool descriptions, and ask it to propose dimensions and example values. Review the list yourself before moving on. Does it cover the easy cases? What happens when a lookup returns nothing? Have you included an approval from the wrong person as well as an approval that’s missing altogether?

![Table of six dimensions for designing eval cases for the expense agent, with examples: who is asking, expense and policy rule, available evidence, capability and tool coverage, difficulty, and ambiguity](https://assets.zenml.io/content/evals/how-to-build-evals-for-an-agent/dba82af9/eval-case-dimensions.avif)

Once the dimensions look right, combine a few values to build a scenario.

For example, employee + hotel stay + missing receipt could become: **“I lost the receipt for my hotel stay. Can you check whether I can still claim it?”**

**In the second pass**, ask the coding agent to write a request for each combination you choose. Review the set for duplicates, gaps, and anything a user would be unlikely to say.

Once the generation is done, you will have 100-150 user queries covering different scenarios. A small agent like this one needs fewer, so I went ahead with 42 requests. Then run the agent on each query to generate traces.

## Step 3. Capture and read traces

A trace is a step-by-step record of everything your agent did to handle one request. It shows what the user asked, every call the agent made to the model and to its tools, what each call returned, and the reply the user got.

When an agent gives the wrong answer, the reply doesn’t tell much about where things went wrong.

You have to drill down to determine whether the lookup returned the wrong record, the policy search missed the relevant rule, or the agent had the right information but still gave an incorrect answer.

To tell those cases apart, capture:

- the user’s request and the final reply
- each tool call in order, including its arguments, result, and any error
- each model call, including its input and output, model name, token usage, and latency
- the details and result of any action the agent claims to have taken

I sent traces to Langfuse with its LangChain callback handler. You can use LangSmith, Braintrust, or whichever observability tool your team already has.

Before running the full batch, open one trace and check that you can follow the request all the way through.

### Which traces should you read?

For a deliberately designed set of 100-150 synthetic requests, I’d read every trace. Each case is there for a reason. I had 42 requests, ran each one three times, and read all 126 traces. A broader agent needs more.

For a production agent that might get 1k-10k requests a day, a practical starting point is to randomly sample 150-200 traces from the past seven days and review those closely. If you’re investigating a rare failure, pull those cases separately and do targeted sampling because a random sample can miss them.

### How should you read a trace?

Start with what the user asked and what they were told. Then follow the calls in order, checking what information the agent had at each step and whether the promised action happened. In the Port Selene trace, the lookup shows one night at $320 (1), the policy search returns the lodging rule with its $350 Port Selene cap (2), and the amount check returns the standard $250 cap (3). Read together, the steps show the agent had the right record and the right rule, trusted the tool’s cap, and flagged a valid stay (4). That's the kind of observation to note on each trace. In the next step, you group them.

![Animated Kitaru session view stepping through one trace in order: the expense record, the policy search, then the amount limit check](https://assets.zenml.io/content/evals/how-to-build-evals-for-an-agent/79fb33e9/reading-a-trace.gif)

## Step 4. Identify failure modes

As you read each trace, leave a short note where you spot a mistake.

`Wrong answer` is an easy tag to add to a trace, but it tells you little about what to fix. The agent might have missed the relevant policy, overlooked a fact in the record, or applied the right rule incorrectly. *Error analysis* helps you figure out which of those happened and which mistakes keep coming up.

1. **Read one trace at a time:** Start with the traces you selected in Step 3 and follow each request through to the outcome.

2. **Leave a short note where you spot a mistake:** Write down what the agent did and the evidence that shows why it was wrong. For the hotel claim, a note on the flag could read: *The agent flagged the $320 Port Selene stay. The amount check returned the standard $250 cap, but the policy allows $350 a night in Port Selene.*

   ![Animated Kitaru session view where a note is written on the check\_amount\_limit step that returned the wrong cap](https://assets.zenml.io/content/evals/how-to-build-evals-for-an-agent/ec00f91a/note-on-a-step.gif)

3. **Group similar notes, then name the pattern:** Another trace in my set was a $240 client dinner for two people. The agent sent it for review against the $150 per-attendee limit, even though each share was $120. Looking at the hotel and dinner notes together gave me a specific *failure mode*: **the agent applied a limit that did not fit the case, such as the wrong city’s cap or a per-person cap read as a total.**

Some traces got several notes, and some notes were about problems other than the decision. To count wrong decisions, I compared each run’s decision with its answer key. The agent got 9 of the 126 runs wrong, across 7 of the 42 requests. Grouping those 9 by their main issue gave me this breakdown:

| What went wrong                         | Runs | One example                                                                                                           |
| --------------------------------------- | ---- | --------------------------------------------------------------------------------------------------------------------- |
| Sent a clear violation for human review | 5    | It said a solo $120 dinner was over the $75 meal cap, then asked a reviewer to decide. It did this in all three runs. |
| Used the wrong limit or unit            | 2    | It checked a Port Selene night against the standard $250 cap instead of $350.                                         |
| Approved a claim it should have stopped | 2    | It approved a dinner the employee had split across two claims to stay under the cap.                                  |

I filed each wrong decision under its main issue, so the three rows add up to 9. In your own analysis, one trace can show more than one failure mode, so count it once in each.

One failure isn’t in this table because it isn’t a wrong decision. In 23 of the 126 runs, the reply told the employee the decision, but the `recommend_decision` receipt was missing (21 runs) or said something different (2 runs). The employee had an answer and finance had nothing to act on. I treat that as its own failure mode, separate from the 9 wrong decisions.

For your agent, read, note, group, and count before you pick any metrics. A handful of failure modes with counts is the most useful artifact in the whole process, because it tells you what to fix first and what to check for.

## Step 5. Choose an evaluator for each failure mode

Take your failure modes one at a time and ask: what in the trace would tell you whether this happened? The answer will decide what kind of evaluator you might need. Take the 23 runs from Step 4 where the employee got a decision and finance got no matching record.

The `recommend_decision` tool returns a confirmation with the expense ID and the decision it saved, so a few lines of code can check this on every trace without calling a model.

That gives you two kinds of evaluators:

1. **Use a code-based evaluator when the trace holds evidence you can check directly.** That might be an `if` check on a tool’s status, a regex that pulls out a policy ID, or a comparison against a latency limit. Run it twice on the same trace, and you get the same result.
2. **Use an LLM judge when the answer needs judgment.** Whether an explanation would mislead the employee is a good example. No field in the trace says so, so a model has to read the request, the record, the policy, and the reply, then decide. A judge can also point to the claim and the evidence behind its verdict. Before you trust it, label some traces yourself and check that its verdicts agree with yours.

| Failure or condition to catch                                           | How to check it                                                                                                                                                                                                                                                                           | Evaluator type                                                                                 |
| ----------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| The employee got the wrong decision by the agent                        | Compare the decision in the reply with the case’s answer key.                                                                                                                                                                                                                             | Code, comparing the extracted decision with the answer key.                                    |
| Finance has no matching decision record                                 | Check that `recommend_decision` completed exactly once and that its receipt contains the right expense ID and decision.                                                                                                                                                                   | Code using counts and field comparisons                                                        |
| The reply omits a citation or cites a section the agent never retrieved | Extract IDs such as `POL-LODGING-01` with a regex. Check that at least one is present and that each appears in the policy search results.                                                                                                                                                 | Code using regex and set membership                                                            |
| The agent applied a limit that does not fit the case                    | Find the cap the policy sets for this expense (the city’s nightly rate for lodging, the per-attendee rate for meals). Fail the trace if the amount check returned a different cap, or if the reply says the claim is over the limit when the per-night or per-person amount is within it. | Code, using the policy’s caps and the arithmetic. My first attempt used regex for the wording. |
| The employee gets a misleading explanation                              | Give the judge the request, record, policy, and reply. Ask which claims are unsupported and what evidence contradicts them.                                                                                                                                                               | LLM judge                                                                                      |
| A request exceeds the team’s latency budget                             | Compare the root span’s duration with the agreed limit. This is a performance check you could add.                                                                                                                                                                                        | Code using a threshold                                                                         |

The rule of thumb: use the simplest evaluator that can settle the question. Prefer a code check over an LLM judge whenever the trace has the evidence. A code check costs almost nothing, so you can run it on every trace after every change. It also gives the same answer every time. A judge costs a model call per trace, and you have to check its verdicts against human labels before you trust them.

Here is the second row in code, cut down to the decision check. It counts the `recommend_decision` calls and compares the saved decision with the one in the reply.

I ran the expense evaluators locally in [Kitaru](https://www.zenml.io/product/kitaru). I exported the 126 traces from Langfuse and imported them into Kitaru, which stores each trace as a session. In Kitaru, an evaluator is a versioned Python function that receives one whole session.

![Animated Kitaru view of importing the Langfuse traces, where each trace becomes a session with its evaluator verdicts beside it](https://assets.zenml.io/content/evals/how-to-build-evals-for-an-agent/cc03a841/import-from-langfuse.gif)

```python
import re
from kitaru.task.evaluator import EvaluationResult, SessionView

def evaluate(session: SessionView, **params) -> list[EvaluationResult]:
    reply = re.search(r"decision:\s*(approve|deny|flag_for_review)", str(session.session.outputs), re.I)
    reply = reply.group(1).lower() if reply else None
    saved = [
        node.inputs.get("decision")
        for node in session.nodes
        if node.node_type == "tool_call" and node.tool_name == "recommend_decision"
    ]
    return [
        EvaluationResult(
            name="decision_saved_once",
            score=len(saved) == 1,
            passed=len(saved) == 1,
            explanation=f"recommend_decision ran {len(saved)} time(s).",
        ),
        EvaluationResult(
            name="saved_matches_reply",
            score=saved == [reply],
            passed=saved == [reply],
            explanation=f"Reply says {reply}; the log holds {saved or 'nothing'}.",
        ),
    ]
```

This check runs on the trace alone. It pulls the decision from the reply, finds what `recommend_decision` saved, and compares the two. 103 of 126 runs passed. In the other 23, finance never got a record that matched what the employee was told: 21 runs never saved a decision, and 2 saved a different one. Saving a decision always leaves a receipt in the trace, so a few lines of code can catch this on every run.

![Animated Kitaru evaluations view showing the decision-record evaluator's pass and fail verdicts per session, including a run that answered the employee but never saved a decision](https://assets.zenml.io/content/evals/how-to-build-evals-for-an-agent/cae6119d/evaluator-verdicts.gif)

## Step 6. Test the evaluator

How do you know your eval works?

Let’s say you ask an LLM to rate each reply from 1 to 5. Scores start coming in, and it feels like you have evals. Then the average goes up after a change. Did the agent get better? You can only tell if the evaluator recognizes the failure you care about.

But a wrong evaluator is worse than none, because it tells you a fix worked when it did not. I found problems in two of mine, and how I found them is the most reusable part of this post.

For a specific failure mode, I’d use **pass or fail** and spend the effort on the rubric. For my explanation judge, the question was: *Would this reason mislead the employee about why the claim was approved, denied, or flagged?*

- **Pass** if the expense record and policy support the material claims in the reason. A short, accurate explanation can pass.
- **Fail** if the reason gets a material fact or rule wrong, or leaves out something that would change what the employee should do next. Ask the judge to point to the claim and the evidence behind its verdict.

Then test whether the judge applies that rubric the way a person would. Have someone who knows the policy label traces before seeing the judge’s answers. Use disagreements to refine the rubric. Once you’ve finished tuning, run the judge on a separate set of human-labeled traces and report two numbers.

First, of the failures your reviewers found, how many did the judge catch?

Second, of the replies your reviewers accepted, how many did the judge pass?

You need both, because a judge that passes everything gets the second number perfect while missing every failure.

Read these two posts for more details: [Hamel Husain and Shreya Shankar on binary scoring](https://hamel.dev/blog/posts/evals-faq/why-do-you-recommend-binary-passfail-evaluations-instead-of-1-5-ratings-likert-scales.html) and [Hamel’s guide to validating an LLM judge](https://hamel.dev/blog/posts/llm-judge/).

**In your agent,** for every evaluator, keep four small tests:

- a trace it should fail
- a trace it should pass
- a paraphrase of each
- a negation

If the evaluator is a model, add human labels and read its reasoning on the cases where you disagree.

## Step 7. Change the agent and rerun

Everything so far measured the agent as it is. The payoff for creating evals comes when you change anything in the agent.

I’d recommend changing one thing at a time. Traces point to a specific cause for the hotel failure: the amount check tool applies one cap to the total, with no unit and no Port Selene rate. So the change is to the tool. It should return the cap, the unit it applies to, and the per-unit amount. The prompt and the model stay as they are.

Also state what should happen before you run it. For the hotel family, a correct fix produces exactly this:

| Case | Memo                                     | Before              | Should become      |
| ---- | ---------------------------------------- | ------------------- | ------------------ |
| q10  | Three nights at $200 each                | Approve             | Approve, unchanged |
| q22  | One night at exactly $250, standard city | Approve             | Approve, unchanged |
| q23  | One night in Port Selene at $320         | Flag in 1 of 3 runs | Approve            |

q22 is the important row. It sits exactly on the standard cap, so it catches a fix that gets the boundary wrong. The set has no standard-city night above $250, so a lazy fix such as "raise the lodging cap to $350" would pass every row. That is a case worth adding before you trust the result.

Lastly, you rerun everything and read it per case. On 42 cases, a pass rate going from 50% to 60% can hide five fixes and one new failure. For every case, compare before and after:

- Fail to pass: the change fixed it. Open the trace and confirm it passed for the right reason.
- Pass to pass: behavior preserved.
- Pass to fail: a regression. This is what the rerun is for.
- Fail to fail: not fixed, or the evaluator lacks evidence.

### The practical problem with rerunning an agent

Rerunning a prompt is easy but rerunning an agent is not, for two reasons. Its tools touch the world: rerunning the agent would write decisions and route expenses a second time. And the world moves: a lookup that returned one record last week may return another today, so you cannot tell whether the agent changed or its inputs did.

The answer is to replay against the recording. The agent's real code runs again from the start, and each tool call is answered with the result recorded the first time. The inputs are held still, so any difference in behavior comes from your change.

Kitaru’s replay does this for you. A replay re-runs the agent’s code, which an imported Langfuse trace does not carry, so I recorded the 42 cases once more through Kitaru’s LangGraph adapter and froze them as a named set. A replay runs the changed agent over that set with recorded tool results, and the same evaluators score the original and the replay side by side.

![Animated Kitaru experiment view replaying the changed agent over the same 42 cases, then comparing one case against its recording](https://assets.zenml.io/content/evals/how-to-build-evals-for-an-agent/75cf7655/replay-and-compare.gif)

One detail matters for my fix in particular. I’m changing a tool, so its recorded results are exactly what I do not want replayed. That tool has to run live, while the lookups around it answer from the recording. Deciding this per tool is part of designing the experiment.

![Kitaru experiment per-unit-limit-fix with its tool policy: calls are answered from the recorded baseline session by default, check\_amount\_limit is set to passthrough so it runs live, and evaluator results are listed per check across the 42 sessions](https://assets.zenml.io/content/evals/how-to-build-evals-for-an-agent/9213b1b6/replay-tool-policy.avif)

In my run, five cases flipped from a wrong decision to the right one, and none went the other way. The finance record went the other way: two cases were fixed, and four runs newly answered the employee without saving a decision. Port Selene had already passed in the recorded baseline, so this run does not prove the hotel fix on its own. With one run per case, some of the five fixes may also be the model’s normal variation, so replay each case three times before you trust a flip.

![Kitaru compare view of an original session and its replay: decision\_matches\_label goes from fail to pass, while decision\_saved\_once and saved\_matches\_reply go from pass to fail](https://assets.zenml.io/content/evals/how-to-build-evals-for-an-agent/70e855d0/replay-compare-evaluator-results.avif)

For your agent. Before any change, write down which cases should flip and which must not. After it, read the regressions first.

## Do this for your own agent

- Write down who relies on the agent and what evidence each outcome leaves.
- Design 100 to 150 cases from dimensions.
- Capture full traces, including reasoning for actions.
- Read every trace and write a short note about what worked and what didn’t in the respective trace.
- Group the similar notes and get 5-6 failure modes.
- Write one evaluator per failure mode, using code wherever the trace has evidence.
- Give each evaluator a known failure, a known pass, a paraphrase, and a negation.
- For an LLM judge, collect human labels and read its reasoning where you disagree.
- Before changing the agent, list which cases should flip and which must not.
- Replay, then read the regressions first.

What you need to start is traces, time set aside to read them, and the habit of asking how each check could be wrong.
