Course
Give your AI a permanent memory of your business. A course for people who use ChatGPT or Claude daily.Compound Context
Jun 30, 2026Articles8 min read

The Anatomy of an AI Agent: Tools, Memory, Planning, and the Loop

NBNikolas Barwicki
AI AgentsAgent ArchitectureTool CallingLLMsAI Engineering

Every AI agent, from a simple ReAct loop to a multi-agent swarm, is built from the same four components. Here is what they are and why each one matters.

The word "agent" gets slapped onto everything now. Chatbots, copilots, workflow automations, glorified cron jobs — all agents, apparently. Strip away the marketing and every genuine AI agent is built from the same handful of components: a loop, tools, memory, and planning.

Understanding these parts matters more than learning any specific framework. Frameworks change every few months. The architecture underneath does not.

This article dissects each component so you can look at any agent — whether built with LangGraph, CrewAI, the OpenAI Agents SDK, or raw API calls — and immediately understand what is happening under the hood.

The Agent Loop: Where Everything Starts

A standard LLM call is a one-shot transaction. Prompt in, response out, done. An agent turns that into a cycle.

The loop follows the same pattern everywhere:

  1. Observe — receive input (user message, tool result, environment state).
  2. Think — the LLM reasons about what to do next.
  3. Act — call a tool, return an answer, or ask a clarifying question.
  4. Repeat — feed the action's result back in and let the LLM decide again.

This is sometimes called the ReAct pattern, from the original paper by Yao et al. that formalized interleaving reasoning traces with actions. The name stuck because it captures the essence: Reason, then Act.

The loop is the skeleton. Without it, you just have an LLM that answers once and stops. With it, the model can chain multiple steps together — checking a database, processing the result, calling another API, and then composing a final answer.

Every framework implements this loop differently on the surface. The Vercel AI SDK gives you a maxSteps parameter. LangGraph models it as a state machine. Raw API code uses a while True loop. But the underlying mechanic is identical: call the model, check if it wants to act, execute the action, feed the result back, repeat.

Tools: How Agents Touch the Real World

An LLM generates text. That is all it does natively. It cannot query your database, check the weather, or send an email. Tools bridge that gap by letting the model request that your code executes a function on its behalf.

Every tool has three parts:

  • A name — how the model refers to it (get_weather, search_database).
  • A description — natural language explaining what the tool does and when to use it. This is effectively a prompt for the model.
  • A parameter schema — a JSON Schema defining the inputs the tool accepts.

The model never executes tools directly. It outputs a structured request — "I want to call get_weather with city: Tokyo" — and your code handles the actual execution. This is a critical safety boundary: you control what runs.

Tool descriptions are more important than most developers realize. A vague description like "searches stuff" forces the model to guess when to use it. A precise description like "searches the company knowledge base for internal policy documents given a natural language query" gives the model the context it needs to pick the right tool at the right time.

For a deeper dive into how this works at the API level, see What Is Tool Calling?. Both OpenAI and Anthropic document their tool-calling APIs extensively.

Memory: Short-Term, Long-Term, and Everything Between

Without memory, every loop iteration starts from zero. The model would forget the tool result it just received. Memory is what gives an agent continuity — the ability to build on previous steps and past interactions.

There are two distinct types:

Short-Term Memory (Context Window)

This is the conversation history: every user message, assistant response, and tool result appended to a growing list of messages. Each loop iteration, the full history gets sent back to the model so it can see what happened before.

Short-term memory is simple and powerful, but it has a hard ceiling: the context window. Once the message history exceeds the model's token limit, you have to summarize, truncate, or offload older messages. This is why context window size (128K tokens for GPT-4o, 200K for Claude) matters so much for agents running complex multi-step tasks.

Long-Term Memory (Persistent Storage)

For knowledge that needs to survive beyond a single session, agents use external storage:

  • Vector databases like Pinecone or Chroma store embeddings for semantic retrieval — the foundation of RAG (Retrieval-Augmented Generation).
  • Key-value stores hold structured facts: user preferences, previous decisions, account details.
  • Conversation logs let agents reference interactions from days or weeks ago.

Long-term memory turns a forgetful assistant into one that learns and adapts. It is also what makes the difference between a demo and a production system. Our article on whether RAG is still relevant explores when you need retrieval versus when a large context window is enough.

Planning: How Agents Break Down Complex Tasks

Ask a basic agent to "plan a three-day trip to Tokyo" and it will try to answer in a single generation. Ask an agent with planning capabilities and it will decompose the task: research flights, find hotels, check weather, build an itinerary, then synthesize everything.

Planning is the component that separates simple tool-calling loops from agents that handle genuinely complex work.

Common planning strategies include:

  • Chain-of-thought reasoning — the model explicitly writes out its thinking step by step before acting. The original research by Wei et al. showed this dramatically improves performance on multi-step problems.
  • Plan-then-execute — the model first generates a full plan (a list of steps), then executes each step sequentially. If a step fails, it can re-plan.
  • Hierarchical decomposition — complex goals get broken into sub-goals, each handled by a specialized sub-agent or tool chain. This is the pattern behind multi-agent systems.

The tradeoff is always autonomy vs. reliability. More planning autonomy means the agent can handle more complex tasks without human intervention. But it also means more room for the model to go off track, hallucinate intermediate steps, or get stuck in loops.

This is why guardrails matter. Maximum iteration limits, input validation, human-in-the-loop checkpoints — these are not optional extras. They are what make planning safe enough for production.

Orchestration: Putting the Components Together

The four components above — loop, tools, memory, planning — do not assemble themselves. Orchestration is the glue: the code that decides how the loop runs, which tools are available, how memory is managed, and whether planning happens explicitly or implicitly.

You have a spectrum of options:

ApproachExampleBest For
Raw API callswhile True + OpenAI/Anthropic SDKLearning, simple agents, full control
Lightweight SDKVercel AI SDK, Anthropic Agent SDKProduction agents, moderate complexity
Full frameworkLangGraph, CrewAI, AutoGenMulti-agent systems, complex state machines

Anthropic's guide on building effective agents makes a strong case for starting simple: use the raw API loop first. Add a framework only when you need features like persistent state, agent handoffs, or complex routing. Most agents do not need a framework — and the ones that do benefit from you understanding the raw mechanics first.

This is also the line between an agent and a workflow. A workflow has a fixed, predetermined execution path. An agent decides its own path at runtime based on the LLM's reasoning. Many production systems are actually workflows dressed up as agents — and that is fine if it meets the requirements.

What Separates Good Agents from Bad Ones

Knowing the components is step one. Making them work reliably is step two.

The gap between a demo agent and a production agent comes down to engineering:

  • Max iteration limits — prevent infinite loops when the model gets stuck. A simple max_steps = 10 saves you from runaway API costs.
  • Error recovery — tools fail. APIs time out. A good agent catches errors, reports them to the model, and lets it try an alternative approach instead of crashing.
  • Structured outputs — constrain the model's responses to valid JSON or predefined schemas when you need predictable downstream processing.
  • Observability — log every loop iteration, every tool call, every token. When an agent misbehaves in production, you need the trace to debug it.
  • Cost controls — each loop iteration costs tokens. Agents that plan poorly burn through API budgets fast. Monitor token usage per task and set hard spending limits.

None of these are glamorous. All of them are necessary.

Conclusion

Every AI agent — from a weekend hack to a production system handling thousands of requests — is assembled from the same building blocks: a loop that drives execution, tools that connect to the outside world, memory that provides continuity, and planning that handles complexity.

The frameworks will keep changing. The architecture will not. Learn the components, build one from scratch, and you will never look at an agent framework as a black box again.

Reach 25,000+ AI enthusiasts every month

Promote your AI tool with featured placement, measurable visibility, and referral traffic.

Learn more →