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.
- Beginner: Agent Architecture
Under the hood, almost every agent has the same five parts. Learning them once lets you understand any agent framework later.
| Part | Job | Analogy |
|---|---|---|
| Instructions | Rules and role: who the agent is and what it must never do | A job description |
| LLM (reasoning engine) | Reads the situation and decides the next action | The brain |
| Tools | Functions the agent can call | Hands and senses |
| State / memory | What has happened so far and what is known | A notebook |
| Loop controller | Your code that runs the cycle and enforces limits | A 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.
- Beginner: The Observe → Reason → Act Loop
The agent loop is a simple cycle that repeats:
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.
- 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.
Remember: Give the model the judgment work and give tools and code the exact work.
- 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 item | Example | Why it matters |
|---|---|---|
| Goal | Reach 22 degrees | Tells the agent when to stop |
| Status | created, running, succeeded, failed | Lets your code control the lifecycle |
| Step counter | 7 of 10 | Prevents endless loops |
| Observations / history | Every tool call and result so far | Becomes the context for the next model call |
| Scratch data | Intermediate values, found IDs | Saves recomputing |
- 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:
| Stage | What happens | Typical exit |
|---|---|---|
| Created | Goal and tools are set, state is empty | Start the loop |
| Running | Observe, reason, act repeat | Goal reached, error, or step limit |
| Succeeded | The model reports the goal is done and the result is checked | Return the answer |
| Failed | Step limit, repeated errors or an unsafe action | Return an error and the trace |
| Waiting (optional) | Paused for a human or a slow tool | Resume 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.
- 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.
Click to see what a step limit preventsCompare ▼
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.
- 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.
# 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"])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
Key Points
The Big Picture
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.