Run a tool-calling loop

The core of every agent: read the model's message, run the requested tool, and stop at the final answer.

What to build

A program that plays a scripted model against three tools and reports what happened at each step.

Requirements

  • The tools are add and mul, which take 2 integers, and neg, which takes 1 integer.
  • A reply that is not a JSON object with a tool or an answer key prints step K: error (bad reply).
  • An answer prints step K: answer: TEXT and ends the loop.
  • A tool name that is not listed prints step K: error (unknown tool). Wrong argument count or types print step K: error (bad arguments).
  • A good call prints step K: NAME(ARGS) = RESULT, with the arguments separated by , .
  • After the loop print no answer if no answer came, then tool calls: N, the number of good calls.

Input

Line 1: n. Then n lines, the model's replies in order.

Output

One line per step, then the closing lines.

Constraints

1 ≤ n ≤ 20.

Example

Input:

3
{"tool": "add", "args": [19, 23]}
{"tool": "mul", "args": [42, 2]}
{"answer": "84"}

Output:

step 1: add(19, 23) = 42
step 2: mul(42, 2) = 84
step 3: answer: 84
tool calls: 2

Why it works

The loop trusts nothing: each reply is parsed, then checked for shape, tool name and arguments before any function runs, and a final answer or the end of the script stops it.

Examples

Example 1 · Input
3
{"tool": "add", "args": [19, 23]}
{"tool": "mul", "args": [42, 2]}
{"answer": "84"}
Expected output
step 1: add(19, 23) = 42
step 2: mul(42, 2) = 84
step 3: answer: 84
tool calls: 2
Example 2 · Input
3
hmm
{"tool": "pow", "args": [2, 3]}
{"answer": "I give up"}
Expected output
step 1: error (bad reply)
step 2: error (unknown tool)
step 3: answer: I give up
tool calls: 0

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Parse each reply with `json.loads`; treat text that is not JSON the same as a JSON value that is not an object.

Hint 2

Check in this order: the shape, then `answer`, then the tool name, then the arguments.

Hint 3

Arguments must be a list with the right length whose items are integers but not booleans.

Solution

Show one correct solution (try it yourself first)
import json

n = int(input())
replies = [input() for _ in range(n)]
arity = {"add": 2, "mul": 2, "neg": 1}
calls = 0
finished = False
for step, reply in enumerate(replies, 1):
    try:
        msg = json.loads(reply)
    except json.JSONDecodeError:
        msg = None
    if not isinstance(msg, dict) or ("tool" not in msg and "answer" not in msg):
        print(f"step {step}: error (bad reply)")
    elif "answer" in msg:
        print(f"step {step}: answer: {msg['answer']}")
        finished = True
        break
    elif not isinstance(msg["tool"], str) or msg["tool"] not in arity:
        print(f"step {step}: error (unknown tool)")
    else:
        args = msg.get("args")
        ok = (
            isinstance(args, list)
            and len(args) == arity[msg["tool"]]
            and all(isinstance(a, int) and not isinstance(a, bool) for a in args)
        )
        if not ok:
            print(f"step {step}: error (bad arguments)")
        else:
            if msg["tool"] == "add":
                result = args[0] + args[1]
            elif msg["tool"] == "mul":
                result = args[0] * args[1]
            else:
                result = -args[0]
            calls += 1
            print(f"step {step}: {msg['tool']}({', '.join(str(a) for a in args)}) = {result}")
if not finished:
    print("no answer")
print(f"tool calls: {calls}")

Lesson: Structured output and tool calling →