← Lesson
The Agent Loop
Joeven
Run
Reset
Python loads on first run
import json tools = { "add": lambda a, b: a + b, "mul": lambda a, b: a * b, } # A fake model: it "decides" from a script of tool calls. # A real model would read the trace. This script does not. script = [ {"type": "tool", "name": "add", "args": {"a": 3, "b": 4}}, {"type": "tool", "name": "mul", "args": {"a": 7, "b": 10}}, {"type": "final", "text": "The result is 70"}, ] trace = [] for step, decision in enumerate(script, start=1): if decision["type"] == "tool": name = decision["name"] args = decision["args"] result = tools[name](**args) event = {"step": step, "call": name, "args": args, "result": result} trace.append(event) print(json.dumps(event)) else: print("FINAL:", decision["text"]) break print("tool calls in trace:", len(trace)) print("last result:", trace[-1]["result"])
Run to execute this in your browser. Nothing is sent to a server.