Creuto is now an OpenAI Select Partner Read More

AI & Machine Learning

Build an AI agent from scratch: loop and tools

How to build an AI agent from scratch: a 25-line loop, Anthropic's tool rules and what a 10-call task costs on Claude and OpenAI, November 2026.

Build an AI agent from scratch: loop and tools

To build an AI agent from scratch you need an LLM API call, a loop that runs the tools the model asks for, and a rule that stops it. Anthropic's own advice is to start there, not with a framework. The engineering is in the tools, and the cost is in how often the loop re-sends its context.

By the end of this post you will have the loop in about 25 lines of Python, the tool-design rules Anthropic publishes, and a per-task cost model you can rerun with your own numbers. On list prices as of November 2026, our example task costs about 31 cents on Claude Sonnet 5.5, 11 cents with prompt caching, and under 2 cents on Claude Haiku 5.5. One badly designed tool can more than double that.

How to build an AI agent from scratch: start with the loop

An agent is a loop, not a product category. Anthropic's "Building effective agents" (December 2024) describes agents as "typically just LLMs using tools based on environmental feedback in a loop", and its first recommendation is that "developers start by using LLM APIs directly: many patterns can be implemented in a few lines of code."

Here is that loop on the Anthropic Python SDK. The model either answers or asks for tools. You run the tools, append the results, and call it again until it stops asking or you hit your turn limit:

import anthropic

client = anthropic.Anthropic()
MAX_TURNS = 10  # the stopping condition

def run_agent(task, system, tools, execute_tool):
    messages = [{"role": "user", "content": task}]
    for _ in range(MAX_TURNS):
        response = client.messages.create(
            model="claude-sonnet-5-5",
            max_tokens=16000,
            system=system,
            tools=tools,
            messages=messages,
            cache_control={"type": "ephemeral"},  # cache the growing prefix
        )
        messages.append({"role": "assistant", "content": response.content})
        if response.stop_reason != "tool_use":
            return response
        results = []
        for block in response.content:
            if block.type == "tool_use":
                results.append({
                    "type": "tool_result",
                    "tool_use_id": block.id,
                    "content": execute_tool(block.name, block.input),
                })
        messages.append({"role": "user", "content": results})
    raise RuntimeError("Agent hit MAX_TURNS without finishing")

Every component of an AI agent is visible in those lines. The model plus its tools is what Anthropic calls the augmented LLM: a model given retrieval, tools and memory, which it can actively use. The messages list is the agent's working memory. MAX_TURNS is the stopping condition Anthropic recommends, "such as a maximum number of iterations". Notice what the loop does on every turn: it sends the whole history again. That single fact drives the cost section below.

Do you need LangChain or a framework to build an AI agent?

You do not need a framework for a single agent. Anthropic's warning is specific: frameworks "often create extra layers of abstraction that can obscure the underlying prompts and responses, making them harder to debug." When an agent picks the wrong tool, you need to see the exact prompt it saw.

The strongest case for a framework such as LangGraph, the OpenAI Agents SDK or the Claude Agent SDK is everything around the loop: tracing, retries, persisted state between sessions, human approval steps, and several agents handing work to each other. Those are real features, and writing them yourself is not free. Our view: write the raw loop first, so your team understands what the framework is doing. Adopt one when you need its state handling or multi-agent orchestration, which we cover in who should run the loop in a coordinator agent pattern.

When to use a workflow instead of an AI agent

Use a workflow whenever you can write the steps down in advance. Anthropic separates workflows, "systems where LLMs and tools are orchestrated through predefined code paths", from agents, "systems where LLMs dynamically direct their own processes and tool usage." Its advice is to find "the simplest solution possible, and only increasing complexity when needed", because "agentic systems often trade latency and cost for better task performance."

The same essay names five workflow patterns to try before an autonomous agent:

  • Prompt chaining: a fixed sequence of calls, each working on the last one's output.
  • Routing: classify the input, then send it to a specialized prompt or model.
  • Parallelization: split the work into independent calls and combine the results.
  • Orchestrator-workers: one call breaks the task down and delegates the parts.
  • Evaluator-optimizer: one call drafts and another critiques until it passes.

An agent is the right choice, in Anthropic's words, "when flexibility and model-driven decision-making are needed at scale". It is the wrong choice when the task is a known sequence (extract, validate, file) dressed up as autonomy. You pay for every turn the model spends deciding what you could have hard-coded.

How to design tools for an AI agent

The tools matter more than the prompt. Building their SWE-bench coding agent, Anthropic's team "spent more time optimizing our tools than the overall prompt." Its essay calls this the agent-computer interface and says to "poka-yoke your tools": change the arguments so mistakes are harder to make. Their own example was requiring absolute file paths, which the model then used without error.

Anthropic's follow-up, "Writing effective tools for agents" (September 2025), turns this into rules you can apply in code review:

  • Build fewer, task-shaped tools. Don't wrap every endpoint. For an address book it suggests a search_contacts or message_contact tool "instead of a list_contacts tool", and one schedule_event tool that finds a slot and books it.
  • Namespace related tools. Shared prefixes by service or resource help the model pick the right one. Whether prefix or suffix naming works better is something to test, not assume.
  • Return high-signal fields. Names, not opaque IDs. Offer a response_format parameter: in Anthropic's example the detailed response was 206 tokens and the concise one 72.
  • Cap what comes back. Use pagination, filtering and truncation with sensible defaults. "For Claude Code, we restrict tool responses to 25,000 tokens by default."
  • Write errors the model can act on. A message that says what to change beats an opaque error code.

A tool definition that follows those rules looks like this:

orders_search = {
    "name": "orders_search",
    "description": "Search orders by customer email or order ID. "
                   "Returns at most 10 orders with status, total and last update.",
    "input_schema": {
        "type": "object",
        "properties": {
            "query": {"type": "string"},
            "response_format": {"type": "string", "enum": ["concise", "detailed"]},
        },
        "required": ["query"],
    },
}

Then test the tools the way Anthropic does: build evaluation prompts, each "paired with a verifiable response or outcome", and keep a held-out set so you are not tuning the tools to the test. Agent evaluation is what turns tool design from taste into measurement.

What an AI agent costs per request, worked from list prices

The cost of an agent task is set by how many times the loop re-sends its context, not by the headline price per token. Here is our model, with every assumption stated so you can swap in yours:

  • The prefix (system prompt, tool definitions and the task) is 6,000 tokens. That includes the 286-token tool-use system prompt that Anthropic's pricing page lists for Sonnet 5.5, Opus 5.5 and Haiku 5.5 whenever any tool is present.
  • The task takes 10 model calls. Each turn adds 1,500 tokens to the history: about 500 tokens of model output (the tool call) and 1,000 tokens of tool result.
  • Call one sends 6,000 input tokens and call ten sends 19,500. Across the task that is 127,500 input tokens and 5,000 output tokens.
  • With caching, everything already sent is a cache hit on the next call and only the new 1,500 tokens are written: 19,500 tokens written and 108,000 read per task.

Applied to the Claude and OpenAI standard prices on 20 November 2026:

ModelInput / output per MTokPer task, uncachedPer task, cached
Claude Opus 5.5$4 / $20$0.61$0.22
Claude Sonnet 5.5$2 / $10$0.31$0.11
Claude Haiku 5.5 (prompts up to 100,000 tokens)$0.10 / $0.50$0.015$0.006
gpt-6.2-astra$8 / $40$1.22$0.40
gpt-6.2-sol$2 / $10$0.31$0.10
gpt-6-luna$0.10 / $0.50$0.015$0.006

The cached column uses Anthropic's 5-minute cache write at 1.25x the input price and cache hits at $0.10 per MTok on Sonnet 5.5, $0.20 on Opus 5.5 and $0.01 on Haiku 5.5. For OpenAI we priced the same hit pattern at each model's listed cached-input rate. These are our calculations from list prices, not measured bills, and your turn count and tool-result sizes will move them more than your model choice will. Output is 5,000 tokens in every row, so on Sonnet 5.5 it is $0.05 of the $0.31, and input is the rest.

How a single tool result can double the cost of a task

Take the same task and let one tool return 25,000 tokens, the Claude Code default cap, on the first turn. That result is now re-sent on each of the nine calls that follow: 225,000 extra input tokens, or $0.45 on Sonnet 5.5 uncached. One verbose tool costs more than the rest of the task combined. With caching it is written once and read eight times, about $0.08, which is why caching matters more for agents than for chat.

This is the cost argument for Anthropic's tool rules. A response_format switch that cuts 206 tokens to 72 is a saving you collect on every later turn of every task, not once.

Prompt caching, batch and the multipliers to check

  • Web search costs $10 per 1,000 searches on the Claude API, plus the result tokens, which stay in the context for later turns. OpenAI also lists $10 per 1,000 web search calls, and file search at $2.50 per 1,000.
  • Haiku 5.5 has a context cliff. A request whose prompt is over 100,000 tokens pays $0.50 / $2.50 per MTok, five times the base rate, and cache reads count toward that length.
  • Data residency. US-only inference with inference_geo applies a 1.1x multiplier to all Claude token prices. OpenAI lists a 10% uplift on regional processing endpoints for models released on or after 5 March 2026.
  • Long context on OpenAI. Above 272,000 input tokens, gpt-6.2-sol's input price doubles to $4.
  • Batch halves prices on both platforms, but only for work that can wait for an asynchronous result. An interactive agent loop usually can't. We compared the trade-offs in OpenAI Batch API vs Flex.

At 10,000 tasks a month, the Sonnet 5.5 rows above come to about $3,050 uncached and $1,100 cached. That is the run cost of the loop only. It is a number to watch per task once you ship, which is what AI spend monitoring is for.

How long it takes to build an AI agent in-house

The loop takes a day. The weeks go to everything the loop touches: tools with clean inputs and capped outputs, permissions for what the agent may change, an evaluation set with verifiable outcomes, and logging you can read when a run goes wrong. As a general delivery figure, we scope a focused MVP at 6-10 weeks and larger enterprise platforms at 3-6 months, depending on integrations and scope.

Owning the loop also means owning model upgrades. New models change request behavior, as Claude Opus 5.5 did for four features agents use, and a raw-API agent needs someone to re-run its evaluations and fix what breaks. That ongoing work, not the first build, is the honest argument for a managed harness or a framework.

Before you commit engineers to an in-house agent

Answer three questions on paper first. Can the task be a workflow with fixed steps? What do your tools return, in tokens, at their worst? And what does one task cost on your chosen model when you put your own turn count and result sizes into the model above? If the per-task number survives your monthly volume, building on the raw API is a reasonable first move.

If you are deciding whether your team builds the agent in-house or brings in help for the tools and evaluations, our AI engineering team can review your prototype's tool design and rerun this cost model against your real traces before you commit engineers to it.

Frequently asked questions

To build an AI agent from scratch, call an LLM API with a list of tool definitions, run whichever tools the model requests, append the results to the conversation and call the model again. Stop when it answers without a tool call or reaches a maximum number of turns. The loop itself fits in about 25 lines of Python.

A framework is not required to build a single AI agent. Anthropic recommends starting with LLM APIs directly, warning that frameworks can obscure the underlying prompts and responses. Frameworks such as LangGraph or the OpenAI Agents SDK earn their place once you need persisted state, tracing, approvals or several cooperating agents.

An AI agent task of 10 model calls, starting from a 6,000-token prefix that grows 1,500 tokens a turn, costs about $0.31 on Claude Sonnet 5.5 at November 2026 list prices, about $0.11 with prompt caching, and under 2 cents on Claude Haiku 5.5. Turn count and tool-result size move the cost most.

A workflow is the better choice whenever the steps can be written down in advance, such as extract, validate and file. Anthropic advises finding the simplest solution and adding complexity only when needed, because agentic systems trade latency and cost for task performance. Agents fit tasks needing model-driven decisions at scale.

Tools for an AI agent should be few and task-shaped, namespaced by service or resource, and return high-signal fields rather than opaque IDs. Anthropic's guidance adds pagination, filtering and truncation, actionable error messages, and evaluation prompts with verifiable outcomes. Claude Code caps tool responses at 25,000 tokens by default.

Written by

Akash Mohapatra

Akash Mohapatra

Co Founder & Director

19 Nov 2026

·

10 min read

Share

LET'S CONNECT

Connect with Creuto!

Ready to take the first step towards unlocking opportunities, realizing goals, and embracing innovation? We're here and eager to connect.