TECHNOLOGIESTieInn Technologies

The agent loop, written from scratch

Agents & Tool Calling · 10 questions

Short notes

Key idea

An agent is a loop: the model decides the next step, your code runs the tool, the result goes back into the history, and it repeats until the model gives a final answer or a limit is hit.

Syntax

messages = [user_question]
for step in range(MAX_STEPS):
    reply = model(messages, tools)
    if not reply.tool_calls:
        return reply.text                       # final answer
    messages.append(reply)
    for call in reply.tool_calls:
        result = run_tool(call.name, call.args)  # validate first
        messages.append(tool_result(call, result))
return "Stopped: too many steps."

Example

"Refund order 4471 if it was damaged." Step 1: get_order(4471) → damaged. Step 2: create_refund(4471) → done. Step 3: no tool call, final text "Refund issued for order 4471."

Traps

  • No step limit means a confused agent can loop and burn money.
  • Losing tool results from the history makes the model repeat calls.
  • Every step adds latency and cost; if the steps are always the same, write a fixed workflow.
  • Log each step (call, arguments, result) so failures can be traced.

Want the full lesson with step-by-step examples, a project and a certificate? Agents & Tool Calling course, ₹180 →

Questions

Open in Code Lab5 free questions
5 questions

    The agent loop, written from scratch

  1. Question 1easy

    What turns a single tool call into an agent?

  2. The agent loop, written from scratch

  3. Question 2hard

    Write run_agent(model, question, max_steps) that loops: call model.generate(messages); if the reply has no tool calls return its text; otherwise run each call with TOOLS[call.name](**call.args), appending a {"role": "tool", "name": ..., "content": str(result)} message per call. After max_steps return "gave up". Store run_agent(model, "Price of product 2?", 4) in result.

    Store your answer in result.

  4. The agent loop, written from scratch

  5. Question 3medium

    The model asks for three tools in one reply, then answers. How many tool results are added?

    from practice_llm import ScriptedModel
    model = ScriptedModel([[{"tool": "t", "args": {}}, {"tool": "t", "args": {}}, {"tool": "t", "args": {}}], "ok"])
    messages, results = [{"role": "user", "content": "go"}], 0
    while True:
        reply = model.generate(messages)
        if not reply.tool_calls:
            break
        for call in reply.tool_calls:
            messages.append({"role": "tool", "name": call.name, "content": "done"})
            results += 1
    print(results)
  6. The agent loop, written from scratch

  7. Question 4medium

    The model keeps asking for the same tool. What does this print?

    from practice_llm import ScriptedModel
    model = ScriptedModel([{"tool": "search", "args": {"q": "x"}}] * 10)
    steps = 0
    for _ in range(4):
        reply = model.generate([{"role": "user", "content": "?"}])
        if not reply.tool_calls:
            break
        steps += 1
    print(steps, model.remaining)
  8. The agent loop, written from scratch

  9. Question 5easy

    What should the agent do when it reaches its step limit without an answer?

5 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.