kagent

Did My Agent Go Haywire?#

If you're using Agents today, there will be plenty of times where you overestimate or underestimate the performance quality and output of what your Agent is doing. One day, it could build you the most flush feature or fix a problem in the most effective way possible and the next day, it fumbles on a task that appears too obvious.

That's why knowing how your Agent will perform, both live, after the fact, and in the future is a key planning need for everyone and anyone using agentic workflows.

In this blog, you'll learn how to test, evaluate, and understand your Agent with kagent + Agent Substrate and agentevals.

Prereqs#

To follow along with this blog post in a hands-on fashion, you will need the following:

  1. A Kubernetes cluster running k8s v1.37 or above.
  2. Everything that's running/deployed in the Instrumenting Agentic Observability in Kagent + Agent Substrate blog post, which you can find here.
  3. Clone down this repo as it's where the manifests live for certain commands in this guide.

Did My Agent Do Good?#

The reality any time you run an Agent is that you realistically have no idea whether or not it's going to do what it was supposed to. Sure, you can have tools, Agent Skills, and specialized prompts, which of course help significantly, but you still can't measure what the output of an Agent will be by default.

A great example of this is when a new model comes out. After the release of a new LLM, a lot of people say "X model now doesn't work as good", and that's largely because what frontier providers do is allocate the hardware resources from the old model to the new model so it performs better, which means worse inference for the old model as it's not utilizing the same amount of hardware under load.

New model ships, why does the old one feel worse? Before the release, Model X gets all 10 GPUs and responds fast. After the release, the hardware is reallocated: the new model gets 8 GPUs and the old Model X serves the same user requests on 2 GPUs, so requests queue up. Same model + same load + fewer GPUs = worse inference.

The "how" behind an Agent performing as expected has many variables as well.

  1. Was the same LLM used?
  2. Was the Agent run at peak load time for an LLM?
  3. Was caching involved?
  4. Were there tools or Agent Skills used?
  5. Was the same prompt used?

And as mentioned above, you don't know if the smallest change will break things. That's why evaluators are so important because without them, you can never really, truly trust your Agent.

The Goal#

For the hands-on portion (and the theoretical portion above) of this article, we want to prove out how evaluations can help us in our engineering work with Agents. We want to know how evaluating how an Agent works can help us trust our Agents more and also get better outcomes.

Here is what things will look like:

  1. Baseline: an Agent answers a fixed set of cluster questions. This will be done with a golden eval set.
  2. Score it: Run a deterministic check (tool_trajectory_avg_score), LLM as a judge (final_response_match_v2) and a rubric, so you can see both kinds of scoring.
  3. Make a realistic change: Switch to a cheaper model.
  4. Re-run the same prompts: See if the scores drop or if it's the same.
  5. See the same results in Grafana: Every score event in Loki carries the trace ID of the turn it judged, which you open in Tempo, and agent latency by service_version (the revision) shows what the change did to speed.

Before the next section, ensure to forward traffic to the following UIs:

kubectl port-forward -n kagent svc/kagent-controller 8083:8083 # kagent CLI
kubectl port-forward -n agentevals svc/agentevals 8001:8001 # agentevals UI and API
kubectl port-forward -n telemetry svc/kube-prometheus-stack-grafana 3000:80 # Grafana
kubectl port-forward -n kagent svc/kagent-ui 8081:8080 # kagent

This is so you can access agentevals, the kagent UI, Grafana, and the kagent Controller is port-forwarded if you want to run Agent turns/calls programmatically instead of on the UI.

Deploying An Agent#

The first step from a hands-on standpoint, before even evaluating an Agent, is to have an Agent up and operational. You'll also want to record a baseline of an Agent run so you have an idea of what's expected or have a trace to turn into a golden eval set.

  1. Set up your terminal for the rest of the guide to run the commands/manifests. Run these from the repo you cloned down via the prereqs section.

    cd kagent-oss/v1/evals
    export AGENT=cluster-helper # the agent under test
    export AE=http://localhost:8001 # agentevals, through its port-forward
    echo "AGENT=$AGENT AE=$AE"
  2. Deploy the new Agent and ModelConfig. For the purposes of this article, OpenAI Models are used, so you will need an OpenAI API key.

    kubectl apply -f - <<EOF
    apiVersion: api.kagent.dev/v1alpha3
    kind: ModelConfig
    metadata:
    name: gpt-62-sol
    namespace: kagent
    spec:
    provider: OpenAI
    model: gpt-6.2-sol
    apiKeySecret: kagent-openai
    apiKeySecretKey: OPENAI_API_KEY
    stream: true
    openAI:
    apiFormat: responses
    ---
    apiVersion: api.kagent.dev/v1alpha3
    kind: Agent
    metadata:
    name: cluster-helper
    namespace: kagent
    spec:
    harnessRef:
    name: kagent-default
    template:
    inline:
    description: Read-only Kubernetes assistant used as the agent under test in the agentevals lab.
    modelConfig:
    name: gpt-62-sol
    systemPrompt: >-
    You are a Kubernetes assistant for this cluster. Use your Kubernetes tools
    (k8s_get_resources, k8s_describe_resource, k8s_get_events, k8s_get_pod_logs) to answer
    questions about the cluster. Don't use bash for cluster questions, because it has no
    access to the cluster. Answer in one or two sentences.
    tools:
    - mcp:
    server:
    kind: RemoteMCPServer
    name: kagent-tool-server
    tools:
    - k8s_get_resources
    - k8s_describe_resource
    - k8s_get_events
    - k8s_get_pod_logs
    EOF
  3. Run the below as agentevals keeps every session it receives, and when you get to the section below for changing models used, it needs to tell the "before and after". The kagent revision is how that's done.

    REV=$(kubectl get agent "$AGENT" -n kagent -o jsonpath='{.status.latestSuccessfulRevision}' | cut -c1-12)
    BASELINE_REV=$REV
    echo "$REV"

Scoring Your Agent#

With the Agent deployed, you can now begin scoring it. There are quite a few ways to score Agents and the categories typically break down into:

  • Trajectory
  • Response
  • Safety
  • Multi-turn
  • Quality
  • Efficiency

For the purposes of this scoring, you will use scoring for the tool trajectory/average scoring, final response quality, and token usage.

  1. Deploy the broken app, which will give your Agent something to troubleshoot/an action to perform so you have something valuable to test against.

    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: Namespace
    metadata:
    name: evals-lab
    ---
    # The Pod needs no network access.
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
    name: default-deny-all
    namespace: evals-lab
    spec:
    podSelector: {}
    policyTypes:
    - Ingress
    - Egress
    ---
    apiVersion: v1
    kind: Pod
    metadata:
    name: checkout
    namespace: evals-lab
    labels:
    app: checkout
    spec:
    restartPolicy: Always
    automountServiceAccountToken: false
    securityContext:
    runAsNonRoot: true
    runAsUser: 65534
    seccompProfile:
    type: RuntimeDefault
    containers:
    - name: checkout
    image: busybox:1.37.0
    command:
    - sh
    - -c
    - |
    echo "checkout service starting"
    echo "loading configuration"
    if [ -z "$DATABASE_URL" ]; then
    echo "ERROR: DATABASE_URL is not set; cannot connect to the orders database"
    exit 1
    fi
    echo "connecting to the orders database"
    while true; do sleep 3600; done
    resources:
    requests:
    cpu: 10m
    memory: 16Mi
    limits:
    cpu: 50m
    memory: 32Mi
    securityContext:
    allowPrivilegeEscalation: false
    readOnlyRootFilesystem: true
    capabilities:
    drop:
    - ALL
    EOF

    The Pod starts, logs why it can't run, and exits, so it ends up in CrashLoopBackOff. The reason that it's failing (DATABASE_URL isn't set) is only in its logs, so the Pod status says it's crashing, but it doesn't say why (that's for the Agent to figure out).

  2. Below, you will see a question within the env var to ask your Agent, which gets asked twice, and each time the question gets asked (the for loop you run below), a new kagent session gets created. The reason why is because one session needs to create the golden eval set and another is the control you score in the next section, Make A Change To Your Agent. The kagent CLI talks to the controller on localhost:8083.

    export QUESTION="The checkout app in the evals-lab namespace isn't working. Find out why."
    for run in 1 2; do
    session=$(kagent agent session create --agent "$AGENT" -o json | jq -r '.session.id')
    echo "== run $run session $session"
    kagent agent invoke --session "$session" -t "$QUESTION"
    done

Each session shows up on agentevals Local Development page as one session card, marked completed a few seconds after the answer.

You'll see an output similar to the below:

Terminal output from the two runs. Each prints its session ID followed by the Agent's answer that the checkout pod in evals-lab is crash-looping because DATABASE_URL is not set, and the two answers are worded differently.

Ironically enough, see how even the output has a different explanation?

Sidenote: Even though you'll be scoring the Agent against the change (in the next section), it's still good to have two turns/sessions for GPT 6.2 Sol because if you wanted to (it's optional), you can see how much the Agent varies on its own by evaluating them against each other with the same exact Agent configs (to the point above about seeing the output difference).

Make A Change To Your Agent#

In the two previous sections, you deployed an Agent, got a baseline, and scored the Agent. Now, let's see what happens when we change something fundamental for the Agent - the LLM that's used. The LLM that was used originally is GPT 6.2 Sol. Let's change it to gpt-4.1-mini and see what the output will now be. The goal is to prove out:

  • On the three single-tool lookups, gpt-4.1-mini calls the same tools and gives answers as good as GPT-6.2 Sol, within the control run's normal variation.
  • On diagnose-checkout, a smaller model is more likely to skip GPT 6.2 Sol's steps, such as reading the logs, and miss the real cause. Agentevals shows the exact turn and step where it fell short, so "is the cheaper model good enough?" gets an answer for each kind of task.
  • Tokens and latency for each revision, from agentevals and from Grafana by service_version, show what the downgrade buys. Every score links back to the exact trace that explains it, so the decision is quality against cost with actual data backing it.
  1. Deploy the change, which is swapping out the GPT 6.2 model with the gpt 4.1 Mini model

    kubectl apply -f - <<EOF
    apiVersion: api.kagent.dev/v1alpha3
    kind: ModelConfig
    metadata:
    name: gpt-41-mini
    namespace: kagent
    spec:
    provider: OpenAI
    model: gpt-4.1-mini
    apiKeySecret: kagent-openai
    apiKeySecretKey: OPENAI_API_KEY
    stream: true
    ---
    apiVersion: api.kagent.dev/v1alpha3
    kind: Agent
    metadata:
    name: cluster-helper
    namespace: kagent
    spec:
    harnessRef:
    name: kagent-default
    template:
    inline:
    description: Read-only Kubernetes assistant used as the agent under test in the agentevals lab.
    modelConfig:
    name: gpt-41-mini
    systemPrompt: >-
    You are a Kubernetes assistant for this cluster. Use your Kubernetes tools
    (k8s_get_resources, k8s_describe_resource, k8s_get_events, k8s_get_pod_logs) to answer
    questions about the cluster. Don't use bash for cluster questions, because it has no
    access to the cluster. Answer in one or two sentences.
    tools:
    - mcp:
    server:
    kind: RemoteMCPServer
    name: kagent-tool-server
    tools:
    - k8s_get_resources
    - k8s_describe_resource
    - k8s_get_events
    - k8s_get_pod_logs
    EOF
  2. Ask the same question as the previous section, but this time, only ask it once (this is what we'll be scoring against).

    session=$(kagent agent session create --agent "$AGENT" -o json | jq -r '.session.id')
    echo "== session $session"
    kagent agent invoke --session "$session" -t "$QUESTION"
  3. Open the agentevals UI via 127.0.0.1:8001 and go to the Local Development dashboard. You'll see 2-3 runs in there.

  4. Click the Set as EvalSet button for the session that has GPT-6.2-SOL as that will be the golden eval set.

    The agentevals Local Development page showing a GPT-4.1-MINI session with 17,091 tokens and a GPT-6.2-SOL session with 13,821 tokens, each with a Set as EvalSet button.
  5. Click the purple Continue to Evaluation button.

    The purple Continue to Evaluation button in agentevals.
  6. Ensure tool_trajectory_avg_score and final_response_match_v2 are chosen for the scoring. Then, scroll down to Evaluation Settings, have the model as gpt-4o, and click the purple Run Evaluation button.

    The agentevals Evaluation Settings: the judge model set to openai/gpt-4o, a pass threshold of 0.8, the trajectory match type set to EXACT (tools must match in exact order), and the purple Run Evaluation button.

You can now see the scoring results.

The agentevals Evaluation Results page: 2 total traces, a 50% pass rate, and a 0.500 average score, with charts comparing latency and token usage across the reference trace and the two evaluated traces.
An agentevals trace detail where tool_trajectory_avg_score failed with a score of 0.00: the expected run made 5 tool calls starting with k8s_get_events, and the actual run made 6 tool calls in a different order.