AI Agent Architecture
Plan AI systems that can use tools, memory, and multi-step workflows.
AI Agent Architecture
Modern AI systems can do more than generate a single response. Within carefully defined boundaries, they can interpret a goal, gather information, choose among approved tools, perform several steps, inspect results, and adapt the next action. Systems designed for this pattern are commonly called AI agents.
AI Agent Architecture is the blueprint for coordinating models, tools, state, memory, control logic, human oversight, security, and operations. The architecture—not the model alone—determines whether an agent behaves reliably in a real workflow.
What Is an AI Agent?
An AI agent is a software system that uses a model to help select and execute actions toward an approved goal. Unlike a basic chatbot that returns text, an agent may call APIs, query databases, update records, create drafts, schedule work, or ask a person for approval.
Agency is a spectrum. A deterministic workflow may use a model only to classify input, while a more flexible agent may decide which tool to use next. Greater autonomy increases the need for limits, observability, evaluation, and human control.
| Pattern | Behavior | Suitable Example |
|---|---|---|
| Single model call | Produces one response | Summarize a supplied document |
| Model-assisted workflow | Fixed steps with model decisions inside them | Classify and route a support ticket |
| Tool-using agent | Selects approved tools over several bounded steps | Research and draft a report |
| Human-supervised agent | Plans and prepares consequential actions for approval | Draft and schedule a customer communication |
| Multi-agent system | Specialized agents coordinate through explicit contracts | Complex work with genuinely separate domains |
When to Use an Agent
- The task requires several steps that vary with intermediate results.
- The correct tool or information source cannot always be selected in advance.
- The workflow benefits from natural-language interpretation and recovery.
- Actions can be constrained, observed, reversed, or approved appropriately.
- The expected value justifies additional latency, cost, and operational risk.
Prefer a deterministic workflow when steps and rules are known. Agentic flexibility is useful only when it improves the outcome enough to justify unpredictability and complexity.
Core Components
User or System Interface
A request may arrive through chat, voice, a website, a business application, an event, or a scheduled job. The boundary authenticates the caller, validates the request, captures constraints, and establishes the goal and authority.
Agent Controller
The controller owns the workflow loop. It assembles context, calls the model, validates the proposed action, enforces limits, invokes approved tools, persists state, handles errors, and decides whether to continue, stop, or escalate.
Model and Planning
A language model can interpret the goal, decompose work, select a tool, fill structured arguments, and summarize observations. Plans should remain bounded and revisable rather than being treated as guaranteed truth.
Tools
Tools provide controlled access to search, databases, calculators, files, email, calendars, ticketing, code execution, or business APIs. Each tool needs a clear schema, narrow purpose, scoped identity, authorization checks, timeout, error contract, and audit policy.
State and Memory
Working state records the current goal, completed steps, tool results, approvals, and remaining budget. Longer-term memory may store approved preferences or reusable facts. Memory must have provenance, scope, retention, correction, deletion, and permission controls.
Knowledge Retrieval
RAG or structured retrieval can provide current, authorized knowledge. Retrieved content is untrusted data and may contain prompt injection, so it must not override system policy or grant tool permissions.
Policy and Guardrails
Deterministic controls enforce identity, authorization, allowed tools, destinations, budgets, data handling, output schemas, approval gates, and stop conditions. A prompt is guidance to a model, not a reliable security boundary.
Observability and Evaluation
Trace model decisions, tool attempts, sanitized arguments and results, state transitions, approvals, latency, tokens, cost, errors, and final outcomes. Evaluation checks whether tasks are completed correctly, safely, efficiently, and within policy.
The Agent Loop
Goal -> Load authorized context -> Decide next action -> Validate policy
^ |
| v
Stop / respond / escalate <- Observe result <- Execute approved tool
Every loop enforces step, time, cost, permission, and safety limits.| Stage | Responsibility |
|---|---|
| Receive | Authenticate the caller and capture goal, context, and constraints |
| Decide | Choose a response, question, plan update, or tool call |
| Validate | Check schema, authorization, risk, budget, and approval requirements |
| Execute | Invoke the tool with scoped credentials and idempotency where needed |
| Observe | Validate and record the result or error |
| Continue | Update state and select the next bounded step |
| Stop | Return a result, ask for clarification, request approval, or fail safely |
Tool Design and Permissions
Tool interfaces should expose the smallest safe capability. A tool named send_email is riskier than separate create_email_draft and send_approved_draft operations because it combines preparation and irreversible execution.
- Use typed schemas, enums, ranges, and maximum sizes.
- Authorize each call using the user and workload identity.
- Use allowlisted operations and destinations rather than arbitrary commands or URLs.
- Separate read, draft, approve, and execute capabilities.
- Make retried write operations idempotent and support rollback where possible.
- Return structured, bounded errors without exposing secrets.
- Log attempted and completed actions with correlation identifiers.
Human Oversight
Human involvement should be designed around consequence and uncertainty. Approval is especially important for financial commitments, external communication, account changes, production modifications, deletion, access grants, regulated decisions, or actions that are difficult to reverse.
| Control | Use |
|---|---|
| Human-in-the-loop | A person must approve before the action executes |
| Human-on-the-loop | A person supervises activity and can interrupt it |
| Human-out-of-the-loop | Only suitable for low-risk, bounded, well-tested actions |
| Escalation | Agent stops when confidence, permissions, data, or tools are insufficient |
| Override and rollback | Authorized people can correct outcomes and recover state |
Memory Architecture
| Memory Type | Purpose | Key Control |
|---|---|---|
| Working memory | Current request and intermediate results | Bounded context and sensitive-data minimization |
| Conversation history | Continuity across turns | User scope, retention, and deletion |
| User preferences | Approved personalization | Consent, correction, and provenance |
| Semantic memory | Retrieved reusable knowledge | Permissions, freshness, and source visibility |
| Workflow state | Durable progress and approvals | Integrity, idempotency, and audit trail |
Do not store everything. Incorrect or malicious memory can persist and influence later actions. Validate writes, isolate tenants, expire unnecessary state, and let authorized users inspect or correct durable information where appropriate.
Security Threats and Controls
| Threat | Example Controls |
|---|---|
| Prompt injection | Separate trusted instructions, restrict tools, validate every action |
| Excessive agency | Narrow scope, step and cost limits, approval gates, kill switch |
| Privilege escalation | Per-call authorization, scoped workload identities, tenant isolation |
| Data leakage | Minimization, redaction, output checks, safe logs, permission-aware retrieval |
| Malicious tool result | Treat observations as untrusted, parse schemas, sanitize content |
| Unintended duplicate action | Idempotency keys, transaction state, confirmation and rollback |
| Denial of wallet or service | Budgets, quotas, timeouts, bounded retries, rate limits |
| Memory poisoning | Validated writes, provenance, review, expiration, isolation |
Failure Handling and Recovery
- Define maximum steps, wall-clock time, tokens, tool calls, and financial spend.
- Use deadlines, bounded retries with backoff, and circuit breakers.
- Checkpoint durable workflows after completed actions.
- Distinguish retryable failures from invalid or forbidden requests.
- Compensate or roll back completed actions when the business process supports it.
- Stop safely when observations conflict, permissions are missing, or progress stalls.
- Provide a clear status and handoff package when escalating to a person.
Single-Agent and Multi-Agent Designs
Start with one agent or a deterministic workflow. Multiple agents can help when domains, permissions, contexts, or ownership are genuinely distinct, but they add communication, coordination, debugging, latency, cost, and security complexity.
Coordinator -> Research specialist
-> Data specialist
-> Review specialist
-> Shared policy, state, evidence, and human approvalMulti-agent communication needs typed contracts, authenticated identities, scoped tools, bounded delegation, shared correlation IDs, conflict resolution, and one accountable owner for the final outcome.
A Simple Analogy
A personal assistant asked to schedule a meeting must understand constraints, inspect permitted calendars, propose times, obtain approval when needed, send invitations, handle conflicts, and report the result. The assistant should not invite arbitrary people or change unrelated appointments.
An AI agent follows the same pattern: it coordinates actions toward a goal, but authority, limits, verification, and escalation determine whether that coordination is safe and useful.
Python Example
This deterministic example illustrates tool registration, validation, and a bounded decision. A production agent would also authenticate callers, authorize each tool call, persist workflow state, require approvals, and emit secure traces.
from typing import Callable
def get_weather(location: str) -> str:
if not location or len(location) > 100:
raise ValueError("A valid location is required")
return f"Forecast for {location}: rain"
def calculate(expression: str) -> str:
allowed = set("0123456789+-*/(). " )
if not expression or not set(expression) <= allowed:
raise ValueError("Unsupported expression")
return str(eval(expression, {"__builtins__": {}}, {}))
TOOLS: dict[str, Callable[[str], str]] = {
"weather": get_weather,
"calculator": calculate,
}
def run_agent(tool_name: str, argument: str) -> str:
tool = TOOLS.get(tool_name)
if tool is None:
return "I cannot perform that action."
return tool(argument)
print(run_agent("weather", "Pune"))Evaluating Agents
| Layer | Example Measures |
|---|---|
| Outcome | Task success, correctness, user acceptance, business result |
| Trajectory | Appropriate plan, tool selection, ordering, and recovery |
| Safety | Policy violations, unauthorized attempts, sensitive-data leakage |
| Efficiency | Steps, tokens, tool calls, latency, and cost per completed task |
| Reliability | Error rate, timeout rate, duplicate actions, recovery success |
| Oversight | Approval quality, escalation rate, override and rollback success |
Use versioned scenarios that include normal tasks, ambiguity, missing information, tool failure, conflicting results, malicious content, denied permissions, repeated requests, and consequential actions. Evaluate both final outcomes and the path taken.
Common Challenges
- Long or looping workflows with no useful progress
- Incorrect tool choice or malformed arguments
- Partial failures and duplicated external actions
- Prompt injection through users, documents, websites, or tool results
- Sensitive information leaking through memory, tools, output, or logs
- Unclear authority between the user, agent, and business system
- High latency, token use, API usage, and infrastructure cost
- Difficulty reproducing and debugging nondeterministic trajectories
- Overcomplicated multi-agent designs without clear benefit
Best Practices
- Define a narrow purpose, allowed users, success criteria, and prohibited actions.
- Prefer deterministic workflows until flexible decision-making is measurably necessary.
- Expose small, typed, allowlisted tools with scoped credentials and per-call authorization.
- Separate read, draft, approve, execute, and rollback capabilities.
- Treat all user, retrieved, memory, model, and tool content as untrusted data.
- Enforce step, time, token, cost, rate, and resource limits outside the model.
- Require human approval for consequential or difficult-to-reverse actions.
- Persist durable state, use idempotency, and design explicit recovery and escalation paths.
- Store only necessary memory with provenance, permissions, retention, and correction controls.
- Trace and evaluate outcomes, trajectories, safety, reliability, and unit cost before expanding autonomy.