Notebooks
A
Arize AI
Evaluate Agent

Evaluate Agent

agentsllmsLlamaIndexarize-phoenixopenaitutorialsevalsllmopsai-monitoringaiengineeringprompt-engineeringdatasetsllm-evalai-observabilityllm-evaluationsmolagentslegacyanthropiclangchain

Open In Colab

phoenix logo
Docs | GitHub | Community

Evaluating an Agent

This notebook serves as an end-to-end example of how to trace and evaluate an agent. The example uses a "talk-to-your-data" agent as its example.

The notebook includes:

  • Manually instrumenting an agent using Phoenix decorators
  • Evaluating function calling accuracy using LLM as a Judge
  • Evaluating function calling accuracy by comparing to ground truth
  • Evaluating SQL query generation
  • Evaluating Python code generation
  • Evaluating the path of an agent

Install Dependencies, Import Libraries, Set API Keys

[1]
[2]
[3]

Enable Phoenix Tracing

Sign up for a free instance of Phoenix Cloud to get your API key. If you'd prefer, you can instead self-host Phoenix.

[4]
[ ]

Prepare dataset

Your agent will interact with a local database. Start by loading in that data:

[ ]

Define the tools

Now you can define your agent tools.

Tool 1: Database Lookup

[7]
[ ]

Tool 2: Data Visualization

[9]
[10]
[11]

Tool 3: Data Analysis

[12]
[13]

Tool Schema:

You'll need to pass your tool descriptions into your agent router. The following code allows you to easily do so:

[14]

Agent logic

With the tools defined, you're ready to define the main routing and tool call handling steps of your agent.

[15]
[16]

Run the agent

Your agent is now good to go! Let's try it out with some example questions:

[ ]
[ ]

Agent Traces

Evaluating the agent

So your agent looks like it's working, but how can you measure its performance?

[19]
[ ]
[ ]

Function Calling Evals using LLM as a Judge

This first evaluation will evaluate your agent router choices using another LLM.

It follows a standard pattern:

  1. Export traces from Phoenix
  2. Prepare those exported traces in a dataframe with the correct columns
  3. Use llm_classify to run a standard template across each row of that dataframe and produce an eval label
  4. Upload the results back into Phoenix
[ ]
[ ]
[ ]

You should now see eval labels in Phoenix.

Function Calling Evals

Function Calling Evals using Ground Truth

The above example works, however if you have ground truth labled data, you can use that data to get an even more accurate measure of your router's performance by running an experiments.

Experiments also follow a standard step-by-step process in Phoenix:

  1. Create a dataset of test cases, and optionally, expected outputs
  2. Create a task to run on each test case - usually this is invoking your agent or a specifc step of it
  3. Create evaluator(s) to run on each output of your task
  4. Visualize results in Phoenix
[ ]

For your task, you can simply run just the router call of your agent:

[34]

Your evaluator can also be simple, since you have expected outputs. If you didn't have those expected outputs, you could instead use an LLM as a Judge here, or even basic code:

[35]
[ ]

Tool Evals

The next piece of your agent to evaluate is its tools. Each tool is usually evaluated differently - we've included some examples below. If you need other ideas, Phoenix's built-in evaluators give you an idea of other metrics to use.

Evaluating our SQL generation tool

[ ]
[ ]
[40]
[41]
[ ]

Evaluating our Python code generation tool

[ ]
[45]

In this case, you don't have ground truth data to compare to. Instead you can just use a simple code evaluator: trying to run the generated code and catching any errors.

[46]
[48]
[ ]

Evaluating the agent path and convergence

Finally, the last piece of your agent to evaluate is its path. This is important to evaluate to understand how efficient your agent is in its execution. Does it need to call the same tool multiple times? Does it skip steps it shouldn't, and have to backtrack later? Convergence or path evals can tell you this.

Convergence evals operate slightly differently. The one you'll use below relies on knowing the minimum number of steps taken by the agent for a given type of query. Instead of just running an experiment, you'll run an experiment then after it completes, attach a second evaluator to calculate convergence.

The workflow is as follows:

  1. Create a dataset of the same type of question, phrased different ways each time - the agent should take the same path for each, but you'll often find it doesn't.
  2. Create a task that runs the agent on each question, while tracking the number of steps it takes.
  3. Run the experiment without an evaluator.
  4. Calculate the minimum number of steps taken to complete the task.
  5. Create an evaluator that compares the steps taken of each run against that min step number.
  6. Run this evaluator on your experiment from step 3.
  7. View your results in Phoenix
[ ]
[29]
[30]
[ ]
[ ]
[ ]
[33]
[ ]

Advanced - Combining all the evals into our experiment

As an optional final step, you can combine all the evaluators and experiments above into a single experiment. This requires some more advanced data wrangling, but gives you a single report on your agent's performance.

Build a version of our agent that tracks all the necessary information for evals

[72]
[73]
[ ]
[ ]
[ ]
[ ]
[65]
[ ]
[89]
[ ]

Congratulations! 🎉

You've now evaluated every aspect of your agent. If you've made it this far, you're now an expert in evaluating agent routers, tools, and paths!

Combined Agent Experiments