Blog/Learn Agentic AI Development/From One Tool Call to a Bounded Execution Loop

Chapter 08 / 10 min read

From One Tool Call to a Bounded Execution Loop

The five-iteration loop, multiple tool calls, execution history and the limits of a model-driven sequence.

From one tool call to a bounded execution loop

A one-shot tool call is a useful first test. Real tasks may require several interactions: the model requests a tool, receives the result, and then requests another tool or returns a final answer.

1. Why a loop is required

The first version called the model once. That is not enough if the model needs an observation before deciding what to do next.

The basic control flow is:

Code
Send conversation to model
          |
          v
Receive assistant response
          |
          v
Append assistant response to history
          |
          v
Are there tool calls?
      /       \
    yes        no
     |          |
     v          v
Execute tools  Return final answer
Append results Stop
     |
     v
Next model iteration

2. Cap the number of iterations

The source implementation used a maximum of five iterations:

Code
max_iterations = 5
current_iteration = 0

while current_iteration < max_iterations:
    current_iteration += 1
    print(f"Agent current iteration: {current_iteration}")

    response = send_message(messages=messages)
    messages.append(
        response.message.model_dump(exclude_none=True)
    )

    if response.message.tool_calls:
        for tool_call in response.message.tool_calls:
            tool_name = tool_call.function.name
            tool_arguments = tool_call.function.arguments

            result = execute_tool(
                tool_name=tool_name,
                tool_arguments=tool_arguments,
            )

            messages.append({
                "role": "tool",
                "tool_name": tool_name,
                "content": json.dumps(result),
            })
    else:
        print(response.message.content)
        break

The maximum is a guardrail. It prevents a faulty model interaction from continuing forever. In a production application, the loop should also have a clear timeout and token/cost budget.

3. The first multi-operation experiment

The user request was:

Multiply 250 by 40, then divide the result by 5.

The observed first model response contained two tool calls:

Code
multiply_numbers(a=250, b=40)
divide_numbers(a=10000, b=5)

The tool outputs were 10,000 and 2,000. The next model response was:

Code
The final result of multiplying 250 by 40 and then dividing by 5 is 2000.

A three-operation request added 100 and produced 2,100.

4. The important distinction: multiple calls versus dependent steps

The early loop executes every tool call returned in a single model response. That is acceptable only when the calls are independent or when the model has supplied all required inputs correctly.

But consider a task where Step 2 needs the actual result of Step 1. The model might guess or fill in a value rather than wait for the application to provide the output. That is why the project later introduced explicit task steps, dependency planning and a result store.

5. Preserve the final assistant response

The conversation history should include the final assistant response as well as the tool calls and observations:

Code
final_response = send_message(messages=messages)

messages.append(
    final_response.message.model_dump(exclude_none=True)
)

print(final_response.message.content)

The original code evolved through a few iterations here. One early version accidentally printed the first response object after making the second call. Another version placed message appending outside the intended branch due to indentation. These are good reminders to inspect both the control flow and the actual object being printed.

What this loop does not yet solve

The bounded loop handles model ↔ tool interaction. It is not yet a dependency-aware scheduler. It does not independently prove that one step's prerequisite completed before another step begins. Explicit task-step records and dependency planning are the next layer.