Learn › Agent Harness, the big picture › Stop 3
Stop 3 of 9 · Code
Same job, two ways
One job, written twice: once as ordinary code where we decide every step, and once as an agent where the model does.
The job: register a motor insurance claim. First with ordinary code, then with an agent. Both versions are complete and both run.
Version 1: ordinary code
We decide every step and the order. The caller must hand us three tidy values, the way a web form would.
"""Register a claim with ordinary code. Our code decides every step, in the same order, every time."""from world import create_claim, get_policydef register_claim(policy_id, accident_date, what_happened): policy = get_policy(policy_id) # step 1: always look up the policy if "error" in policy: return "I can't find that policy number." if not policy["active"]: # step 2: always check that it is active return "This policy is not active, so I can't register a claim." if "accident" not in policy["covers"]: # step 3: always check the cover return "This policy does not cover accidents." claim = create_claim(policy_id, accident_date, what_happened) # step 4: always create the claim return f"Your claim {claim['claim_id']} is registered."if __name__ == "__main__": print(register_claim("P-1001", "2026-11-15", "Hit from behind at a signal"))
$ python3 deterministic.py Your claim C-501 is registered.
What it prints.
Version 2: an agent
We write one small loop and a list of tools. The model decides which tool to use next, turn by turn. The caller hands us a sentence.
"""Do the same job with an agent. The model decides the next step; our code runs it and goes round again."""import jsonfrom world import create_claim, get_policyMAX_STEPS = 5SYSTEM = "You are a claims assistant for an insurance company. Use the tools. Answer briefly."TOOLS = {"get_policy": get_policy, "create_claim": create_claim} # what the agent is able to dodef run(task, call_model, log=print): messages = [{"role": "user", "content": task}] for step in range(1, MAX_STEPS + 1): reply = call_model(SYSTEM, list(TOOLS), messages) # 1. ask the model what to do next messages.append({"role": "assistant", "content": reply["content"]}) if reply["stop_reason"] != "tool_use": # 2. no tool asked for: that is the answer return reply["content"][0]["text"] results = [] for block in reply["content"]: # 3. run each tool the model asked for if block["type"] == "tool_use": log(f"step {step}: the model asks for {block['name']}({block['input']})") output = TOOLS[block["name"]](**block["input"]) results.append({"type": "tool_result", "tool_use_id": block["id"], "content": json.dumps(output)}) messages.append({"role": "user", "content": results}) # 4. show the model the results return "I couldn't finish this in time, so I am handing over to a person." # 5. the limitif __name__ == "__main__": from pretend_model import pretend_model print(run("I had a car accident yesterday. My policy is P-1001. Please register my claim.", pretend_model)) print(run("What does my policy P-1001 cover?", pretend_model))
$ python3 agent.py
step 1: the model asks for get_policy({'policy_id': 'P-1001'})
step 2: the model asks for create_claim({'policy_id': 'P-1001', 'accident_date': '2026-11-15', 'what_happened': 'car accident'})
Your claim C-501 is registered.
step 1: the model asks for get_policy({'policy_id': 'P-1001'})
Your policy covers: accident, theft.What it prints. The second request is a question, which version 1 has no code for.
What is different
| Question | Ordinary code | Agent |
|---|---|---|
| Who decides the next step? | We do, when we write the code | The model does, each time round the loop |
| What does the input look like? | Three typed values, like a form | A sentence in the person's own words |
| A request nobody planned for? | Needs a new function ("what does my policy cover?" is not handled) | The same loop handles it, with no new code |
| Can you predict the path? | Yes: the same four steps every time | No: anything from one turn to five |
| How do you test it? | A table of inputs and expected outputs | The same table, plus checks on which tools it used, plus watching real runs |
| What does a run cost? | Almost nothing | One model call for each turn, and each turn sends more text than the last |
| What can go wrong? | A bug you can read in the code | A wrong step, a costly step or an endless loop, and the code looks fine |
| Use it when | You know the steps in advance | The steps depend on what turns up along the way |
Look at what the agent does not have
Line 23 runs whatever tool the model names, with whatever input the model gives, for whoever is asking. Nothing checks any of it. The only protection in the whole file is line 26, the limit of five turns. Stop 5 shows what that costs.
The rest of the files
world.py: the pretend insurance company
"""A tiny pretend insurance company: two policies and a list of claims."""POLICIES = { "P-1001": {"holder": "Rohan", "active": True, "covers": ["accident", "theft"]}, "P-2002": {"holder": "Meera", "active": False, "covers": ["accident"]},}CLAIMS = {}def get_policy(policy_id): return POLICIES.get(policy_id) or {"error": f"No policy called {policy_id}"}def create_claim(policy_id, accident_date, what_happened): claim_id = f"C-{501 + len(CLAIMS)}" CLAIMS[claim_id] = {"policy_id": policy_id, "accident_date": accident_date, "what_happened": what_happened} return {"claim_id": claim_id, "status": "registered"}
pretend_model.py: the stand-in for a real model
So that the example runs with no account and no cost, the model here is a few plain rules that return replies in the same shape a real model does. It shows the loop, not intelligence. Replace it with a call to a real model and agent.py does not change.
"""A pretend model, so the example runs with no account and no cost.It returns replies in the same shape a real model does: a list of content blocks and a stop_reason.A real model reads the words and decides; this one follows three simple rules, which is enough toshow the loop. Replace it with a call to a real model and agent.py does not change."""import jsonimport redef _results(messages): """Every tool result the loop has shown the model so far.""" out = [] for m in messages: if m["role"] == "user" and isinstance(m["content"], list): out += [json.loads(b["content"]) for b in m["content"] if b["type"] == "tool_result"] return outdef _ask(n, name, args): return {"stop_reason": "tool_use", "content": [{"type": "tool_use", "id": f"call_{n}", "name": name, "input": args}]}def _say(text): return {"stop_reason": "end_turn", "content": [{"type": "text", "text": text}]}def pretend_model(system, tools, messages): task = messages[0]["content"] seen = _results(messages) policy_id = re.search(r"P-\d+", task).group() if not seen: # nothing looked up yet: start with the policy return _ask(1, "get_policy", {"policy_id": policy_id}) policy = seen[0] if "error" in policy: return _say("I can't find that policy number.") if "register" not in task: # only a question: answer from the policy return _say(f"Your policy covers: {', '.join(policy['covers'])}.") if not policy["active"]: return _say("This policy is not active, so I can't register a claim.") if len(seen) == 1: # policy is fine: now create the claim return _ask(2, "create_claim", {"policy_id": policy_id, "accident_date": "2026-11-15", "what_happened": "car accident"}) return _say(f"Your claim {seen[1]['claim_id']} is registered.")def stuck_model(system, tools, messages): """A model that never finishes: it asks for the same tool every time.""" return _ask(len(messages), "get_policy", {"policy_id": "P-1001"})
test_both.py: a table of checks for both versions
"""Table-driven checks for both versions. Run: python3 test_both.py"""import worldfrom agent import runfrom deterministic import register_claimfrom pretend_model import pretend_model, stuck_modelDETERMINISTIC = [ # policy, expected reply ("P-1001", "Your claim C-501 is registered."), ("P-2002", "This policy is not active, so I can't register a claim."), ("P-9999", "I can't find that policy number."),]AGENT = [ # task, model, expected reply ("I had a car accident yesterday. My policy is P-1001. Please register my claim.", pretend_model, "Your claim C-501 is registered."), ("Please register my claim on P-2002.", pretend_model, "This policy is not active, so I can't register a claim."), ("What does my policy P-1001 cover?", pretend_model, "Your policy covers: accident, theft."), ("Please register my claim on P-1001.", stuck_model, "I couldn't finish this in time, so I am handing over to a person."),]for policy, expected in DETERMINISTIC: world.CLAIMS.clear() got = register_claim(policy, "2026-11-15", "car accident") assert got == expected, (policy, got)for task, model, expected in AGENT: world.CLAIMS.clear() got = run(task, model, log=lambda line: None) assert got == expected, (task, got)print(f"{len(DETERMINISTIC) + len(AGENT)} cases passed")
$ python3 test_both.py 7 cases passed
Next: stop 4 runs the loop slowly, so you can see every message that crosses between our code and the model.