Module 10 · Lesson 5 · 17 min read

Testing and deploying the agent

Testing and Deployment

Introduction

An agent that works on your machine against cases you tried yourself is not yet ready for users. In this lesson we build a testing pyramid specific to agents, from testing a single tool to testing the full scenario, then we learn how to deploy the agent gradually with monitoring, limits and a rollback plan.

Learning objectives

  • Build a testing pyramid for the agent: tool tests, loop tests with a mock model, and scenario tests
  • Write unit tests for a tool using unittest
  • Prepare a deployment checklist that covers limits, secrets and monitoring
  • Plan a gradual deployment with a rollback plan

The agent testing pyramid

In traditional software we talk about the testing pyramid: many fast unit tests at the base, fewer integration tests in the middle, and a few end-to-end tests at the top. Agents have a similar pyramid:

  1. Tool tests (Unit Tests): each tool is an ordinary function, tested with correct, wrong and edge inputs without any language model. Fast and deterministic.
  2. Loop tests with a mock model: we replace the model with a function that returns pre-written replies, and we make sure the loop executes the right tool, respects the step limit, and stops at approval.
  3. Scenario tests (Scenario Evals): the full evaluation set with the real model, with each case repeated. Slower and more expensive, and run before every release.

The first two layers catch most programming errors quickly and without model cost, so do not leave everything to scenario tests.

Test tasksEval SetRun the agentRunTracingTracesMetricsSuccess · Latency · CostImprove
The evaluation cycle: a fixed set of tasks, a logged run, metrics, then improvement and re-measurement on the same set.

Practical example: unit tests and a loop with a mock model

We test a simple division tool that returns a structured error instead of throwing an exception, like the isolated file tool in the Permissions and isolation lesson, then we test a small agent loop with a mock model. All of this with the standard library unittest:

python
import unittest

def safe_divide(a, b):
    if b == 0:
        return {"isError": True, "message": "Cannot divide by zero"}
    return {"isError": False, "value": a / b}

TOOLS = {"safe_divide": safe_divide}

def run_agent(model, task, max_steps=3):
    calls = []
    for _ in range(max_steps):
        action = model(task, calls)
        if action["type"] == "final":
            return {"answer": action["text"], "calls": calls}
        result = TOOLS[action["tool"]](**action["args"])
        calls.append((action["tool"], result))
    return {"answer": "Stopped at the step limit", "calls": calls}

def looping_model(task, calls):
    # A mock model that repeats the same call endlessly
    return {"type": "tool", "tool": "safe_divide", "args": {"a": 1, "b": 0}}

class AgentTests(unittest.TestCase):
    def test_divide_by_zero_is_structured_error(self):
        self.assertTrue(safe_divide(4, 0)["isError"])

    def test_divide_ok(self):
        self.assertEqual(safe_divide(9, 3)["value"], 3)

    def test_loop_respects_max_steps(self):
        out = run_agent(looping_model, "Divide 1 by 0", max_steps=3)
        self.assertEqual(len(out["calls"]), 3)
        self.assertIn("step limit", out["answer"])

unittest.main(argv=["x"], exit=False)

The third test is the most important: a "badly behaved" mock model that repeats the same tool, and we make sure the loop stops. We do not need to wait for the real model to do this in order to discover the problem.

Pre-deployment checklist

  • Secrets: keys in environment variables or a secrets vault, not in the code or in the instructions.
  • Limits: a step limit, a timeout for each tool, and a cost limit per user.
  • Approval: every irreversible write tool is protected by approval.
  • Tracing: every run produces a trace with sensitive data masked, as in the Tracing and observability lesson.
  • Versions: a version number for the instructions, the tools and the model, so we know exactly what changed when a problem appears.
  • Evaluation: the last run of the evaluation set passed and is documented.

What do we test in the loop?

Besides the step limit, the loop deserves tests for other behaviors with different mock models: a model that asks for a tool that does not exist, where the loop should return a structured error rather than crash; a model that asks for a write tool, where the loop should stop to wait for approval; and a model that returns an empty final answer, which should be treated as a failure, not a success. Each of these behaviors is written once and runs on every later change.

Gradual deployment and monitoring

Do not release the agent to all users at once. A reasonable plan:

  1. Shadow mode: the agent works on real requests, but its answers do not reach the user; the team reviews them.
  2. A small group: a limited share of users or a single department in the university, with a clear button for reporting a wrong answer.
  3. Gradual expansion: only if the metrics remain stable.
  4. A rollback plan (Rollback): the ability to return quickly to the previous version, or to disable a single tool without stopping the whole agent.

After deployment, monitor: the success rate in a reviewed sample, the tool error rate, the average number of steps, the escalation rate, and the daily cost. Any sudden change after a new release is a signal to roll back and investigate.

Summary

The agent is tested with a three-layer pyramid: unit tests for the tools, loop tests with a mock model, and scenario tests with the real model. The mock model reveals loop problems such as endless repetition quickly and at no cost. Before deployment we check the secrets, limits, approvals, tracing, versions and evaluation results. And deployment is gradual, from shadow mode to a small group and then expansion, with continuous monitoring and a ready rollback plan.

Terms

TermEnglishDefinition
Unit testUnit TestTesting a single function or tool in isolation from the rest of the system
Mock modelMock ModelA function that stands in for the language model and returns predefined replies for testing
Shadow modeShadow ModeRunning the system on real requests without delivering its outputs to users
RollbackRollbackQuickly returning to a previous stable version
VersioningVersioningNumbering the instructions, the tools and the model to track changes

Review questions

  1. What are the three layers of the agent testing pyramid? And what does each layer catch?
  2. Why is a mock model useful for testing the step limit?
  3. Name four items from the pre-deployment checklist.
  4. Describe a gradual deployment plan for a support agent at a university.

References