New Tool Radar

AI Concepts

How to Build an Agent Tool Loop in Five Steps

Build an agent tool loop in five steps: send the tools, read the call, run it, return the result, and answer.

Schematic illustration of build an agent tool loop in five steps: step one send the tools, step two run the call, step three answer from the result.

Tool calling is a multi-step conversation between your application and a model: make a request with tools the model could call, receive a tool call, execute code with the tool call’s input, make a second request with the tool output, and receive a final response. That five-step loop is the whole agent: the model decides, your code acts, and the model answers from what your code found.

Prerequisites

  • An API that supports function calling.
  • One function the model can call, with a JSON Schema.
  • Code that can run the function and send its result back.

Steps

Step 1: Send the tools

Send the request with the task and the list of tools the model could call. Each tool needs a name, a description of when to use it, and a parameters schema.

Step 2: Run the call

Read the model’s tool call, validate its arguments against the schema, and run your function with them. Do not let the model call a function your code has not implemented.

Step 3: Answer from the result

Send the tool’s result back to the model in a second request, along with the conversation so far. The model then gives its final answer, or makes another tool call, and the loop repeats.

Worked example: Harbor Light library book lookup agent

The fictional Harbor Light library builds an agent that looks up a book and answers the reader.

Step Who acts What happens
1. Send the tools Your code Request with get_book tool
2. Receive a tool call Model Calls get_book with a title
3. Execute the code Your code Looks up the book record
4. Send the result back Your code Returns the record to the model
5. Final response Model Answers the reader from the record

All 5 steps run in order: the model calls the tool, the code runs it, and the final answer comes from the book record.

Expected result. The loop runs through all five steps, and the final answer is based on the tool’s result. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.

Limits

  • The API generates the tool call; it does not execute the function. Your code is the execution engine.
  • A loop with no step cap can run forever; cap it, as the step-budget guide shows.
  • This guide covers one tool; multiple tools follow the same loop.

Sources

See all