Papa Programmer
Lesson 20 of 20

Watching an agent in production

7 min read

Optional

Record the trace

A trace is the full record of one run: the goal, each prompt, each tool call and result, the answer and the time and cost of every step. Without it, a user's report such as "it gave me the wrong refund" leaves you guessing.

Record one line per step in a structured format, so that you can search and count later.

python
import json
import time

trace = []

def record(run_id, step, tool, ok, seconds, cost):
	entry = {"run": run_id, "step": step, "tool": tool, "ok": ok, "seconds": seconds, "cost": cost, "at": time.time()}
	trace.append(entry)
	print(json.dumps(entry))

record("r-17", 1, "search_orders", True, 0.8, 0.002)
record("r-17", 2, "refund_order", False, 1.4, 0.003)
failed = [entry["tool"] for entry in trace if not entry["ok"]]
print("failed:", failed)

The last line prints failed: ['refund_order']. Because every step is a record, you can answer questions such as "which tool fails most?" with one line of code.

Be careful what you record. Traces hold user messages, so strip or mask private data, and delete old traces on a schedule.

Four numbers to watch

Track these over time and look for changes.

  • Success rate. How many runs reach a good result? Use user ratings, an automatic check or both.
  • Steps per run. A rise often means the agent is wandering or looping.
  • Cost per run. A rise often means a longer history or a new failure.
  • Time per run. Users notice delay before they notice most errors.

A sudden change usually has a cause you can find: a new model version, an edited prompt, a changed tool or a different mix of requests.

Read real runs

Numbers show that something moved. Reading runs shows why. Once a week, open about ten traces. Pick some at random, some with low ratings and some that hit a limit. Look for the first wrong step in each one, as in the earlier Lesson.

Each finding goes to one of three places:

  1. A bug in a tool or the loop. Fix it in code.
  2. A gap in the instructions. Edit the prompt.
  3. A new kind of request. Add it to your Evaluation setA list of tasks, each with a way to check the result, that you run again after every change to an agent. You measure how often the agent gets each task right. so that the agent is tested on it from now on.

Keep a way to stop it

Plan for the day something goes badly wrong. Keep a switch that disables the agent or removes its risky tools without a new release. Decide who can flip it and how they will know. Afterwards, write down what happened and what check would have caught it, and add that check. This habit is what makes an agent better after every release.

Record every stepSave a structured trace of each run, with private data masked.
Watch four numbersTrack success, steps, cost and time, and look into any sudden change.
Close the loopTurn each failure into a fix or a new test case, and keep a switch to stop the agent.

Your reading position is saved

0%

0 of 20 complete

Contents

Learning tools