Autonomous Systems & AI Engineering 60–90 mins read Intermediate → Advanced • Tool Calling & Agent Loops

AI Agents in Production: Tool Calling, Planning, Memory, Agent Loops & Reliability

Transforming foundation LLMs from passive text generators into proactive, goal-driven autonomous systems requires far more than long system prompts. Production agents combine structured tool calling, dynamic planning, durable working and long-term memory, explicit state machines, least-privilege authorization, deterministic human approvals, and rigorous circuit breakers to deliver verifiable business value safely.

What You Will Master

The Agent Anatomy: Deconstruct $\text{Agent} = \text{Model} + \text{Tools} + \text{State} + \text{ControlLoop}$ and contrast agents with deterministic LLM pipelines.
Tool Calling & Schema Design: Construct JSON Schema tool definitions, validate structured inputs/outputs, and separate read tools from mutating write tools.
Planning & ReAct Loops: Implement task decomposition, Plan-and-Execute, Dynamic Replanning, and Thought-Action-Observation cycles.
Multi-Tier Memory Architecture: Distinguish working memory, conversation context, long-term profile storage, and external knowledge with tenant privacy isolation.
State Machines & Multi-Agent Systems: Model explicit task lifecycles and compare sequential, parallel, and supervisor-worker multi-agent topologies.
Safety, HITL & Circuit Breakers: Enforce least privilege, cryptographic Human-in-the-Loop approvals, idempotency keys, and runaway loop protection.
1

Introduction: The Shift from Static Prompts to Autonomous Action

In previous lessons, we mastered fine-tuning, inference serving with vLLM, and enterprise production architectures with RAG, semantic caching, and AI gateways. In those applications, the execution sequence is largely linear and predefined: a user submits a query, the application retrieves context chunks, passes them to the LLM, validates the output, and returns a response.

An AI Agent operates on an entirely different paradigm. Given a high-level goal, an agent dynamically selects which tools to invoke, observes their outputs, updates its internal state, reasons about the outcome, and decides the subsequent action until the goal is achieved or a stopping condition is met.

// 1. TRADITIONAL STATIC LLM PIPELINE: User Prompt → Ingestion → Context Retrieval (RAG) → LLM Generation → Static Response // 2. DYNAMIC AI AGENT CONTROL LOOP: User Goal → [ Agent Controller ] ←→ [ State & Working Memory ] ↓ [ Model Proposes Action ] ↓ [ Security / Permission / Policy Check ] ↓ [ Execute Tool: Database / API / Search ] ↓ [ Observe Result & Feed to Context ] ↓ ←←←← [ Has Goal Been Reached? ] →→→→ [ Final Response / Completion ] (No: Iterate Next Step)

Consider this enterprise request: "Find order #ORD-1024, check its delivery status, and email the customer with an updated tracking link if the package is delayed."

A traditional LLM explains how a customer support representative might accomplish this. An AI agent, by contrast, executes the entire operational workflow:

  1. Queries the order database for ORD-1024.
  2. Inspects the courier delivery API for transit timestamps.
  3. Evaluates whether the package is delayed against SLA thresholds.
  4. Drafts a personalized apology notification with updated tracking.
  5. Checks organizational policy to determine whether human manager approval is required.
  6. Triggers the authorized notification service upon receiving approval.
  7. Records the completed action in the customer audit ledger.
Cardinal Architecture Rule: An AI agent must never be permitted to execute arbitrary actions directly. Its tools, permissions, budgets, and stopping criteria must be strictly governed by the host application. The model proposes actions; the application authorizes and executes them.
2

What Is an AI Agent? The Core Anatomy

Formally, an AI agent is a software system that uses a foundation model as a reasoning engine to select and execute discrete actions toward a specified goal, observing environmental feedback to guide subsequent iterations.

$$\text{Agent} = \text{Model} + \text{Tools} + \text{State} + \text{ControlLoop} + \text{Policy}$$

Every production agent architecture relies on six distinct architectural responsibilities:

Component Responsibility Production Implementation
Model (Reasoning Engine) Interprets context, decomposes goals, and selects appropriate tool invocations. GPT-4o, Claude 3.5 Sonnet, Llama-3-70B-Instruct
Tools (Actuators) Deterministic functions allowing the agent to query or mutate external systems. REST APIs, SQL DBs, Search engines, Email gateways
State & Memory Maintains task variables, completed steps, tool observations, and dialogue context. Redis hashes, PostgreSQL records, in-memory state dicts
Control Loop (Orchestrator) Repeatedly alternates between model prompting, tool execution, and condition checks. Custom while-loop, LangGraph, Temporal, BullMQ worker
Policy & Guardrails Enforces RBAC permissions, human approvals, budget ceilings, and safety filters. Backend middleware, Pydantic validators, Token buckets
Evaluator Determines whether the task outcome satisfies verifiable acceptance criteria. Deterministic unit assertions, LLM-as-a-Judge verifier

Key Architectural Takeaway: An agent is not simply an LLM with a 4,000-word prompt. Its defining attribute is agency: the delegated capability to select operations dynamically, ingest external tool results, and iteratively adjust its path toward goal completion.

3

Traditional LLM Applications vs. Autonomous AI Agents

Consider an enterprise user requesting: "Find total revenue across EMEA for Q3, compare against our quarterly target, and send a summary PDF to the CFO."

System Dimension Traditional LLM App (RAG / Chain) Autonomous AI Agent
Workflow Sequencing Hardcoded in code: Step 1 → Step 2 → Step 3. Dynamic: The model selects tools based on intermediate results.
Error Recovery Fails or raises an exception if a step returns unexpected data. Can inspect error messages, reformulate tool parameters, or try alternative tools.
Environment Feedback One-shot context injection; no observation loop. Continuous: Observes tool output and uses it in the next reasoning step.
Cost & Latency Highly predictable (1 or 2 LLM calls per request). Variable: Depends on step count, tool latency, and replanning iterations.
Safety Gating Standard API authentication and output sanitization. Requires granular tool RBAC, action budgets, state tracking, and human-in-the-loop gates.

Traditional LLM pipelines can indeed call tools (such as performing a SQL lookup in a RAG step). The critical distinction lies in how much control over the sequence of actions is delegated to the foundation model.

4

The Anatomy of an AI Agent: Goals, Controller & State

4.1 Goal Definition

A production agent requires a verifiable, bounded goal. Vague prompts invite hallucinated tool loops and ambiguous termination conditions.

✗ Ambiguous / Open-Ended Goal: "Help me manage our warehouse stock and fix any problems you find."
Problem: No concrete stopping condition; invites infinite tool exploration and unauthorized data writes.
✓ Verifiable Production Goal: "Find all active SKUs in the 'Electronics' category with fewer than 10 units in stock. Calculate 30-day replenishment amounts and draft a purchase order for manager approval."
Success Criteria: Concrete categories, explicit thresholds, deterministic output schema, and human approval requirement.

4.2 The Agent Controller

The controller is the supervisory backend orchestrator. It is written in deterministic code (Python, TypeScript, Go) and governs:

  • Formatting conversation history and available tool schemas into model prompts.
  • Parsing model responses for structured tool calls.
  • Verifying that the requested tool is allowed under the current user's session token.
  • Executing authorized functions with timeout safeguards.
  • Appending tool observations back into the working context.
  • Enforcing loop iteration limits and token spend budgets.

4.3 Durable Task State

A production agent must not rely solely on the LLM's conversation history as its state store. If an application worker crashes or restarts mid-task, conversation history may be lost. State must be durably stored:

// Durable Task State Schema (Stored in Redis / PostgreSQL) { "task_id": "tsk_98241b", "tenant_id": "org_enterprise_corp", "user_id": "usr_ops_lead_42", "status": "RUNNING", "current_step": 3, "max_steps": 8, "tokens_consumed": 2480, "allocated_budget_usd": 0.50, "accumulated_cost_usd": 0.038, "state_variables": { "order_id": "ORD-1024", "delivery_status": "DELAYED", "carrier_code": "FEDEX_EXPRESS", "tracking_id": "TRK-992019", "revised_eta": "2026-10-10T14:00:00Z", "email_drafted": true, "approval_token": null } }
5

The Agent Control Loop: Reason, Act & Observe

The Agent Loop is the fundamental operational engine of an AI agent. In each iteration, the agent decides whether it requires more information from tools or whether it has collected sufficient evidence to formulate a final response.

[ Start Task ] ↓ [ Prepare Context: Goal + History + State + Tool Registry ] ↓ [ Model Call: Predict Next Step ] ↓ < Does Model Request Tool Call or Final Answer? > / \ / [Tool Request] \ [Final Answer] ↓ ↓ [ Validate Permissions & Schema ] [ Validate Completion Criteria ] ↓ ↓ [ Execute Tool Function ] [ Update State: COMPLETED ] ↓ ↓ [ Format Observation ] [ Return Final Result to User ] ↓ [ Check Limits: Steps < Max? Budget < Cap? ] ↓ ( Yes: Continue Loop → Return to Context )

Consider the catalog query: "Find the five cheapest ultrabooks in our inventory and summarize their specs."

Step Model Thought / Decision Action Invoked Observed Result
1 Need to query product catalog sorted by price ascending for category 'ultrabooks'. search_products(category="ultrabooks", sort="price_asc", limit=5) Returns 5 product records with IDs, titles, and base prices.
2 Product IDs identified: [U1, U2, U3, U4, U5]. Need hardware specifications (RAM, CPU, storage). get_product_specs(product_ids=["U1","U2","U3","U4","U5"]) Returns structured technical specifications for all 5 laptops in a single call.
3 All necessary data acquired. Ready to generate formatted comparison table. FINISH (Final Answer) Generates Markdown comparison table with prices, processors, and recommendations.
Efficiency Principle: Notice that in Step 2, the tool takes a batch of IDs (["U1", "U2", ...]) rather than executing 5 separate tool calls in 5 sequential loop iterations. Excellent tool engineering drastically shrinks agent loop depth, cutting token cost and latency by up to 80%.
6

Tool Calling: Schemas, Validation & Structured Outputs

Tool calling (also termed Function Calling) allows language models to emit structured JSON arguments conforming to an explicit schema, rather than outputting arbitrary text. The model does not execute the function; it outputs an instruction requesting that the application execute it.

6.1 Tool Definition Schema

// JSON Schema Provided to the LLM via API: { "type": "function", "function": { "name": "get_order_details", "description": "Retrieve canonical order metadata, shipping address, and current courier status.", "parameters": { "type": "object", "properties": { "order_id": { "type": "string", "pattern": "^ORD-[0-9]{4,8}$", "description": "Unique order identifier formatted as ORD-XXXX" }, "include_tracking_events": { "type": "boolean", "description": "If true, returns historical courier checkpoints." } }, "required": ["order_id"] } } }

6.2 Tool Design Principles

  • Single Responsibility: Prefer granular, focused tools (get_order_status, cancel_order) over monolithic "do-everything" tools (manage_system(action, payload)).
  • Explicit Parameter Types & Regex: Constrain inputs using strict regex patterns and enums to prevent hallucinated argument variations.
  • Structured JSON Return Values: Always return clean JSON key-value pairs (e.g., {"status": "delayed", "delay_days": 2}) rather than natural language paragraphs. Structured data minimizes token consumption and prevents ambiguous model interpretation.
  • Timeout and Resilience: Every tool execution must wrap external network calls in hard timeouts (e.g., 3,000ms) with circuit-breaker protection.
7

Read Tools vs. Write Tools: The Security Boundary

Not all tools carry equal blast radius. Conflating read operations with write operations in an agent's security model is one of the most critical vulnerabilities in production AI systems.

Read-Only Tools (Non-Mutating)
  • search_inventory(sku, location)
  • get_customer_profile(customer_id)
  • query_analytics_db(sql_query)
  • fetch_policy_doc(doc_id)
Security Posture: Low risk. Can be executed automatically if the authenticated user has session read permissions for the target tenant.
Write Tools (State-Mutating)
  • issue_customer_refund(order_id, amount)
  • update_catalog_price(sku, price)
  • dispatch_external_email(recipient, body)
  • delete_customer_account(customer_id)
Security Posture: High risk. Requires least-privilege role verification, parameter boundary checks, idempotency tokens, and optional human approval.

Production Defense: Even if a model outputs a valid tool call for issue_customer_refund, the backend controller must independently verify:

  1. Does the active user session possess the BILLING_ADMIN role?
  2. Is the refund amount $\le \$500$ (automated policy limit)?
  3. Has a refund already been processed for this order (idempotency check)?
  4. Is the order within the 30-day return policy window?
8

Planning and Task Decomposition

Complex real-world goals cannot be resolved in a single step. An agent must break down a compound objective into a sequence or Directed Acyclic Graph (DAG) of actionable subtasks.

Planning Paradigm How It Operates Best Enterprise Use Case
Fixed Workflow Application code defines every sequential step. The model is called only for extraction or summarization within steps. Compliance-heavy processes, banking transactions, strict SLAs.
Dynamic Step-by-Step (ReAct) The model inspects intermediate observations and chooses the next single tool dynamically. Exploratory debugging, customer support diagnosis, web search.
Plan-and-Execute A Planner model creates a multi-step plan upfront; an Executor agent runs each subtask sequentially. Complex multi-document synthesis, data pipeline generation.
Dynamic Replanning The agent evaluates its original plan against failed tool executions and revises remaining steps on the fly. Uncertain environments, distributed web scraping, API failover.
9

The ReAct Framework: Reasoning & Acting

Pioneered by Yao et al., the ReAct (Reasoning + Acting) framework interleaves verbal reasoning ("Thoughts") with action execution ("Actions") and environmental feedback ("Observations").

Thought 1: "The user wants to know why order ORD-1024 has not arrived. I need to fetch the order record." Action 1: get_order_status(order_id="ORD-1024") Observation 1: {"status": "shipped", "carrier": "FedEx", "tracking": "TRK-901"} Thought 2: "The order is shipped, but expected delivery was yesterday. I must check current courier tracking events." Action 2: query_carrier_api(carrier="FedEx", tracking_id="TRK-901") Observation 2: {"exception": "Severe weather delay at hub Memphis", "revised_eta": "Tomorrow"} Thought 3: "The delay is caused by severe weather in Memphis, and revised delivery is tomorrow. I have sufficient facts." Final Answer: "Your order #ORD-1024 is delayed due to severe weather at the Memphis FedEx transit hub. It is now scheduled for delivery tomorrow."

Production Insight: In user-facing production systems, never expose raw internal reasoning thoughts directly to users. Chain-of-thought tokens may leak prompt instructions or confuse non-technical users. Store thoughts in an internal telemetry span for debugging, and present only polished final answers.

10

Agent Memory Architecture & Tenant Privacy

Production agents require distinct memory subsystems tailored to specific temporal scopes:

1. Working Memory (Task Scope)

Ephemeral state maintained during the active execution loop: current step index, intermediate tool responses, temporary calculation registers. Flushed upon task completion.

2. Conversation Memory (Session Scope)

Recent user and agent dialogue turns within an active conversation session. Managed via sliding token windows, message buffers, or summarization layers.

3. Long-Term Memory (User Scope)

Persistent user preferences, previous resolutions, and recurring requirements stored across days or weeks in durable vector or relational databases (subject to GDPR consent).

4. External Knowledge (Enterprise Scope)

Authoritative corporate knowledge bases, live CRM systems, product catalogs, and policies retrieved dynamically via RAG.

Security & Privacy Safeguards: Long-term memory must never become an unmonitored data lake. Enforce strict tenant partitioning (`tenant_id`), automated data retention TTLs (e.g. 90-day expiry), PII redaction, and an explicit API endpoint allowing users to inspect and purge remembered personal data.
11

Agent State Machines & Task Lifecycles

For mission-critical enterprise workflows, an agent must be governed by an explicit Finite State Machine (FSM). State machines enforce valid state transitions and prevent illegal operational shortcuts.

[ PENDING ] → [ VALIDATING ] → [ EXECUTING ] → [ COMPLETED ] ↓ ↓ ↓ ↑ | [ AWAITING_APPROVAL ] → [ APPROVED ] ----+ | ↓ | [ REJECTED ] ↓ ↓ [ CANCELLED ] [ FAILED ]

Under this FSM:

  • A task in PENDING transitions to VALIDATING once user inputs and RBAC permissions are confirmed.
  • If a tool requires human oversight, the task moves to AWAITING_APPROVAL and halts execution.
  • The backend rejects any direct transition from PENDING directly to COMPLETED, eliminating bypass vulnerabilities.
12

Deterministic Workflows vs. Autonomous Agents

One of the most consequential decisions in AI engineering is choosing between deterministic software pipelines and autonomous agents:

Criteria Deterministic Workflow Autonomous Agent
Sequence Control Code-defined Directed Acyclic Graph (DAG). Model selects next step based on environmental observations.
Testability Deterministic unit & integration testing with 100% path coverage. Trajectory evaluation, statistical acceptance rates, golden test suites.
Runaway Risk Zero. Executes exactly defined steps and exits. Moderate to high without hard iteration and budget circuit breakers.
Ideal Domain Payments, checkout flows, ledger balancing, compliance reporting. Open-ended customer diagnostics, code refactoring, competitive research.
Golden Architecture Rule: Use deterministic code for critical business rules, permissions, and financial calculations; delegate to AI agents only where flexible decision-making and dynamic tool sequencing add demonstrable value.
13

Multi-Agent Systems: Sequential, Parallel & Hierarchical

When tasks encompass disparate domains requiring distinct toolsets and prompt specializations, a single monolithic agent can become overwhelmed. Multi-agent systems decompose tasks across specialized agents:

1. Sequential Pipeline Research → Analysis → Drafting → Review

Each specialized agent consumes the previous agent's output. Simple to audit, but total latency is additive.

2. Parallel Fan-Out Coordinator → [Worker A, Worker B, Worker C] → Aggregator

Independent subtasks run concurrently, reducing wall-clock latency at the cost of multiplied token consumption.

3. Hierarchical Supervisor Supervisor directs workers, inspects results, and reassigns

A centralized coordinator assigns work, aggregates intermediate findings, and delegates until acceptance criteria are met.

When to Avoid Multi-Agent Architecture: If a task can be accomplished reliably with a single agent equipped with 3 well-scoped tools, do not introduce multi-agent complexity. Every additional agent multiplies context overhead, token costs, coordination failures, and debugging difficulty.

14

Agent Safety, Least Privilege & Human-in-the-Loop (HITL)

Because agents can trigger real-world mutations, systems must strictly enforce Least Privilege and Human-in-the-Loop (HITL) governance.

14.1 Role-Based Tool Matrix

User Role Read Tools Standard Writes Sensitive Operations
Customer Own Orders Only Cancel Pending Order Prohibited
Support Rep (Tier 1) All Tenant Orders Update Address, Resend Email Refunds ≤ $50 (Auto), > $50 (Requires Approval)
Store Manager Full Catalog & Analytics Adjust Stock, Reorder Approve Refunds ≤ $1,000
Super Administrator Unrestricted All Operations Bulk Mutations Require 2FA Confirmation

14.2 The HITL Asynchronous Approval Pattern

When an agent encounters an action flagged as sensitive, the controller freezes task state and issues a signed approval request:

[ Agent proposes refund of $1,250 for order ORD-1024 ] ↓ [ Backend policy detects amount > $100 → APPROVAL_REQUIRED ] ↓ [ State transitioned to AWAITING_APPROVAL ] [ Emits Event to Manager Dashboard with cryptographically signed token: ] approval_token = HMAC_SHA256(secret, "task_101:issue_refund:ORD-1024:1250.00") ↓ [ Manager reviews and clicks APPROVE ] ↓ [ Controller verifies signature & executes with Idempotency Key ] ↓ [ Task resumes and reaches COMPLETED ]

Parameter Binding Security: The approval token must bind strictly to the exact arguments proposed. If the refund amount changes from \$1,250 to \$1,251, the HMAC signature fails validation, blocking tampering attacks.

15

Preventing Infinite Loops & Runaway API Costs

Without strict execution boundaries, an agent can easily fall into an infinite loop (e.g. searching with empty results, encountering recurring 400 errors, or flapping between two conflicting tools):

Max Iterations Hard cap on while-loop steps (e.g., maximum 8 to 12 iterations).
Max Tool Calls Cap on cumulative tool executions (e.g., maximum 10 tool calls per task).
Task Wall Timeout Strict wall-clock timeout (e.g., 60 seconds for HTTP, 10 mins for async jobs).
Token & Spend Budget Abort immediately if cumulative cost exceeds dollar threshold (e.g., \$0.50).
Duplicate Call Trap Halt if the agent invokes the identical tool with identical parameters 3 times.
Graceful Exit Return structured explanation rather than dropping connection when limits trip.
16

Idempotency & Structured Error Handling

16.1 Idempotency Keys

If an agent calls issue_refund(order_id="ORD-1024") and the HTTP socket times out after the payment provider debits the account, a naive retry could issue a double refund.

Every write tool invocation must carry a deterministic Idempotency Key:

// Client generates idempotency key before firing network mutation: idempotency_key = f"refund:{order_id}:{task_id}:{step_index}" // In Redis / PostgreSQL: SETNX lock:refund:ORD-1024:tsk_9824:step_3 "PROCESSING" EX 300

16.2 Structured Tool Errors

Tools must return structured error objects allowing the agent to distinguish recoverable faults from fatal rejections:

// 1. Recoverable Service Outage: { "success": false, "error": { "code": "RATE_LIMIT_429", "message": "Courier API rate limited", "retryable": true, "backoff_seconds": 2 } } // 2. Non-Recoverable Validation Rejection: { "success": false, "error": { "code": "ORDER_NOT_FOUND", "message": "Order #ORD-9999 does not exist", "retryable": false } }
17

Agent Observability & Trajectory Evaluation

Evaluating an agent requires inspecting its trajectory (the entire sequence of actions taken), not just its final text output. An agent that answers a question correctly by brute-forcing 15 unauthorized database queries is an operational failure.

Key Telemetry Metric What It Measures Target Healthy SLA
Task Completion Rate Percentage of goals reaching COMPLETED successfully. > 92%
Tool Execution Success Percentage of invoked tools executing without exceptions. > 98%
Average Step Depth Mean number of loop iterations required per successful task. 2.5 – 4.0 steps
Loop Tripping Rate Frequency of tasks halted by maximum iteration or budget limits. < 1.5%
Cost per Successful Task Total model & tool costs divided by completed tasks. < $0.05 per task
18

Production Python Reference: Minimal Reliable Agent Loop

Below is a clean, dependency-free reference implementation of a production-style agent controller featuring explicit state tracking, tool registries, step caps, and structured error propagation:

from dataclasses import dataclass, field
from typing import Callable, Dict, Any, List

# 1. State Definition
@dataclass
class AgentState:
    task_id: str
    goal: str
    max_steps: int = 5
    current_step: int = 0
    status: str = "PENDING"
    history: List[Dict[str, Any]] = field(default_factory=list)
    variables: Dict[str, Any] = field(default_factory=dict)

# 2. Tool Registry & Implementation
TOOLS: Dict[str, Callable] = {}

def register_tool(name: str):
    def decorator(fn: Callable):
        TOOLS[name] = fn
        return fn
    return decorator

@register_tool("get_order")
def get_order(order_id: str) -> Dict[str, Any]:
    db = {"ORD-1024": {"status": "delayed", "eta": "2026-10-09", "carrier": "FedEx"} }
    if order_id not in db:
        return {"success": False, "error": "ORDER_NOT_FOUND"}
    return {"success": True, "order": db[order_id]}

@register_tool("notify_customer")
def notify_customer(order_id: str, message: str) -> Dict[str, Any]:
    # Production write tool: check approval or execute
    return {"success": True, "dispatched": True, "recipient": "customer@example.com"}

# 3. Secure Execution Dispatcher
def execute_tool(name: str, args: Dict[str, Any]) -> Dict[str, Any]:
    if name not in TOOLS:
        return {"success": False, "error": f"UNKNOWN_TOOL: {name}"}
    try:
        return TOOLS[name](**args)
    except Exception as e:
        return {"success": False, "error": f"TOOL_EXCEPTION: {str(e)}"}

# 4. Production Agent Control Loop
def run_agent_loop(goal: str, model_decide: Callable) -> Dict[str, Any]:
    state = AgentState(task_id="tsk_001", goal=goal)
    state.status = "RUNNING"

    while state.current_step < state.max_steps:
        state.current_step += 1
        
        # In production: format prompt with state.history and tools schemas
        decision = model_decide(goal=state.goal, history=state.history)

        if decision["type"] == "FINAL_ANSWER":
            state.status = "COMPLETED"
            return {"status": "COMPLETED", "answer": decision["text"], "steps": state.current_step}

        if decision["type"] == "TOOL_CALL":
            tool_name = decision["name"]
            tool_args = decision["args"]
            
            # Execute with observation
            result = execute_tool(tool_name, tool_args)
            state.history.append({
                "step": state.current_step,
                "thought": decision.get("thought", ""),
                "tool": tool_name,
                "args": tool_args,
                "observation": result
            })
            
            # Check stopping on terminal failure
            if not result.get("success") and not result.get("retryable", True):
                state.status = "FAILED"
                return {"status": "FAILED", "error": result["error"], "steps": state.current_step}

    state.status = "LIMIT_REACHED"
    return {"status": "LIMIT_REACHED", "steps": state.current_step, "error": "Max step ceiling tripped"}
19

Top 10 Production AI Agent Anti-Patterns

Anti-Pattern Why It Fails in Production Production Solution
1. Unrestricted Tool Registries Exposing all system tools to all users invites privilege escalation. Scope tool availability dynamically based on session RBAC tokens.
2. Trusting Model Authorization Assuming an action is safe because the LLM said "I am authorized". Enforce deterministic authorization immediately before tool execution in the backend.
3. Unbounded Agent Loops While-loops without step or token limits can run indefinitely. Enforce hard iteration caps, timeouts, and token spend circuit breakers.
4. Omitting Idempotency Keys Network retries execute duplicate financial charges or emails. Bind every write operation to a deterministic idempotency token.
5. Monolithic "Do-Everything" Tools Tools with vague parameters cause hallucinated schemas and validation failures. Design focused, single-responsibility functions with strict JSON schemas.
6. Over-Complex Multi-Agent Teams Creating 8 agents where 1 agent with 2 tools is sufficient balloons latency and cost. Start with a single well-scoped agent; split only when responsibilities diverge.
7. Memory Without Expiry or Scope Pollutes prompts with stale data and violates data privacy laws. Enforce tenant scoping, TTL retention policies, and user purge endpoints.
8. Assuming Output = Execution Treating a model's generated tool call as proof of real-world completion. Await verifiable return values from backend APIs before declaring task success.
9. Lacking Trajectory Audits Evaluating only final strings masks unsafe or inefficient tool paths. Record OpenTelemetry distributed spans across every step in the agent's path.
10. Synchronous HTTP Timeouts Running 10-step agent tasks within a 15-second browser HTTP request times out. Offload multi-step agents to background task queues (Celery/BullMQ) with WebSocket updates.
20

Production Agent Security & Readiness Checklist

Security & Reliability Standards: 0 / 16 Completed
Access Control & Authorization
Execution Safety & Governance
Reliability & Cost Control
Observability & Auditing
21

Interactive Production Agent Workbench

Explore production agent mechanics in real time across four interactive simulations: ReAct control loops, RBAC tool permissions, Human-in-the-Loop approval state machines, and infinite loop circuit breakers.

Task Objective & Loop Control

Maximum Allowed Steps: 5 Steps
Current State: PENDING
Completed Steps: 0 / 5
Accumulated Tokens: 0 Tokens
Estimated Task Cost: $0.0000
// ReAct Thought → Action → Observation Pipeline Ready
System: Agent initialized. Ready to execute Step 1.
22

Hands-on Assignment: Build an Enterprise Inventory Replenishment Agent

Architect and document a production-grade AI inventory agent that retrieves stock levels, identifies low-stock items, calculates replenishment quantities, and prepares purchase orders with human manager approval.

Assignment Milestones Completed: 0 / 10 Completed
23

Final Architectural Takeaways

  • An Agent Is Not Just an LLM: Agency emerges from the closed feedback loop connecting foundation models with typed tools, durable state, and control loops.
  • The Application Governs the Agent: Never permit the foundation model to serve as the security perimeter. The host application authorizes, bounds, and executes every tool call.
  • Design Granular, Structured Tools: Use strict JSON schemas with regex patterns. Return clean key-value JSON objects rather than prose to conserve context.
  • Enforce Finite State Machines: Critical operations must follow explicit state transitions with Human-in-the-Loop approvals for high-value mutations.
  • Protect Against Runaway Loops: Always configure hard iteration ceilings, token spend circuit breakers, timeouts, and idempotency keys.
  • Evaluate Trajectories, Not Just Final Strings: Measure tool selection precision, step depth efficiency, and policy compliance across every step in the path.

Coming Up: Lesson 15.43 — AI Agent Frameworks & Orchestration: LangGraph, State Machines & Durable Workflows

Now that you master fundamental agent anatomy, tool calling, and safety boundaries, we advance to industry-standard orchestration frameworks. In Lesson 15.43, we explore LangGraph, graph-based execution nodes, conditional branching edges, state checkpointing, durable workflows with Temporal, and human approval interrupts.

Previous: Lesson 15.41 (LLM Production Architecture) Next: Lesson 15.43 (Agent Frameworks & LangGraph)