How do AI agents work at the core?
How do AI agents work? Underneath every framework and product, the answer is one loop that runs three steps over and over: think, act, observe. The agent thinks about what to do next, acts by calling a tool, observes the result, and then thinks again with that new information. It keeps cycling until the goal is met, a step limit is hit, or it decides it is stuck.
That is the entire mechanism. A language model on its own only does the first step, thinking, and produces text. The loop is what adds the other two. It takes the model's proposed action, actually runs it, captures what came back, and hands that back to the model as the starting point for the next round. If you have read our plain-English definition of an AI agent, this is the machinery under that definition.
The three steps, one at a time
Think. The model receives the current state: the goal, the tools it can use, and everything that has happened so far. It reasons about the situation and outputs a decision. Usually that decision is "call this tool with these arguments," but it can also be "I am done, here is the answer" or "I need to ask the human something."
Act. Your code reads the model's decision and executes it. If the model said "call search_web with query X," the loop runs the actual web search. The model does not touch the internet, the database, or the filesystem directly. It only names an action; the runtime performs it. This separation is what keeps the whole thing controllable.
Observe. The action produces a result: search hits, query rows, an error message, a file's contents. The loop packages that result and appends it to the running history. On the next think step, the model sees it and adjusts. If the search returned nothing useful, the model can try a different query. If a command failed, it can read the error and fix its approach.
This pattern has a name in the research literature. It comes from the ReAct approach, which interleaves reasoning and acting, and you can read the original ReAct paper for the formal version. Nearly every agent framework in 2026 is a variation on it.
The loop in code
Stripped to its essentials, the control loop looks like this. Everything real, the retries, the budgets, the logging, hangs off this skeleton.
def run_agent(goal: str, tools: dict, max_steps: int = 20):
history = [{"role": "user", "content": goal}]
for step in range(max_steps):
# THINK: model proposes the next action
decision = model.decide(history, tools=tools.schemas())
if decision.is_final:
return decision.answer
# ACT: the runtime executes the chosen tool
tool = tools[decision.tool_name]
result = tool.run(**decision.arguments)
# OBSERVE: feed the result back for the next round
history.append({"role": "assistant", "content": decision.raw})
history.append({"role": "tool", "content": str(result)})
return "Stopped: hit the step limit."def run_agent(goal: str, tools: dict, max_steps: int = 20):
history = [{"role": "user", "content": goal}]
for step in range(max_steps):
# THINK: model proposes the next action
decision = model.decide(history, tools=tools.schemas())
if decision.is_final:
return decision.answer
# ACT: the runtime executes the chosen tool
tool = tools[decision.tool_name]
result = tool.run(**decision.arguments)
# OBSERVE: feed the result back for the next round
history.append({"role": "assistant", "content": decision.raw})
history.append({"role": "tool", "content": str(result)})
return "Stopped: hit the step limit."Read it top to bottom and the three steps are right there. model.decide is think. tool.run is act. Appending the result to history is observe. The for loop is what makes it an agent instead of a single question and answer.
A worked example: the deploy-checker agent
Abstract loops are hard to hold in your head, so walk through a real one. You give an agent the goal "check whether the latest deploy is healthy, and if not, tell me why." It has three tools: get_deploy_status, read_logs, and query_metrics.
Here is the loop in motion:
Think: "I should start by checking the deploy status." Act: calls
get_deploy_status. Observe: result says "deployed, but error rate is 4x baseline."Think: "Errors are up. I should read recent logs to see what is failing." Act: calls
read_logsfor the last five minutes. Observe: logs show repeated "connection refused" to the payments service.Think: "Looks like the payments dependency is down. Let me confirm with metrics." Act: calls
query_metricsfor the payments service. Observe: payments service is returning 100% 503s.Think: "I have enough to answer." Act: returns a final summary: the deploy is unhealthy because the payments dependency is down, not because of the new code.
No step was clever in isolation. The intelligence is in the chaining: each observation shaped the next decision. That is the answer to how do AI agents work in practice. They are not one giant leap of reasoning. They are many small, checkable steps.
What the model actually decides
A fair question at this point: if the code runs the tools, what is the model really doing? Two things, and both are hard.
Choosing the next action. Out of every tool available and every possible argument, the model picks one that moves toward the goal. This is where a good reasoning model earns its keep, because a weak one calls the wrong tool or loops in circles.
Knowing when to stop. Deciding "I have enough to answer" or "this is not working, I should ask a human" is genuinely difficult. Many real-world agent failures are stopping failures: the agent quits too early, or never quits at all.
How much of the path the model chooses, versus how much you hard-code, is exactly the axis that separates a rigid workflow from something genuinely agentic. The more the model decides at runtime, the more agentic the system, and the more careful you have to be.
Where the loop gets complicated in production
The skeleton above is honest but incomplete. Real agents wrap it in machinery:
Memory. The full history grows too long to fit in the model's context, so agents summarize old steps or store facts in a database and retrieve them when relevant.
Parallel tools. Some loops fire several tool calls at once, wait for all of them, and observe the batch. An autonomous research agent doing this can read twenty sources in the time a serial loop reads one.
Budgets and timeouts. Caps on steps, tokens spent, and wall-clock time, so a stuck agent fails cheaply instead of running all night.
Error handling. When a tool throws, the loop can feed the error back so the model retries, or bail out if the same error repeats.
None of these change the core. They protect it. The think-act-observe loop is still the engine; production just adds a fuel gauge, a governor, and a seatbelt.
Single-agent loops vs multi-agent systems
The loop described above runs one model. A growing pattern splits the work across several agents, each with its own loop and a narrow job, coordinated by an orchestrator. One agent plans, others handle sub-tasks, and a final one reviews the result.
Why split. A focused agent with five tools makes better decisions than one agent drowning in fifty. Narrow scope means clearer choices and fewer wrong turns.
The cost. Coordination overhead. Agents have to pass context to each other, and a bug in the hand-off is a new failure mode a single loop does not have.
When it pays. Big, decomposable tasks: research that splits into sub-questions, or a build-test-review pipeline where each stage is its own agent.
Under the hood, every one of those agents still runs the same think-act-observe loop. Multi-agent is not a different mechanism. It is many copies of the same mechanism with a coordinator on top.
Why the same loop can fail in three ways
Understanding the loop also means understanding how it breaks, because every failure maps to one of the three steps.
Bad thinking. The model picks the wrong tool or invents an argument. Better models and clearer tool descriptions help most here.
Bad acting. The tool itself fails: a timeout, a permission error, a malformed request. Your loop needs to catch these and feed the error back so the model can adapt instead of crashing.
Bad observing. The result comes back in a form the model misreads, so it draws the wrong conclusion. Returning clean, structured tool results, not raw dumps, prevents a surprising amount of this.
Map any real agent failure to think, act, or observe and you will usually know where to look for the fix.
How context length shapes the loop
Every trip through the loop, the model reads the entire history so far. That history grows with each step, and the model's context window is finite. This single fact drives a lot of agent design.
Early in a task the full history fits and the model sees everything. As the loop runs long, the history threatens to overflow. Agents handle this by summarizing older steps, dropping stale observations, or moving facts into external memory and retrieving only what is relevant. Get this wrong and a long-running agent starts forgetting what it did ten steps ago, then repeats work or contradicts itself. The rule of thumb: the longer you expect a loop to run, the more you have to invest in managing what stays in context. A five-step agent needs none of this. A fifty-step agent lives or dies by it.
A checklist for a healthy loop
When you build or debug an agent loop, walk this list:
Is there a hard cap on steps so it cannot run forever?
Does a tool error get fed back to the model, or does it crash the run?
Is every tool call and result logged so you can replay what happened?
Can the model actually tell when it is done, or does it ramble past the answer?
Are irreversible actions gated so a bad decision cannot do permanent harm?
Most production agent problems trace back to a missing item on this list, not to the model being insufficiently smart.
The takeaway
So how do AI agents work? One model, one set of tools, and one loop that thinks, acts, and observes until the job is done. Everything else, memory systems, frameworks, orchestration layers, is scaffolding around those three steps. Once you can see the loop, agent products stop looking like magic and start looking like what they are: a well-managed loop with good tools and sensible limits. If you want to run one yourself without hand-building the process management, storage, and secret handling around that loop, that infrastructure is the part worth buying rather than building.