Agent must not loop on the same tool call
`run_agent(respond, execute_tool, *, max_steps=4, fallback="...")` drives the
"model → tool → model" loop until it gets a final answer.
`respond(messages)` returns an action:
- `{"type": "final", "text": "..."}` — the answer is ready;
- `{"type": "tool", "name": "search", "args": {"q": "..."}}` — a tool call.
`messages` starts empty. After a successful call an observation is appended to
it:
```python
{"role": "tool", "name": "search", "args": {"q": "..."}, "content": <result>}
```
If the tool raises `ToolError`, the observation carries an `error` string
instead of `content`, and the loop continues. Other exceptions are not caught.
Rules:
- `max_steps` caps the model turns, the final one included; once the budget is
gone, return `fallback`;
- a repeated call with the same name **and the same arguments** returns
`fallback` immediately — the tool is not executed again;
- the same tool with different arguments is not a loop;
- argument comparison does not depend on key order;
- a malformed action (not a dict, unknown `type`, empty name, non-dict `args`,
empty `text`) raises `AgentError`;
- `max_steps < 1` raises `ValueError`.
Write tests: immediate final answer, one tool call then final, step limit,
looping, different arguments, recovery after `ToolError`, repeat of a failed
call, and malformed actions.
The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.
Log in and askHint 1
What separates "the model is exploring" from "the model is stuck", when both
look like a repeated tool call?