Cookbook: Improve an agent end to end
Follow one example agent, a lunch-order agent, from first trace to regression test using each part of Judgment.
This cookbook follows one agent through the whole Judgment loop: see what it does, find what goes wrong, measure it, fix it, and keep it fixed. Each section names the Judgment feature involved and links to the guide that covers it. The other cookbooks go deep on one stretch of the loop.
The agent
The lunch-order agent helps teams order lunch to the office. Someone types "order me mediterranean food to feed 30 hungry engineers", and the agent finds restaurants nearby, checks that they can cater a group that size, prices the order, and asks the user to confirm before it places it.
Its Judgment project is called lunch-order-agent.
| Tool | What it does |
|---|---|
get_user_context | Looks up the office address, past orders, and dietary notes. |
search_nearby_restaurants | Finds restaurants by cuisine and distance. |
web_search | Searches the web when the restaurant data is thin. |
get_menu | Fetches a restaurant's menu from a third-party API. |
check_catering_capacity | Checks whether a restaurant can feed the headcount. |
get_reviews | Pulls ratings for a restaurant. |
estimate_order | Prices the order. |
check_delivery_eta | Estimates when the food arrives. |
1. See what it does with tracing
Set up tracing and every run becomes a trace. Here's one, simplified:
- User request: "order me mediterranean food to feed 30 hungry engineers"
- llm call: understand the request (cuisine: mediterranean, headcount: 30)
- tool
get_user_context: office address, past orders, dietary notes - tool
search_nearby_restaurants: 9 mediterranean restaurants within 2 miles - llm call: shortlist restaurants that offer catering (4 left)
- tool
get_menux3: Olive & Thyme and Falafel House return menus, Cedar Grill fails (504 Gateway Timeout) - tool
get_menuretry: Cedar Grill fails again - llm call: drop Cedar Grill and continue with the other two
- tool
check_catering_capacityx2: Olive & Thyme can handle 30 people, Falafel House can't - tool
get_reviews: Olive & Thyme has 4.7 stars - tool
estimate_order: $612 total - tool
check_delivery_eta: 12:15 PM - Response: "I recommend ordering from Olive & Thyme (0.4 mi away): 8 mezze platters, 30 chicken shawarma bowls (6 vegetarian), and 3 sides of baklava. $612 total, delivered by 12:15 PM. Want me to place the order?"
When the user replies "yes, place it", that's a second trace in the same session. Add context to tag traces with the headcount and agent version so you can filter by them later.
The response looks fine, but Cedar Grill was dropped without the user ever knowing. A real trace has far more in it: raw search results, full menus, prompts, token counts. That's what makes traces useful and also hard to read at scale.
2. Find what goes wrong with Issues
Issue detection reads your traces and groups recurring failures. For this agent it finds the get_menu timeouts: a restaurant's menu lookup fails, the agent drops that restaurant, and the user never hears about it. Set up detection with instructions that say what a good run looks like:
This agent orders catered lunch for office teams. A good run ends with a
restaurant that can handle the headcount, an order total under the user's
budget, and a delivery time before the requested time.
Prioritize:
- Tool calls (get_menu, check_catering_capacity, check_delivery_eta) that
fail or time out, and what the agent does next.
- Recommendations that ignore dietary notes from get_user_context.
- Users who have to repeat or correct their request.
Ignore wording and formatting preferences that don't change the order.Root-cause an Issue starts from this Issue and ends with a judge that watches for it.
3. Measure it with judges
A judge turns a question about a trace into a label you can count. Create a judge by describing the question in plain words:
Flag traces where a restaurant's menu lookup failed and the agent dropped it without telling the user.Other judges this agent needs:
- Request completed: Completed or Not completed. Align it against your own calls on traces like the Cedar Grill one, until it agrees with you.
- Order size: Small (under $200), Medium ($200 to $500), Large (over $500), read from the
estimate_ordercall. Each choice is defined so two people would pick the same one. - Jailbreak attempt: Attempted or Not attempted, with a judge action that posts to Slack on Attempted.
To have a judge label live traffic, run it on live traffic. A judge about menu failures can match with a span group: Any single span with Span Name get_menu and Error conditions on the same span.
Alerting alerts on jailbreak attempts, cost and latency, and reuses the root-cause judge to send Failed silently to Slack.
4. Ask questions with the Judgment Agent
Once judges label your traces, the Judgment Agent answers questions about them without re-reading each one. For this agent:
How often were users frustrated with the lunch-order agent last week?
Group it by cuisine and order size, and tell me what caused it.Which tool calls failed most in production this week? For the top one,
show me three traces and say whether the agent recovered.Create a judge that flags traces where get_menu fails and the agent drops
that restaurant without telling the user. Show me the rubric before you save it.Make the answers repeatable:
- Scheduled Tasks run a question on a schedule. For example:
Review traces from the past 24 hours in production. Find restaurants whose get_menu calls failed, how many orders were affected, and whether the agent told the user. Link representative traces. Keep it under 10 bullet points. - Agent Knowledge holds a glossary (what a "headcount" or "catering capacity" means) and skills such as
weekly-lunch-review, which you run with/weekly-lunch-review. - The Slack Agent answers the same questions when someone mentions @Judgment.
- Query with SQL reads the same labels as tables.
Data analysis adds judges for user sentiment, order size, cuisine and completed requests, then asks why users got frustrated last week.
5. Keep it fixed with tests
When you fix the agent, keep the failure from coming back. Create a test suite with a case for each request that broke it, such as order vegetarian lunch for 12 under $200, then run the suite in CI.
- Fix an Issue behind a regression test picks up the same Issue, reproduces it in a test environment, and gates the fix in CI.
- Testing compares
lunch-agent-v1, which ignores budgets, with alunch-agent-v2that respects them.
Where to go next
Use the loop on your own agent: Quickstart sets up tracing, a judge and Issue detection in one pass.