How AI Agents Work

Understand the architecture of an AI agent: the observe, reason, act loop, the LLM as the reasoning engine, agent state and the basic lifecycle from start to finish, with a working loop in Python.

25 minBeginnerCode Examples

The Core Idea: Every agent is built around one loop: observe the situation, reason about what to do, act, then observe again. The LLM is the reasoning engine, and the state is everything the agent remembers about the task so far.

  1. Beginner: Agent Architecture

Under the hood, almost every agent has the same five parts. Learning them once lets you understand any agent framework later.

PartJobAnalogy
InstructionsRules and role: who the agent is and what it must never doA job description
LLM (reasoning engine)Reads the situation and decides the next actionThe brain
ToolsFunctions the agent can callHands and senses
State / memoryWhat has happened so far and what is knownA notebook
Loop controllerYour code that runs the cycle and enforces limitsA manager with a stopwatch

Key idea: The model does not run by itself. Your code calls the model, runs the tools it asks for, updates the state and decides when to stop.

  1. Beginner: The Observe → Reason → Act Loop

The agent loop is a simple cycle that repeats:

1
Observe: Collect the latest information: the user goal, tool results, new messages, the current state.
2
Reason: The LLM decides what to do next, or decides that the goal is reached.
3
Act: Your code runs the chosen tool or sends the final answer.
4
Repeat: The result becomes the next observation.

Why a loop? A single model call has no way to look at the result of its own action. The loop is what lets the agent see the consequences and adapt, for example by retrying with different input.

  1. Medium: The LLM as the Reasoning Engine

The LLM is not the whole agent. It plays one role: given the instructions, the history and the list of tools, it outputs the next step, either a tool call or a final answer. It does this by predicting text, so its output must be checked by your code.

What the LLM does wellStrengths
Understand fuzzy language
Choose between tools
Write plans and summaries
Adapt when results surprise it
What it needs help withLimits
Exact arithmetic and live facts (use tools)
Remembering beyond its context window (use memory)
Guaranteeing safety (use guardrails)
Stopping on its own (use step limits)

Remember: Give the model the judgment work and give tools and code the exact work.

  1. Medium: Agent State

State is the agent's working memory for one task. It is usually a small data structure that your loop updates after every step.

State itemExampleWhy it matters
GoalReach 22 degreesTells the agent when to stop
Statuscreated, running, succeeded, failedLets your code control the lifecycle
Step counter7 of 10Prevents endless loops
Observations / historyEvery tool call and result so farBecomes the context for the next model call
Scratch dataIntermediate values, found IDsSaves recomputing
State vs. memory
State lives for one task. Long-term memory across tasks is a separate topic covered later in this series.

  1. Advanced: The Basic Agent Lifecycle

A run of an agent moves through clear stages. Making them explicit makes your agent easier to debug and control:

StageWhat happensTypical exit
CreatedGoal and tools are set, state is emptyStart the loop
RunningObserve, reason, act repeatGoal reached, error, or step limit
SucceededThe model reports the goal is done and the result is checkedReturn the answer
FailedStep limit, repeated errors or an unsafe actionReturn an error and the trace
Waiting (optional)Paused for a human or a slow toolResume with new input

Always set a step limit. Without a maximum number of steps, a confused agent can loop forever and run up your bill. The code below includes the limit in its state.

  1. Visual Explanation: The Agent Loop and Its Five Parts

The figure shows one turn of the loop, then lists the five parts every agent shares.

Part 1 · The LoopRound and round until done
ObserveInput
Goal and state
↓
Latest tool result
↓
What is true right now?
ReasonLLM
Reads instructions and history
↓
Picks the next action
↓
Tool call or final answer
ActYour code
Runs the tool
↓
Updates the state
↓
New observation for the next turn
Part 2 · The Five PartsSame in every agent
Instructions
Role and rules
What it is and must never do.
LLM
Reasoning
Chooses the next step.
Tools
Acting
Functions it can call.
State
Memory
What happened so far.
Controller
Your code
Runs the loop and limits.
Click to see what a step limit preventsCompare ▼
If the target can never be reached, the loop would run forever. A maximum step count turns that into a clean failed status. The code below shows a status moving from created to running to succeeded or failed.

Part 1 reads left to right and then starts again. Part 2 lists the parts. Open the bar to see why every loop needs a step limit.

  1. Python Implementation: An Observe, Reason, Act Loop With State

This script is a tiny thermostat agent. It observes the temperature, reasons about what to do, and acts, while a state dictionary tracks the goal, the step count, the observations and the lifecycle status.

observe_reason_act.pyPython 3.10+ · standard library only
# The Observe -> Reason -> Act loop with explicit agent state and lifecycleroom = {"temp": 15.0} def observe():    return room["temp"] def reason(observation, target):    if abs(observation - target) <= 0.5:        return "stop"    return "heat" if observation < target else "cool" def act(action):    room["temp"] += 1.0 if action == "heat" else -1.0 state = {"status": "created", "goal": 22.0, "steps": 0, "observations": [], "max_steps": 10}state["status"] = "running" while state["status"] == "running":    obs = observe()                                   # 1. Observe    state["observations"].append(obs)    decision = reason(obs, state["goal"])             # 2. Reason    state["steps"] += 1    print("Step", state["steps"], "| sees", obs, "| decides", decision)    if decision == "stop":        state["status"] = "succeeded"    elif state["steps"] >= state["max_steps"]:        state["status"] = "failed"    else:        act(decision)                                 # 3. Act print("Lifecycle ended with status:", state["status"], "after", state["steps"], "steps")print("Everything the agent remembers:", state["observations"])
EXPECTED OUTPUT (your numbers may differ slightly)
Step 1 | sees 15.0 | decides heat
Step 2 | sees 16.0 | decides heat
Step 3 | sees 17.0 | decides heat
Step 4 | sees 18.0 | decides heat
Step 5 | sees 19.0 | decides heat
Step 6 | sees 20.0 | decides heat
Step 7 | sees 21.0 | decides heat
Step 8 | sees 22.0 | decides stop
Lifecycle ended with status: succeeded after 8 steps
Everything the agent remembers: [15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0]

The agent warms the room one step at a time and stops as soon as it is within tolerance of the goal. Notice that the state holds everything it remembers. If you set the goal to something unreachable, the step limit would end the run with the failed status.

Knowledge Check

Question 1: What does the Reason step of the loop do?
Question 2: Why should every agent have a step limit?

Key Points

✓An agent has instructions, an LLM, tools, state and a loop controller.
✓The loop is observe, reason, act, then observe the result again.
✓The LLM is the reasoning engine. Your code runs tools, updates state and enforces limits.
✓State holds the goal, status, step count and history for one task.
✓The lifecycle runs created, running, then succeeded or failed.
✓Always set a maximum number of steps.

The Big Picture

How Agents Work in One Line

Observe → Reason (LLM) → Act (tool) → update state → repeat → stop at the goal or the limit

Frameworks differ in names and style, but they all implement this same loop.

Up Next: The agent's hands are its tools. In Tools and Function Calling you will learn how tools are described, chosen and run.