Contents
AI Agent Architecture Diagram
Hover over any component to learn what it does
context window
if task continues
Need more steps? → Loop again
returned to user
when goal is met
pgvector · Mem0
1 What Is an AI Agent?
An AI Agent is an autonomous system that perceives its environment, reasons about its goals, takes actions — including calling external tools — and iterates until a task is complete. Unlike a simple chatbot that responds to a single prompt, an agent can execute multi-step workflows across dozens of actions with minimal human intervention.
At its core, an agent combines four primitive capabilities:
Key distinction: An LLM generates text. An AI agent acts — it uses tool calls, reads results, updates its state, and loops until the goal is achieved or a stopping condition is met.
2 The Perception-Reasoning-Action Loop
The agentic loop is the heartbeat of every AI agent. It repeats until the task is complete or a stopping condition is met.
# Simplified agent execution loop
def run_agent(task, max_steps=20):
memory = AgentMemory()
context = build_context(task, memory)
for step in range(max_steps):
# REASON: LLM picks next action
response = llm.complete(context)
if response.is_final_answer:
return response.output
# ACT: Execute tool call
tool_result = execute_tool(
response.tool_name,
response.tool_args
)
# OBSERVE: Feed result back
context = update_context(
context, tool_result
)
memory.store(step, tool_result)
return "Max steps reached"
3 Short-Term Memory
Short-term memory is the agent's active working context — everything present in the LLM's context window during the current task execution. It is the fastest and most accessible form of memory but is strictly bounded by the model's token limit.
What lives in the context window?
Context Window Constraint: Models have fixed context window sizes (e.g., 128K–2M tokens). As the context fills, older content must be selectively dropped or summarized. This is why long-term external memory is critical for extended tasks.
4 Long-Term Memory
Long-term memory is persistent storage outside the context window that the agent can selectively read from and write to. This enables agents to recall facts, preferences, and past experiences across different sessions and even different users.
# Write to long-term memory after a task
async def save_to_memory(agent_id, content, memory_type):
embedding = await embedder.embed(content)
await vector_db.upsert({
"id": uuid(), "vector": embedding,
"payload": { "agent_id": agent_id, "content": content,
"memory_type": memory_type, "ts": now() }
})
# Retrieve relevant memories before a task step
async def retrieve_memories(query, agent_id, top_k=5):
q_vec = await embedder.embed(query)
results = await vector_db.search(
vector=q_vec, filter={"agent_id": agent_id}, limit=top_k
)
return [r.payload["content"] for r in results]
5 RAG & Vector Databases
Retrieval-Augmented Generation (RAG) is the technique of grounding LLM responses with real, retrieved knowledge — preventing hallucinations and enabling agents to answer questions about private or up-to-date data without retraining the model.
The 5-Step RAG Pipeline
Advanced RAG Patterns
HNSW (Hierarchical Navigable Small World) is the standard indexing algorithm. It builds a layered graph:
- Upper layers: sparse, long-range connections for fast navigation
- Lower layers: dense, local connections for precision
- Search: enter at top, greedily descend toward the query vector
Result: sub-millisecond ANN search across millions of vectors.
6 Planning & Reasoning Patterns
Planning converts a high-level goal into a sequence of executable actions. Modern agents use several established patterns — often combining them within a single workflow.
7 Tool Integration Layer
Tools are the hands of the agent — the interface between the reasoning LLM and the outside world. When a tool is called, the agent's loop pauses, the tool executes, the result is returned as an observation, and reasoning continues.
MCP (Model Context Protocol): Anthropic's open standard for connecting agents to tools and data sources. Provides a universal, language-agnostic interface so any agent can use any tool. Learn MCP →
# Define a tool for the agent
tools = [{
"name": "web_search",
"description": "Search the web for
real-time information",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search query"
},
"max_results": {
"type": "integer",
"default": 5
}
},
"required": ["query"]
}
}]
# LLM emits this structured call:
# { "name": "web_search",
# "arguments": {"query": "GDP 2025"}}
8 Multi-Agent Architecture
Complex tasks often exceed the capabilities of a single agent. Multi-agent systems distribute work across specialized agents that collaborate, check each other's work, and operate in parallel — increasing both capability and reliability.
9 Production Considerations
10 FAQ
What is the difference between an AI agent and an LLM? →
Why use a vector database instead of SQL for agent memory? →
What is RAG and when should I use it vs. fine-tuning? →
How do you prevent an AI agent from running forever? →
What is the ReAct pattern and why is it so widely used? →
How do multi-agent systems avoid duplicating work? →
Related Tutorials
Deep dives into each layer of the AI agent architecture