AI ordering agent project · Chapter 3 of 12

The loop

By the end of this chapter you can run an agent from a member's message to an answer: ask the model, run the tools it asks for, send each result back with its call's id, and ask again, with every request carrying the whole conversation. And you can stop a model that never answers. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The loop
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of AI ordering agent project are free. This one comes with the all labs plan.

Why you need this

Chapter 2's tools work, but only by hand. Tara's "Book 2 Jain Thali for Tuesday lunch at 12:30 in Thane" needs three of them in order: find the slot, find the dish, place the order, each step using what the one before found. In the pilot week Bhavna's team did exactly that by hand, 312 times.

The model does one step per request and remembers nothing between requests (chapter 1). So something has to run each tool it asks for, carry the result back, and ask again until it has an answer for Tara. That something is a loop of about fifteen lines, and Kunal wants one more thing from it: it must always end, even when the model does not.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    loop.py: run the tools the model asked for

    Write run_tools(ctx, reply, messages, calls) in agent/loop.py. For each call of reply.calls, in order, it runs result = run_tool(ctx, call.name, call.arguments) and appends {"role": "tool", "tool_call_id": call.id, "content": json.dumps(result, ensure_ascii=False)} to messages and {"name": call.name, "arguments": call.arguments, "result": result} to calls. It returns nothing.

    The Check gives your run_tools a reply with two calls, find_slots and find_dish for Thane, as Tara, and reads the two lists: a tool message per call in order with its id, the content the JSON of the tool's result (slot 80 for the first), and the calls with their names, arguments and results.

  2. Task 2

    loop.py: the loop

    Write run_agent(ctx, messages, tools=None, max_steps=None) in agent/loop.py. Work on a copy of messages and use TOOLS when tools is None. Repeat: reply = read_reply(chat(messages, tools)); append reply.message to the messages; if the reply has no calls, return RunResult("answered", reply.text, messages, calls, the number of model calls made); otherwise run_tools(ctx, reply, messages, calls) and ask again.

    The Check runs Tara's "Book 2 Jain Thali for Tuesday lunch at 12:30 in Thane" through your run_agent and reads every request the lab model received: find_slots, find_dish, then place_order, each request sending everything so far, every call's result sent back, then the answer. It wants the list it gave you unchanged, a RunResult with status answered, the last reply's text and the number of model calls, the messages ending with the answer, every call in calls, and one pending order for Tara.

  3. Task 3 · interview question

    Interview: loop.py, the step limit

    Make run_agent ask the model at most max_steps times (agent_settings.max_steps, 6, when max_steps is None). When they are used up without an answer, append {"role": "assistant", "content": answer} with answer = STEP_LIMIT.format(n=max_steps) and return RunResult("step_limit", answer, messages, calls, max_steps).

    The Check runs your loop with lab-chat-loopy, a model that asks for a tool after every result, with max_steps=3 (exactly 3 requests, then status step_limit, the STEP_LIMIT answer appended), then with agent_settings.max_steps set to 2 (2 requests). Then POST /agent/chat as Tara must answer 200 with status answered.

    Kunal will ask: why a limit on steps, when the loop already stops as soon as the model answers?

What you will learn

  • The tool message: a result sent back, with tool_call_id and the result as text.
  • The order of messages: an assistant message with tool_calls, then one tool message per call.
  • The 400 you get when that order breaks, and the other two the API sends.
  • The loop: model, tools, results, until it answers in words, with the whole conversation resent every time.
  • What each step costs: tokens and seconds, growing with the conversation.
  • A step limit: a loop that ends on its own, tried on lab-chat-loopy.

More in The loop

Build The loop yourself

DabbaDrop members want to order by chat. You build the agent that books their tiffins through DabbaDrop's own API, and asks before it pays. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of AI ordering agent project are free. This one comes with the all labs plan.