Watching an agent in production
7 min read
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.
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:
- A bug in a tool or the loop. Fix it in code.
- A gap in the instructions. Edit the prompt.
- 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.
What should you record?
A user reports a wrong refund and you cannot see what the agent did.
A trace lets you find the first wrong step. Mask private data and delete old traces on a schedule.
What does a rise in steps per run suggest?
The average number of steps per run doubled this week.
A change in steps, cost or time usually has a cause, such as a new prompt, tool or model version.
Where does a new kind of failure go?
You find a request type that your agent handles badly.
Close the loop. Every failure becomes a fix or a new test.
Your reading position is saved
0 of 20 complete
Lesson complete
Watching an agent in production
Up next