Skip to content

Practice: agents

Three exercises that build on the labs, then one open build task. Do them in your own copy (a fork) of this repo, commit as you go, and keep short notes. Read the labs first: agent/loop.py (run()), agent/state.py, agent/approval.py, agent/checks.py, 05_budgets_and_recovery.py, 06_pause_and_resume.py.

Where your work goes:

  • Code changes: in agent/ and test_agent.py.
  • Your own files: my-work/ in this folder.
  • Notes: my-work/agents.md. For each exercise, a few lines on what you changed, why, and what you would do differently in production.

Some rules below say Decide. Make the choice yourself and write it in your notes. If your team gave you an ASSIGNMENTS.md, it makes those choices for you: follow it.

ex1: Re-check before an approved call runs

Why: an approval can wait for hours. By then the quote may have expired or the credit been used by another order. Lab 06 runs the approved call without looking again.

  1. Add an optional revalidate parameter to run(): an async function (call, state, tools) -> list[str] that returns problems.
  2. On resume, call it for each approved call before running it. Problems: the call does not run, the model gets a tool message {"error": "not run: <problems>"}, and a Step("check", "failed") goes into the trace.
  3. Write recheck_order() for place_order: the quote in state.facts must still be valid today, and check_credit must still approve its total.
  4. No revalidate: works exactly as today.
  5. Decide: when the re-check fails, does the model carry on (and maybe make a new quote), or does the run stop for a person?
  6. Add model-free tests to test_agent.py with a fake model and fake tools: approved, but credit is now short, so place_order is never called.

Done when pytest -q passes, including your new tests.

ex2: Auto-approve small orders

Why: a manager who approves every ₹2,000 order stops reading approvals. Low-risk writes can earn auto-approval under a limit. (If you did the try-it in learn-agents.md, this turns that one-off rule into a reusable approver.)

  1. Let an approver return "approve", "decline" or "pause". True and False keep working as today.
  2. "pause" behaves like approver=None for that call: the state comes back as waiting_approval.
  3. Add limit_approver(limit_inr): place_order for a quote whose total (from state.facts) is at or under the limit is approved; anything else, or an unknown total, pauses.
  4. Decide: the limit, and whether it depends on the customer's tier.
  5. Add model-free tests for each rule.

Done when pytest -q passes, including your new tests.

ex3: A trajectory eval

Why: the right answer reached the wrong way (no credit check before ordering) is a bug waiting to happen. Score the path, not only the answer.

  1. Create my-work/agent_eval.py with a list GOALS of at least 8 cases: {"goal": "...", "must_call": ["create_quote", "check_credit"], "must_not_call": ["place_order"], "status": "done"}. must_call is in order but may have other calls between; status is done, waiting_approval or stopped.
  2. Include a goal that must pause for approval, one that can't be done (an unknown SKU) and one that needs no tool.
  3. Score each run from state.steps: required tools in order, no forbidden tool, the right status, plus tool calls and seconds. Print a line per goal and the overall pass rate. Put the run under if __name__ == "__main__": so importing the file does not call the model.
  4. Add model-free tests for the scorer, with hand-made states.
  5. Change one thing (a line in the system prompt, a budget) and run again.

Done when the script runs and your notes have the pass rate before and after, and two failures with their cause.

Names these exercises build on

Please keep these names in agent/ as they are: run, run_calls, AgentState, Budget, Step, McpTools, check_grounded, describe_call, call_with_retry, and AgentState.save, load, decide. Checks and later topics rely on them.

Build it yourself: a returns agent

No starter code and no steps this time: a short brief, like one you'd get at work. Plan for half a day. Your code goes in my-work/build/; reuse run() and AgentState from agent/. For tools that are plain Python functions, write a small class with the same methods as McpTools (specs, names, needs_approval, retry_safe, call), as ../whatsapp-agent/bot/tools.py does with LocalTools. Notes go in a "Build" section of my-work/agents.md.

The brief. Buyers ask to return or replace part of a delivered order: "12 bags were wet, replace them", "send back the 16 mm bars, wrong size". Build an agent that gathers the facts, checks the return policy and prepares a return request that a person approves.

It must:

  • Have its own fake data and tools: look up an order, read the return policy for a category, and create a return request (a write).
  • Compute any refund or credit amount in a tool, never in the model, and check in code that every ₹ amount and ID in the answer came from a tool result.
  • Never accept more than the quantity delivered minus what was already returned (code, not prompt).
  • Pause the write for approval with a description that shows the lines and the amount, save to a file, and resume in a new process.
  • Have budgets and a stop reason for every run. Reads retry; the write never does.

Decide: the return window per category. When does a request need a photo (a flag on the request is enough)?

Done when:

  • pytest -q my-work/build passes with model-free tests: the policy, the quantity rule, the answer check and the approval path with a fake model.
  • A demo runs three goals on qwen3:8b: a valid return, one past the window, one asking for more than was delivered.
  • A trajectory eval like ex3 (at least 5 goals) runs, and your notes have its pass rate.

Or bring your own problem. Build the same pattern on a problem from your own work instead. Write a five-line brief first (who uses it, what goes in, what comes out, what must never go wrong, how you'll measure it), then meet the same "Done when" list. Use synthetic data only, never real customer data.