In this article, you will learn the seven architectural components that separate a production-grade agentic AI system from a demo script, and how each one fits into the agent’s core feedback loop.
Topics we will cover include:
- What each of the seven components — perception, memory, reasoning and planning, tool execution, orchestration, guardrails, and observability — is specifically responsible for.
- Where each component tends to break in real systems, and why that component must be kept separate from the others.
- Focused, runnable Python code illustrating the responsibility of each component in isolation.

Introduction
Most “build an AI agent” tutorials show a 40-line script that calls an LLM in a loop and calls it done. That script works fine for a demo. It does not survive a second concurrent user, a flaky third-party API, or a task that turns out to need twelve steps instead of two.
The gap between the demo and the production system isn’t clever prompting. It’s architecture. Production agentic systems are built from a consistent set of interconnected components: perception, reasoning, planning, memory, tool execution, orchestration, and guardrails. That same component breakdown shows up across nearly every serious architecture writeup, survey paper, and production postmortem published in the last year, regardless of which framework or vendor is doing the writing.
The loop underneath all of it is consistent: Goal → Perception → Reasoning → Planning → Action → Observation → Memory Update → back to Reasoning, repeating until the goal is met, a stop condition fires, or the agent decides it needs a human. This article walks through each piece of that loop as its own component — what it’s responsible for, where it tends to break, and a focused code excerpt that makes the responsibility concrete. Nothing here is wired into one running pipeline. Each piece is shown in isolation, which is also how you should reason about your own system when deciding what it needs.
The Seven Components, at a Glance
Architectural surveys converge on the same core set: Perception, Memory, Reasoning/Planning, Tool Execution, and Orchestration form a closed feedback loop — the cycle that actually runs, step after step. Guardrails and Observability wrap around that entire loop as cross-cutting concerns rather than steps inside the sequence. You don’t “do” guardrails at step 4; guardrails sit between every proposed action and the world, watching every step.
That distinction shapes the rest of this article. The first five sections walk through the loop in the order data actually flows through it. The last two sections cover the wrapper layers that make the loop survivable once real money, real customers, and real side effects are involved.
Turning Raw Input Into Something the Agent Can Reason About
Perception’s job is to transform raw inputs — text, voice, API payloads, sensor data, and file uploads — into a structured representation that the reasoning engine can actually work with. This is the component most tutorials skip entirely, because in a demo, “the user just types text” and there’s nothing to normalize. Real systems take input from webhooks, structured API calls, file uploads, and multiple channels simultaneously, and every one of those needs to land in the same shape before anything downstream can trust it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# perception.py # Prerequisites: none beyond Python's standard library # Run: python perception.py from dataclasses import dataclass, field from typing import Any from enum import Enum import json from datetime import datetime, timezone class InputSource(Enum): USER_TEXT = "user_text" WEBHOOK = "webhook" FILE_UPLOAD = "file_upload" @dataclass class AgentInput: """ The normalized internal shape every downstream component consumes, regardless of where the raw input actually came from. This is the entire point of a perception layer: everything past this point only ever sees this one structure. """ source: InputSource content: str metadata: dict[str, Any] = field(default_factory=dict) received_at: str = field(default_factory=lambda: datetime.now(timezone.utc).isoformat()) def perceive_user_text(raw_text: str) -> AgentInput: """Raw chat input -- the simplest case, but it still needs normalization.""" return AgentInput( source=InputSource.USER_TEXT, content=raw_text.strip(), metadata={"channel": "chat"}, ) def perceive_webhook(raw_payload: str) -> AgentInput: """ A webhook delivers structured JSON, not plain text. Perception extracts the part the agent should reason about and discards transport-level noise like headers and signatures. """ payload = json.loads(raw_payload) event_type = payload.get("event_type", "unknown") description = payload.get("description", "") return AgentInput( source=InputSource.WEBHOOK, content=f"Event '{event_type}' received: {description}", metadata={"event_type": event_type, "raw_payload": payload}, ) def perceive_file_upload(filename: str, file_size_bytes: int, mime_type: str) -> AgentInput: """ A file upload event has no natural-language content at all -- perception has to construct something the reasoning engine can actually use. """ return AgentInput( source=InputSource.FILE_UPLOAD, content=f"User uploaded file '{filename}' ({mime_type}, {file_size_bytes} bytes)", metadata={"filename": filename, "mime_type": mime_type, "size_bytes": file_size_bytes}, ) if __name__ == "__main__": text_input = perceive_user_text(" What's the status of my refund? ") webhook_input = perceive_webhook(json.dumps({ "event_type": "payment_failed", "description": "Card declined for order #4821", })) file_input = perceive_file_upload("invoice_q3.pdf", 184320, "application/pdf") for inp in [text_input, webhook_input, file_input]: print(f"[{inp.source.value}] content='{inp.content}'") print(f" metadata keys: {list(inp.metadata.keys())}\n") |
How to run: python perception.py, no dependencies required.
Three completely different raw shapes — plain text, a webhook JSON payload, and a file-upload event — all collapse into the same AgentInput structure. The reasoning component downstream never needs to know or care which channel something arrived through. That’s the entire value of treating perception as its own component rather than inlining ad hoc parsing wherever input happens to enter the system.
Working Context vs. What Actually Persists
This is the component with the most nuance, and the one demo code gets wrong most often by treating “memory” as just “the conversation so far.” Production memory architecture separates working memory — the immediate context window for the current task — from long-term memory, which itself splits into episodic memory (what happened), semantic memory (facts learned), and procedural memory (skills and how-to knowledge). Short-term memory lives in-context and is essentially free; long-term memory typically lives in a vector store, indexed for semantic retrieval rather than exact match.
The operational distinction matters: working memory is fast and disposable — it evaporates the moment the session ends. Episodic memory gives the agent something working memory structurally cannot provide: hindsight across sessions, the ability to recall “we handled something like this before, and here’s what happened.”
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# memory.py # Prerequisites: none beyond Python's standard library # Run: python memory.py from dataclasses import dataclass, field from datetime import datetime, timezone @dataclass class WorkingMemory: """ Working memory: the immediate context for the CURRENT task only. Lives in-process, bounded by a turn limit, and is gone the moment the session ends. This is what most demo code calls "memory" -- but it's only one piece of the real picture. """ max_turns: int = 10 turns: list[dict] = field(default_factory=list) def add_turn(self, role: str, content: str) -> None: self.turns.append({"role": role, "content": content}) if len(self.turns) > self.max_turns: self.turns.pop(0) # Oldest turn drops off once the limit is hit def as_context(self) -> str: return "\n".join(f"{t['role']}: {t['content']}" for t in self.turns) @dataclass class EpisodicMemoryEntry: """A single stored episode -- what happened, when, and its embedding for later recall.""" timestamp: str summary: str embedding: list[float] # In production this comes from a real embedding model class EpisodicMemory: """ Episodic memory: persists ACROSS sessions, stored externally (a vector store in production), and retrieved by semantic similarity rather than recency. This is what gives an agent "hindsight" -- a capability working memory structurally cannot have, since it's gone the instant the session ends. """ def __init__(self): self._store: list[EpisodicMemoryEntry] = [] def record_episode(self, summary: str, embedding: list[float]) -> None: self._store.append(EpisodicMemoryEntry( timestamp=datetime.now(timezone.utc).isoformat(), summary=summary, embedding=embedding, )) def retrieve_similar(self, query_embedding: list[float], top_k: int = 2) -> list[EpisodicMemoryEntry]: """Real implementations do cosine similarity against a vector index.""" def dot(a, b): return sum(x * y for x, y in zip(a, b)) ranked = sorted(self._store, key=lambda e: dot(e.embedding, query_embedding), reverse=True) return ranked[:top_k] if __name__ == "__main__": wm = WorkingMemory(max_turns=3) wm.add_turn("user", "What's my refund status?") wm.add_turn("agent", "Let me check that for you.") wm.add_turn("user", "It's order 4821") wm.add_turn("agent", "Found it -- refund is processing") # pushes the first turn out print("Working memory (bounded to last 3 turns):") print(wm.as_context()) em = EpisodicMemory() em.record_episode("User asked about refund for order 4821, resolved successfully", [0.9, 0.1, 0.0]) em.record_episode("User asked about shipping delay for order 1190", [0.1, 0.9, 0.0]) em.record_episode("User asked about refund eligibility for order 7734", [0.85, 0.15, 0.0]) similar = em.retrieve_similar([0.88, 0.12, 0.0], top_k=2) print("\nEpisodic memory -- retrieved by similarity to a new refund query:") for entry in similar: print(f" {entry.summary}") |
How to run: python memory.py, no dependencies required.
Working memory drops its oldest turn once the limit is hit, and the first exchange about checking the status is gone by the end of the session. Episodic memory does the opposite: it surfaces the two refund-related episodes out of three stored entries, ranked by meaning, not by when they happened. That’s the structural line between the two — one is a sliding window, the other is a searchable archive.
Reasoning and Planning (Deciding What to Do Next)
Reasoning and planning take the current goal, the perceived input, and whatever memory was retrieved, and produce a plan — sometimes a single next action, sometimes a multi-step decomposition. This is the agent’s cognitive core, consulting memory and knowledge resources to synthesize action plans that get handed off to the execution module.
The critical design point, easy to miss: planning’s responsibility ends at producing the plan. It does not call a tool, touch an API, or have any side effects. That separation is deliberate, and it’s what makes the next component — tool execution — independently testable and independently guardable.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# planning.py # Prerequisites: none beyond Python's standard library # Run: python planning.py from dataclasses import dataclass, field import json @dataclass class PlanStep: step_number: int description: str tool_tag: str # which category of tool this step will need -- execution decides HOW @dataclass class Plan: goal: str steps: list[PlanStep] = field(default_factory=list) def mock_llm_plan(goal: str) -> str: """ Mock LLM call standing in for a real planning call. The point being demonstrated is structural: planning PRODUCES a plan object -- it does not execute anything. Execution is a separate component (next section) that the plan gets handed off to. """ if "refund" in goal.lower(): return json.dumps({ "steps": [ {"step_number": 1, "description": "Look up the order by ID", "tool_tag": "database_lookup"}, {"step_number": 2, "description": "Check refund eligibility against policy", "tool_tag": "policy_check"}, {"step_number": 3, "description": "Issue the refund if eligible", "tool_tag": "payment_api"}, {"step_number": 4, "description": "Notify the customer of the outcome", "tool_tag": "email"}, ] }) return json.dumps({"steps": [ {"step_number": 1, "description": "Search knowledge base for the answer", "tool_tag": "search"}, ]}) def create_plan(goal: str) -> Plan: """Take a goal, produce a structured plan. Nothing here has a side effect.""" parsed = json.loads(mock_llm_plan(goal)) return Plan(goal=goal, steps=[PlanStep(**s) for s in parsed["steps"]]) if __name__ == "__main__": for goal in ["Process a refund for order 4821", "What are your business hours?"]: plan = create_plan(goal) print(f"Goal: {plan.goal}") for step in plan.steps: print(f" Step {step.step_number}: {step.description} [tool_tag={step.tool_tag}]") print() |
How to run: python planning.py, no dependencies required.
The refund goal produces a four-step plan; the business-hours question produces one. Neither call executed a single tool — both just returned a Plan object describing what should happen next. That object is the handoff artifact between reasoning and the rest of the pipeline, which is exactly why orchestration (covered later) can choose to pause, modify, or reject a plan before anything in it actually runs.
Tool Execution
Tool execution connects agents to external systems — APIs, databases, and services — handling the mechanics of invoking a capability and feeding the result back into the reasoning process. It’s also where most production incidents actually originate, because it’s the only component in the loop with real, external side effects.
The constraint is worth stating in plain numbers: at a 5% per-action failure rate, an agent taking 20 actions in a run will fail often enough to be unusable without guardrails. That single statistic is why tool execution can’t just be “call the API and hope” — it needs validation, a timeout, and idempotency as baseline requirements, not nice-to-haves.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# tool_execution.py # Prerequisites: none beyond Python's standard library # Run: python tool_execution.py import time import hashlib from dataclasses import dataclass from typing import Callable, Any, Optional @dataclass class ToolResult: success: bool output: Any = None error: Optional[str] = None idempotency_key: Optional[str] = None from_cache: bool = False class ToolExecutor: """ The non-negotiable basics for tool execution: validate inputs before calling anything, enforce a timeout so a slow call can't hang the whole run, and make side-effecting calls idempotent so a retry doesn't double-charge a customer or double-send an email. """ def __init__(self, timeout_seconds: float = 5.0): self.timeout_seconds = timeout_seconds self._idempotency_cache: dict[str, ToolResult] = {} def _make_idempotency_key(self, tool_name: str, args: dict) -> str: raw = f"{tool_name}:{sorted(args.items())}" return hashlib.sha256(raw.encode()).hexdigest()[:16] def execute(self, tool_name: str, tool_fn: Callable, args: dict, required_args: list[str], idempotent: bool = False) -> ToolResult: # 1. Validate before touching anything external missing = [a for a in required_args if a not in args] if missing: return ToolResult(success=False, error=f"Missing required args: {missing}") # 2. Idempotency -- a retried call with identical args returns the cached result idem_key = self._make_idempotency_key(tool_name, args) if idempotent else None if idem_key and idem_key in self._idempotency_cache: cached = self._idempotency_cache[idem_key] return ToolResult(success=cached.success, output=cached.output, idempotency_key=idem_key, from_cache=True) # 3. Execute against a timeout budget start = time.monotonic() try: output = tool_fn(**args) except Exception as e: return ToolResult(success=False, error=str(e), idempotency_key=idem_key) if (time.monotonic() - start) > self.timeout_seconds: return ToolResult(success=False, error=f"Tool exceeded {self.timeout_seconds}s timeout", idempotency_key=idem_key) result = ToolResult(success=True, output=output, idempotency_key=idem_key) if idem_key: self._idempotency_cache[idem_key] = result return result def issue_refund(order_id: str, amount: float) -> str: return f"Refunded ${amount} for order {order_id}" if __name__ == "__main__": executor = ToolExecutor(timeout_seconds=1.0) # Missing arg -- caught before execution r1 = executor.execute("issue_refund", issue_refund, {"order_id": "4821"}, ["order_id", "amount"]) print(f"Missing arg test: success={r1.success}, error='{r1.error}'") # First call executes, retry with identical args hits the idempotency cache args = {"order_id": "4821", "amount": 49.99} r2a = executor.execute("issue_refund", issue_refund, args, ["order_id", "amount"], idempotent=True) r2b = executor.execute("issue_refund", issue_refund, args, ["order_id", "amount"], idempotent=True) print(f"\nFirst call: from_cache={r2a.from_cache}, output='{r2a.output}'") print(f"Retry, same args: from_cache={r2b.from_cache}, output='{r2b.output}'") |
How to run: python tool_execution.py, no dependencies required.
The retry with identical arguments returns the cached result instead of calling issue_refund a second time. The customer gets refunded once, not twice, even if the orchestrator above it retries the step after a transient network blip. That’s the entire purpose of building idempotency into the execution layer rather than hoping the orchestrator never retries.
Orchestration
Orchestration holds the loop together across multiple steps and, in multi-agent systems, across multiple agents — deciding when to continue, when a step’s outcome should change the path, and when the run is actually finished. This is the layer that has matured fastest recently, with LangGraph, CrewAI, and AutoGen now handling production-grade coordination rather than every team hand-rolling their own loop from scratch.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# orchestrator.py # Prerequisites: none beyond Python's standard library # Run: python orchestrator.py from dataclasses import dataclass, field @dataclass class PlanStep: step_number: int description: str tool_tag: str @dataclass class Plan: goal: str steps: list[PlanStep] = field(default_factory=list) @dataclass class StepOutcome: step_number: int success: bool output: str # Stand-ins for the planning and tool-execution components above -- # orchestration's job is to CALL these in sequence, not do their work itself. def mock_create_plan(goal: str) -> Plan: return Plan(goal=goal, steps=[ PlanStep(1, "Look up order", "database_lookup"), PlanStep(2, "Check eligibility", "policy_check"), PlanStep(3, "Issue refund", "payment_api"), ]) def mock_execute_tool(step: PlanStep) -> StepOutcome: if step.tool_tag == "policy_check": # simulate a failure to test stop logic return StepOutcome(step.step_number, success=False, output="Policy check failed: order too old") return StepOutcome(step.step_number, success=True, output=f"Completed: {step.description}") class Orchestrator: """ Holds the sequence together across multiple steps, decides whether to continue or stop, and caps the run with an explicit step limit. It does not plan and it does not execute tools -- it calls the components that do. """ def __init__(self, max_steps: int = 10): self.max_steps = max_steps def run(self, goal: str) -> list[StepOutcome]: plan = mock_create_plan(goal) outcomes: list[StepOutcome] = [] for step in plan.steps: if len(outcomes) >= self.max_steps: print(f" Step limit ({self.max_steps}) reached -- stopping.") break print(f" Executing step {step.step_number}: {step.description}") outcome = mock_execute_tool(step) outcomes.append(outcome) if not outcome.success: print(f" Step {step.step_number} failed: {outcome.output}") print(f" Stopping run -- a failed step blocks the rest of this plan.") break return outcomes if __name__ == "__main__": outcomes = Orchestrator(max_steps=10).run("Process a refund for order 4821") print(f"\nTotal steps attempted: {len(outcomes)}") print(f"Final outcome: success={outcomes[-1].success}, output='{outcomes[-1].output}'") |
How to
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.