SKIP TO CONTENT
All articles
EXPLAINER·June 23, 2026·4 MIN READ

How to build your first AI agent: a model, some tools, and a loop

By VCA Newsroom

"AI agent" sounds like it should require a framework, a vector database, and a weekend of setup. It doesn't. Anthropic's own definition is refreshingly small: an agent is an LLM autonomously using tools in a loop. Once you see the pattern, you can build a useful first agent in well under a hundred lines of code — and, just as importantly, know when you shouldn't.

The core loop

Every agent, no matter how fancy, is the same four-step cycle:

  1. You give the model a goal and a list of tools it's allowed to call.
  2. The model decides whether to answer directly or call a tool.
  3. Your code runs the tool and feeds the result back to the model.
  4. Repeat until the model says it's done.

The model never touches your filesystem or network itself. It only ever asks to run a tool; your code decides whether and how to actually do it. That gap is where all your safety lives.

A concrete example

Here's a minimal agent that can answer questions using a calculator tool. The structure is what matters — the same shape scales to file editing, web search, or database queries.

import anthropic

client = anthropic.Anthropic()

tools = [{
    "name": "calculator",
    "description": "Evaluate a basic arithmetic expression and return the result.",
    "input_schema": {
        "type": "object",
        "properties": {"expression": {"type": "string"}},
        "required": ["expression"],
    },
}]

def run_tool(name, args):
    if name == "calculator":
        # In real code, use a safe parser, not eval()
        return str(eval(args["expression"], {"__builtins__": {}}))
    return "unknown tool"

messages = [{"role": "user", "content": "What is 1894 * 37 plus 12?"}]

while True:
    resp = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        tools=tools,
        messages=messages,
    )
    messages.append({"role": "assistant", "content": resp.content})

    if resp.stop_reason != "tool_use":
        print(resp.content[0].text)
        break

    tool_results = []
    for block in resp.content:
        if block.type == "tool_use":
            result = run_tool(block.name, block.input)
            tool_results.append({
                "type": "tool_result",
                "tool_use_id": block.id,
                "content": result,
            })
    messages.append({"role": "user", "content": tool_results})

That while loop is the agent. The model sees the question, asks to call calculator with "1894 * 37 + 12", your code runs it, hands back 70090, and the model writes the final answer. Swap the calculator for a read_file/write_file pair and you have a primitive coding agent.

Tools are the part that actually matters

The model is only as capable as the tools you give it, and tool design is where most first agents fail. Anthropic's guidance on writing tools for agents boils down to a few rules:

  • Name and describe tools like you're writing for a new teammate. search_invoices(customer_id, date_range) beats a vague query(sql) — the clearer the contract, the fewer wrong calls.
  • Return high-signal results, not raw dumps. If a tool returns 5,000 lines of JSON, you've just spent the model's context budget on noise. Trim to what's useful.
  • Make errors instructive. "File not found: did you mean config.yaml?" lets the model recover; a bare stack trace usually doesn't.

Guardrails before you let it run

Because your code executes every tool call, you own the safety. Three guardrails to add before any agent touches something real:

  • Cap the loop. Add a max-iteration count so a confused agent can't spin forever (and run up a bill).
  • Gate destructive actions. Anything that deletes, sends, or pays should require confirmation or run against a sandbox first.
  • Validate tool inputs. Never eval() untrusted strings or pass model output straight into a shell. Treat every tool argument as untrusted user input — because effectively it is.

When NOT to build an agent

The most useful thing to learn early: many problems don't need an agent at all. If the steps are known in advance — fetch data, summarize it, email the summary — a plain script with one or two model calls is cheaper, faster, and easier to debug. Anthropic calls these workflows, and recommends them whenever the path is predictable. Reach for a true agent only when the model genuinely needs to decide what to do next based on what it finds.

Start with the four-step loop above, give it one or two well-described tools, cap the iterations, and add a real task. You'll learn more from one working agent than from any amount of framework documentation.

Auto-generated by Vibe Coding Academy on June 23, 2026, grounded in the real sources linked above. We review for accuracy, but please verify time-sensitive details against the primary sources.

Build Blueprint · Creator

Have an idea? Get the spec your AI agent can build from.

Describe any product and get a complete build blueprint — stack, data model, screens, APIs, and a ready-to-paste prompt for Claude Code or Cursor. Export to PDF.

Open the Blueprint