Skip to content

< all problems10 · Level 05, Agents

Build a Tool-Calling Agent Loop

medium · agent loop · Agents

Implement run_agent(question, model, tools, max_turns=5):

  1. Send the question to the model, offering tools.specs().
  2. Append the reply to the conversation.
  3. If the model didn't ask for a tool, return response.text.
  4. Otherwise run each requested tool and append one user message holding a tool_result block per call, in request order.
  5. An unknown tool is not a crash. Return the error as the tool result with "is_error": True and let the model recover.
  6. Stop at max_turns and return None.