GitHubGet started

BrekkyLab team · · 16 min read

AI Agent Architecture: 5 Parts and Where They Run

AI Agent Architecture: 5 Parts and Where They Run. The model at the provider sends a reply to the loop in your program, which holds the tools, keys and history, and the loop sends tool calls to a sandbox holding a shell, files and code.

TL;DR

  • An AI agent is a model that uses tools in a loop. Anthropic calls agents “LLMs using tools based on environmental feedback in a loop”. Everything else in the architecture exists to make that loop work.
  • It has five parts. The model decides, the loop carries its requests out, the tools are the actions it can take, the sandbox is where those actions run and the runtime keeps the agent’s history and state between turns and runs.
  • Where each part runs is the real architecture decision. The model runs on the provider’s servers, the loop runs and the keys live in your program, and the commands the model writes run in a sandbox that sees only what the task needs.
  • Most agent incidents come from a part in the wrong place. Generated code that runs next to the credentials, a shell on your own machine, or a history that grows until the model loses track.
  • Ailoy gives every agent a Linux VM of its own, on your machine. There is no Docker and no sandbox account to set up. The agent installs software, runs code and keeps files in its own computer, while the loop and the keys stay in your program.

Search for “AI agent architecture” and you get diagrams of perception, reasoning, memory and action. They describe how an agent thinks. They don’t tell you what to build, which process holds your API key, or where the rm -rf the model just wrote is going to run.

This guide splits an agent into the five parts you build or choose (model, loop, tools, sandbox and runtime), shows what each one does in a single turn and where each one runs, then covers the mistakes that come from putting a part in the wrong place. It ends with how Ailoy puts the five together, with a working example.

What is AI agent architecture?

AI agent architecture is how the parts of an agent are split up and connected: which part decides, which part acts, where each action runs and where the agent’s state is kept.

The people who build agents agree on the core. Anthropic separates agents from fixed workflows: “Workflows are systems where LLMs and tools are orchestrated through predefined code paths”, while agents “dynamically direct their own processes and tool usage”. OpenAI’s guide says “an agent consists of three core components”: the model, the tools and the instructions. Lilian Weng’s post on LLM agents describes the model as “the agent’s brain”, with planning, memory and tool use around it.

Those lists describe what an agent needs. Once you write one, two more questions come up that they leave open: where do the tools run, and what keeps track of the agent between steps? That gives the five parts below.

What are the five parts of an AI agent?

Where each part of an agent runs. The provider holds the model, which gets a prompt from your program and sends back a reply. Your program holds the loop, your own tools, the history and the API key, and the keys stay there. A tool call goes to the sandbox, the agent's own VM, which holds the shell and files, the code it writes, the packages it installs, one mounted folder and the files it keeps, with no keys. The result comes back to your program, and credentials never cross to the sandbox.
  • The model decides. A language model reads the conversation and the list of tools, then writes either an answer or a request to call a tool. It never runs anything itself. Example: asked to chart last month’s refunds, the model writes shell("python chart.py"). Writing that line is all it does.
  • The loop carries the requests out. The loop (often called the harness) sends the model its context, runs each tool call, adds the result to the conversation and calls the model again, until the model answers without asking for a tool or a limit stops it. Our post What Is an Agent Harness? covers it in depth. Example: the loop sees the shell call, sends it to the machine that runs commands and puts the output back in front of the model.
  • The tools are the actions on offer. Each tool is a name, a description and a schema for its arguments. Some act on a computer (a shell, file reads and edits), some call your own code (an order lookup, a pricing API), some reach out (a web search, an MCP server). Example: a support agent has lookup_order(order_id) for your database and shell for the CSV work.
  • The sandbox is where the actions run. When a tool runs a command or code the model wrote, it needs a computer to run on. A sandbox is an isolated one, usually a container or a VM, holding only the files and network access the task needs. Example: the chart script runs in a microVM that sees one exported CSV and nothing else on your laptop.
  • The runtime keeps the agent going. The runtime holds what the agent has done: the conversation history, the files it left in the sandbox and anything it needs to pick up after a restart. Example: a research agent stops for the night, and the next morning a new process loads yesterday’s history and reopens the same sandbox.

Instructions and memory, which other lists name separately, sit inside these parts. The instructions are the first message the loop sends the model. Memory is split between the history the runtime keeps and the files the agent writes in its sandbox.

Where does each part of an agent run?

Each part runs in a different place, and the boundary between them decides what a mistake can reach:

Part Where it runs What it holds
Model The provider’s servers, or a server you run yourself such as vLLM or Ollama Nothing between calls. It only sees what the loop sends
Loop Your program The model API key and your rules for each tool
Your own tools Your program Credentials for your database and internal APIs
Shell and file tools The sandbox The files and network access you give it, no keys
Runtime state Your program and the sandbox The history in your program, the agent’s files in the sandbox

The line to hold is between deciding and doing. The model and the loop sit on the deciding side. The sandbox is where code the model wrote, packages it installed and commands it chose run for real, so it is the part that sees the least. Anthropic draws the same line between “the ‘brain’ (Claude and its harness)” and “the ‘hands’ (sandboxes and tools that perform actions)”.

Our earlier post, AI Agent Sandbox: Containers vs gVisor vs microVMs, covers the sandbox side on its own: containers against VMs, mounts and network access. For where the loop, often called the harness, stops and an SDK, a framework or a runtime begins, read What Is an Agent Harness?.

What happens in one turn of an AI agent?

A turn is one request and everything the agent does to answer it. Follow one through the five parts: “Chart refunds by month from refunds.csv and save the chart.”

  1. The loop builds the context. It puts the instructions, the history so far, your request and a description of each tool into one request, and sends it to the model.
  2. The model asks for a tool. It replies with read("/data/refunds.csv"). Nothing has been read yet. The model has only written a request.
  3. The loop runs it in the right place. A file read belongs to the sandbox, so the loop sends it there and adds the first rows of the file to the history. A call to your own lookup_order would run in your program instead.
  4. The loop goes around again. The model writes a script with write, runs it with shell, sees an error about a date column, fixes the script and runs it again. Each result goes back to the model.
  5. The turn ends. The model answers in plain text: “Saved the chart to /data/refunds_by_month.png.” OpenAI describes the run as a loop that goes on “until an exit condition is reached”, and notes that “Common exit conditions include tool calls, a certain structured output, errors, or reaching a maximum number of turns.”
  6. The runtime keeps the result. Every message from that turn stays in the history, and the chart stays in the sandbox, so the next request (“now split it by country”) starts where this one stopped.

Anthropic says it is “crucial for the agents to gain ‘ground truth’ from the environment at each step”. Step 4 is where that happens: the model learns about the date column from the error the script really threw.

Is an AI agent the same as a workflow?

No. In a workflow, your code fixes the steps and the model fills in each one. In an agent, the model picks the next step itself, based on what the last one returned.

The five parts are the same in both. What changes is who drives the loop:

  • A workflow follows a path you wrote. Example: a ticket triage flow always classifies the ticket, then drafts a reply, then posts it for review. The model never decides to do something else.
  • An agent chooses its next tool call. Example: a debugging agent reads a log, decides to run the tests, sees a failure in a different module and opens that file instead. Nobody wrote that path in advance.

Anthropic recommends “finding the simplest solution possible, and only increasing complexity when needed”, so reach for an agent when the task needs that flexibility. The more freedom the model has to choose its own steps, the more the sandbox has to do, because you can no longer list in advance what will run.

What goes wrong when a part is in the wrong place?

When a part sits in the wrong place: four cases, code next to the keys, a shell on your machine, a network left open and a history never trimmed, all lead to the same fix, put each part in its place. The model can be fine and the agent still fails.

The model can be fine and the agent still fails, because one of the other parts sits where it shouldn’t. These cases each come from a placement decision:

How do you choose an architecture for your agent?

Answer these five questions before you pick a framework or write the loop:

  1. Which models will it run? If you want to compare Claude, GPT and an open model on the same task, the model should be a setting you can change in one place. Example: run the same agent on anthropic/claude-haiku-5-5 and Qwen/Qwen3-235B-A22B-Instruct-2507 and compare the results. In Ailoy, the model is the one string you pass to AgentBuilder.
  2. Where will its commands run? Decide between your machine, a container, a VM on your machine or a hosted sandbox, and whether you choose the files and the network it sees. Example: an agent that summarises invoices should see the invoices folder and have no internet. In Ailoy, the VM runs on your machine, and create_sandbox sets the volumes and network it gets.
  3. Where do the keys live? Keep the model key and any credentials for your own systems in your program, outside the sandbox. Example: the agent calls lookup_order in your program, which holds the database password, and never sees the password itself. In Ailoy, the keys stay in your program and the VM holds none.
  4. What happens to state between runs? Decide how you save the history, and whether the sandbox’s files survive to the next session. Example: a coding agent that installed dependencies on Monday shouldn’t install them again on Tuesday. In Ailoy, you save agent.history and start the next run from it, on the same sandbox, and keep any files a later run needs on a volume.
  5. Can you see each step? You want every tool call and result as it happens, to log it, show it in a UI or stop the run. Example: a support lead checks which order the agent looked up before the reply goes out. In Ailoy, agent.run yields each message as it completes, and run_stream streams the words as the model writes them.

How does Ailoy put these parts together?

Ailoy is an open-source agent SDK from BrekkyLab, for Python, Node.js and Rust, that gives every agent a Linux VM of its own, on your own machine, with no Docker and no sandbox account to set up. It spans more than one of the five parts: AgentBuilder is the loop and the tools, and virtx, the open-source VM layer developed alongside it, is the sandbox. The history lives in your program and the agent’s files live in its VM, so the runtime state sits on both sides of the line.

Here is what that gives an agent, according to the Ailoy docs:

  • A full computer for every agent. Client.spawn() starts a virtx server on your machine, and create_sandbox boots a Linux microVM from any image. Hand it to the agent with .sandbox(sandbox) and its shell and file tools run there. The agent installs software, writes and runs code and keeps its files in it, and the VM can use the host’s GPU through Vulkan or serve an app on a port you open (giving an agent a sandbox). The repo’s examples use it for CAD models, a game played over VNC and SAM3 on the GPU.
  • Local, with no vendor in between. The VM runs on your own machine, so there is no sandbox account, no sandbox bill and no network hop to a hosted service. When one machine is not enough, the same code can use a virtx server on another machine.
  • The loop and the keys stay outside the VM. The model key, the loop and your own tools’ credentials stay in your program. You choose the volumes the VM sees, whether it has a network and which ports it opens, which is the placement the sections above call for.
  • Any model, one string. The model is a setting such as anthropic/claude-haiku-5-5 or openai/gpt-6-luna, and each model gets the shell and file tools that suit it (model providers, tools).
  • State that carries over. Save agent.history and build the next agent from it, then hand it the same sandbox, or a new one that mounts the volume holding its files (continue the work later).

The rest is what you would expect from an agent SDK: a context manager that trims old tool results, and your own functions, MCP servers, skills and browser tools (context manager, running an agent, tools, using MCP, skills, using a browser).

This example, adapted from the sandbox guide, runs the refunds request from earlier and puts the five parts in one file:

import asyncio
import json
from pathlib import Path
from ailoy import AgentBuilder
from virtx import Client, Recipe, SandboxVolume
async def main() -> None:
# Sandbox: a virtx server on this machine, and an image with pandas and matplotlib
client = Client.spawn()
recipe = Recipe("python:3.12-slim-trixie").run("pip install --no-cache-dir pandas matplotlib")
await client.build_image(recipe, "refunds:latest").complete()
# One volume carries refunds.csv in and the chart out; no network
data = await client.create_volume("refunds-data")
await data.write_file("refunds.csv", Path("refunds.csv").read_bytes())
sandbox = await client.create_sandbox(
"refunds:latest",
volumes=[SandboxVolume(volume=data.name, path="/data")],
network=False,
)
# Model, tools and loop: the shell and file tools run in the sandbox
agent = await (
AgentBuilder("anthropic/claude-haiku-5-5")
.instruction("Your files are in /data. Save what you make there.")
.sandbox(sandbox)
.context_manager(max_input_tokens=50_000, preserve_recent_turns=2)
.build()
)
# One turn: every tool call and result is yielded as it completes
async for output in agent.run(
"Chart refunds by month from /data/refunds.csv and save the chart to /data/refunds_by_month.png."
):
for part in output["message"]["contents"]:
if part["type"] == "text":
print(part["text"])
# Runtime state: save the history to carry on later, copy the chart out of the volume
Path("history.json").write_text(json.dumps(agent.history))
Path("refunds_by_month.png").write_bytes(await data.read_file("refunds_by_month.png"))
await agent.close()
await sandbox.remove()
asyncio.run(main())

The agent works inside the VM: the script it writes and its scratch files stay there. It reaches refunds.csv only through the refunds-data volume, and your code copies the chart back out when the turn ends. The model key, the loop and the history stay in your program, out of the agent’s reach. The sandbox is yours too: the agent never starts, stops or removes it, so your code decides when it goes.

Conclusion

Whatever framework you pick, every agent you build has to place its model, loop, tools, sandbox and runtime somewhere, and most of the decisions that make it safe and reliable are placement decisions. Keep the keys with the loop in your program, run the commands the model writes in a sandbox that sees only what the task needs, and decide up front what state carries over. To try that layout on your own machine, start with the Ailoy quick start.

FAQs

What are the main components of an AI agent?

A model, a loop, tools, a sandbox and a runtime. The model decides what to do, the loop runs its tool calls, the tools are the actions available, the sandbox is where commands and code run, and the runtime keeps the history and state between turns and runs.

Is the LLM the same as the agent?

No. The LLM is one part of the agent. It writes answers and tool requests, and the loop around it runs the tools, feeds back the results and decides when to stop.

Does every AI agent need a sandbox?

No. An agent that only answers questions or calls your own APIs doesn’t run any code it wrote. Once it runs shell commands, edits files or executes code, it needs a sandbox so those actions can’t reach the rest of your machine.

Should the API key go inside the sandbox?

No. The sandbox runs code the model wrote, so a prompt injection there could read anything it holds. Keep the model key and your tools’ credentials in your program, with the loop.

Can I switch models in an Ailoy agent without changing its architecture?

Yes. The model is a string in AgentBuilder, such as anthropic/claude-haiku-5-5. Change it to move between Claude, GPT, Gemini or an open model such as Qwen, and the agent gets the shell and file tools that suit that model in the same sandbox.

Is Ailoy an agent framework or a sandbox?

It covers both. Ailoy is an agent SDK with the loop and tools built in, and through virtx it gives every agent a Linux VM of its own as its sandbox, so there is no separate sandbox service to wire in.

How is Ailoy different from a hosted sandbox like E2B or Daytona?

E2B and Daytona are sandbox services: in the usual setup you sign up, get an API key and connect your own agent loop to sandboxes that run on their cloud. Ailoy is the agent SDK and the sandbox together. Its VM boots on your own machine through virtx with no account, and .sandbox(sandbox) hands it to the agent in one call.

Run your first agent
in its own microVM

Install the package, name a model and run it