GNGiseldo Neo and Alana NeoBooks
Building AI Agents15 / 18

Assembling your agent

“Building in the right order is the difference between debugging one problem at a time and debugging all of them at once.”

Objective

Consolidate the book into a sequence of eight verifiable steps, from the loop with a fake model to the agent in production, and close with an objective readiness checklist.

12.1 The order that works

Order matters, because each piece depends on the previous one and is verifiable without the next. It is the same order that organized this book.

12.1.1 Step 1 — the loop, with a fake model

Write the loop with a scripted model (Chapter 2). No network, no key, no cost. Make it work with two tools. The goal is to be sure the message format is right before spending money.

12.1.2 Step 2 — real tools, one at a time

Add a real tool (Chapter 3). Validate the arguments. Turn errors into observations. Only then add the second one.

12.1.3 Step 3 — policies and budget

Before connecting a real model, put up the barriers (Chapter 4). Allowlist, step ceiling, time ceiling. It is easier to test policy with a deterministic model.

12.1.4 Step 4 — tracing

Turn on tracing before debugging. You will need it at the first strange behavior, and you will not want to add it while the bug is happening.

12.1.5 Step 5 — real model

Replace the fake model with the API. The format has already been validated, so any errors that show up will be about behavior, not infrastructure.

Listing 12.1. Loop with a real model (OpenAI-compatible API).

def agent_loop(question: str, max_steps: int = 6) -> str:
    messages = [
        {"role": "system",
         "content": "You are an agent. Use run_shell to get real facts."},
        {"role": "user", "content": question},
    ]
    for _ in range(max_steps):
        resp = client.chat.completions.create(
            model=MODEL, messages=messages, tools=TOOLS_SPEC)
        msg = resp.choices[0].message
        messages.append(msg.model_dump(exclude_none=True))
        if not msg.tool_calls:
            return msg.content or ""
        for call in msg.tool_calls:
            args = json.loads(call.function.arguments)
            result = run_shell(**args)
            messages.append({"role": "tool",
                             "tool_call_id": call.id,
                             "content": result})
    return "step limit reached"

Two details bite. First, exclude_none=True in model_dump: if you resend null fields, some providers reject the payload. Second, json.loads(call.function.arguments) can fail — the model sometimes emits invalid JSON. Wrap it in try/except and return the error as an observation.

12.1.6 Step 6 — skills

Add the skill registry (Chapter 6). Start with one or two. Write the pitfalls section carefully — it is the one that pays off the most.

12.1.7 Step 7 — MCP

When a tool needs to be shared or isolated, move it to an MCP server (Chapter 7). The loop does not change.

12.1.8 Step 8 — evaluation

Write the suite before you have many cases (Chapter 10). Three well-chosen cases are worth more than thirty loose ones.

12.2 The final checklist

Before putting an agent into use, check:

  • Does the loop have ceilings on steps, time and tokens?

  • Does every tool validate arguments before executing?

  • Do tool errors become observations, never exceptions?

  • Are policies enforced in code, not just in the prompt?

  • Do destructive commands require an allowlist or confirmation?

  • Does the trace record every call with arguments and result?

  • Are tool outputs truncated at the source?

  • Does the context have compaction with a ceiling and a rewritten summary?

  • Are the goal, the constraints and the current state treated as anchors?

  • Are skills loaded on demand rather than in the system prompt?

  • Is there an evaluation suite that has already failed at least once?

  • Are subagent results verified by handle, not by report?

  • Does the agent run in an environment with the least possible privilege?

If any item is blank, you have a prototype. That is not a problem — as long as you know it.

12.3 Epilogue

What this book has tried to show is that an agent is not magic. It is a loop, a set of contracts, and an engineering discipline around them.

As in any machine learning system, the model code is only a fraction of the total system; the rest is the infrastructure around it (Sculley et al. 2015). The loop is small enough to fit on one screen and be understood in full. The contracts — the tool schema, the message format, the MCP handshake — are simple enough to be written by hand. The discipline — budget, policy, tracing, evaluation — is boring enough to be forgotten, and that is exactly why it separates what works from what merely seems to work.

If you build the loop with a fake model, then the tools with validation, then the barriers, then tracing, and only then plug in a real model, you will have an agent you understand. And an agent you understand is an agent you can fix.

The rest is iteration.