All insights

How Do I Build a Simple AI Agent Myself?

How a simple AI agent is built: tool descriptions, a loop, a step limit and a stop condition, shown as a plain sketch, plus what a first version leaves out.

By 8MB Tech Published 10 min read

Close-up of hands typing on a silver laptop keyboard on a light wooden table, with a sofa and plant softly blurred behind.

The short answer

To build a simple AI agent, describe a tool or two to a model, write code that runs them, and wrap both in a loop. The loop sends the conversation to the model, runs any tool it requests, returns the result, and stops on an answer or a step limit. This post sketches that structure; we did not run it.

Imagine it is Friday at the Lagos fabric shop from the first post. The shop’s developer has read the earlier posts about how agents work and wants to know what one looks like in code. Not a product, just a small agent that answers “Where is order 1042?” using two tools. This post walks through how such an agent is put together. The data is made up, and every tool only reads.

(This is an illustrative scene, not a real business.)

What are we sketching?

We are sketching the loop from the second post in a form short enough to read in a minute. The program sends a question to a model along with descriptions of two tools, lookup_order and track_parcel. When the model asks for a tool, the program runs it and sends back the result. When the model gives an answer, the program shows it.

One thing to say plainly: we did not run this. The sketch below is pseudocode, which means plain instructions laid out like code but not written for any real programming language or library. We have not tested it against a model, so this post shows no output, no timings and no costs. What we did do is read how Anthropic and OpenAI document the same loop, and the sketch follows what they describe. Anthropic’s own tutorial builds the loop by hand and then replaces it with a ready-made helper in its library, which it calls the tool runner. We stay with the hand-written shape because it shows the parts.

What do you need before you start?

Four things. A model you can call over an API, which is how your program sends text to a service and gets text back. A short list of tools you want it to use. Some code of your own that actually does the work when the model asks. And a loop that ties them together.

To call a model, you need an account with the vendor and a secret key that identifies it. Keep that key out of your code. Each vendor’s quickstart has the exact steps, and they change, so follow the current one. Using the API costs money, and each vendor lists prices on its models page.

What does the whole sketch look like?

Here is the whole structure. Everything in it is made up, including the order number.

Illustrative sketch, not run.

MAX_STEPS = 6

tools = [
  lookup_order(order_number)
    "Find one order by its number. Return its status, tracking code
     and items. Use when someone asks about an order."
  track_parcel(tracking_code)
    "Say where a parcel is, using the tracking code from lookup_order."
]

function run_tool(name, input):
  if name is lookup_order:
    find the order in the made-up data
    if missing: return error "No order with that number. Ask the user to check it."
    return the order record
  if name is track_parcel:
    find the parcel in the made-up data
    if missing: return error "No parcel with that tracking code."
    return the parcel record
  otherwise: return error "Unknown tool"

conversation = [ user says: "Where is order 1042?" ]

repeat up to MAX_STEPS times:
  reply = ask the model(conversation, tools)
  if the reply is not a tool request:
    stop and show the reply          # normally the final answer
  add the reply to conversation
  for each tool request in the reply:
    result = run_tool(request.name, request.input)
    add the result to conversation, tagged with the request's id

if the loop ended without a final answer:
  report "stopped: reached MAX_STEPS" and fail

The rest of this post goes through it in order.

How do you describe the tools to the model?

The tools list is the model’s only view of your tools. Each entry has a name, a plain description and a definition of the inputs it takes, as in the fifth post. The model never sees the code that runs them. As of October 2026, a comment in Anthropic’s tutorial code says: “Claude determines whether to call the tool based on the request and the tool description.” So the descriptions matter. Ours say when to use each tool and where its input comes from. The description of track_parcel says to use the tracking code from lookup_order, which, in our reading, helps the model chain the two.

How do you write the tools themselves?

They are ordinary functions in your own code. In the sketch, run_tool looks things up in a small set of made-up records. In a real shop it would query a database or a courier’s system. We kept both tools read-only, so nothing can be changed by mistake. The fifth post showed how a tool that sends or changes something should wait for a person’s approval.

Tools fail, so the sketch plans for that. If an order is missing, run_tool returns an error message and marks it as an error. Anthropic’s documentation, as of October 2026, says to “send the error message back with is_error: true instead of crashing”, and that Claude “can retry with corrected input, ask the user for clarification, or explain the limitation.” It also advises writing instructive messages: “Instead of generic errors such as ‘failed’, include what went wrong and what Claude should try next.” That is why our missing-order message ends “Ask the user to check it.”

How does the loop work?

The loop keeps going while the model asks for tools. Anthropic’s documentation, as of October 2026, says a tool request arrives as a response with stop_reason: "tool_use" and a tool_use block “containing the tool name, a unique id, and the structured input”. Your code runs the tool and sends the result “in a tool_result block whose tool_use_id matches the id from the call.” Each pass of the loop in our sketch does this:

  1. Send the conversation and the tool descriptions to the model.
  2. If the reply is not a tool request, stop. Normally that means the model has answered.
  3. Otherwise add the model’s reply to the conversation, run each tool it asked for, and add the results as a new message.

The program keeps the whole conversation and sends it every time. That is the point of the seventh post: the model does not remember earlier steps, so the software sends them again.

Notice the inner “for each tool request”. Anthropic’s tutorial, as of October 2026, says a model “might return several tool_use blocks in a single response”, and “Your loop needs to process all of them and send back all results together in one user message.” So the sketch answers every request in a reply, not just the first.

OpenAI’s function calling guide, as of October 2026, describes the same five steps in its own terms, listed in the FAQ for this post. The field names differ between vendors. The shape does not.

Why does the loop have a step limit?

In our reading, a loop that is allowed to run forever can run up a bill or repeat a mistake. The step limit is MAX_STEPS, set to 6 in the sketch. Anthropic’s engineering team wrote in “Building effective agents” in December 2024 that “it’s also common to include stopping conditions (such as a maximum number of iterations) to maintain control.” Anthropic has since added a note to that December 2024 page saying “Much of the tooling landscape described in this post has changed since December 2024.”, so we use only its general guidance here.

The sketch has two ways to stop: the model answers, or the limit is reached. If the limit is hit first, the program says so and reports a failure, rather than quietly returning nothing. The number 6 is our choice, not a recommendation from any vendor. Pick one that fits the job, then watch what real runs need.

What should happen when it runs?

We cannot show a run, because we did not do one. What we can say is what the loop is built to do. Anthropic’s tutorial, as of October 2026, says “The loop might run once or several times depending on how Claude breaks down the task.” In our reading, for the question about order 1042, expect a first pass where the model asks for lookup_order, a second where it asks for track_parcel, and a third where it answers. A real model may take a different route or ask for both at once. A step limit is a ceiling, not a target.

If you build your own version, run it yourself, read what the model asked for at each step, and check the answer against your data.

What does this sketch leave out?

A lot, on purpose. It has no approval step before an action, no memory between runs, no logging, and no checks on what the model sends to your tools. It also does not handle a reply that was cut off before it finished, or any other way a reply can end besides a tool request or a normal answer. Our tools only read made-up data, so those gaps would do no harm here. In real code they would.

If you give an agent tools that send messages or change records, OpenAI’s guide, as of October 2026, says actions that are “sensitive, irreversible, or have high stakes should trigger human oversight until confidence in the agent’s reliability grows”. In our reading, adding an approval step before any such tool is the first thing to do.

Anthropic’s engineering team also suggested in December 2024 keeping things simple. It said developers should start with the LLM APIs directly, and that many patterns “can be implemented in a few lines of code”. That matches the size of this sketch. If you later use a framework, make sure you understand the code underneath it.

What to remember

  • An agent is a loop. Send the conversation and the tool descriptions, run the tools the model asks for, send the results back, and stop when the model answers.
  • Give every loop a step limit, and make tool errors readable, so the model can recover from them.
  • A first version is a learning tool. Before real use, add approvals, logging and checks, test it yourself, and keep a person in charge of risky actions.

If you want to add a language model to your own software, our LLM integration guide covers API patterns. To revisit earlier posts, see How Do AI Agents Use Tools and Call Functions? and How Do AI Agents Remember Things? Context Windows, Memory and RAG.

Next in the series: when several AI agents work better than one.

Frequently asked questions

Do I need a framework to build an AI agent?

No. Anthropic's engineering team suggested in December 2024 that developers start by using LLM APIs directly, because many patterns can be implemented in a few lines of code. It warns that frameworks can add layers that obscure the underlying prompts and responses. If you use one, make sure you understand the code underneath it.

How much does it cost to run an AI agent?

The API is billed by usage, and each model has its own price per million tokens. A small agent like the one sketched here should need only a few requests, but an agent that loops sends the whole conversation each time. Check the current prices on your vendor's models page and set a step limit before you run anything.

Can I build this with a model from another company?

Yes, in principle. The loop is the same idea at both vendors whose documentation we read, and we have not tested any vendor's code. OpenAI's function calling guide, as of October 2026, describes the same five steps: send tools, receive a tool call, run it in your code, send the output back, and receive an answer or more tool calls. The names of fields and functions differ, so follow your vendor's documentation.

How do I stop an agent from looping forever?

Add a stopping condition. Anthropic's engineering team wrote in December 2024 that it is common to include stopping conditions, such as a maximum number of iterations, to maintain control. In our sketch that is a constant called MAX_STEPS. If the limit is reached before an answer, the loop stops and says so.

Sources

  1. Tutorial: Build a tool-using agent Anthropic · Accessed
  2. Handle tool calls Anthropic · Accessed
  3. Building effective agents Anthropic (Engineering), 19 December 2024 · Accessed
  4. Function calling OpenAI · Accessed
  5. A practical guide to building agents OpenAI · Accessed

Tell us what you need

Book a call, phone us or send an email.

Suite B6, 3 Military Street, Lagos Island, Lagos 101231, Nigeria