Saturday, 29 August 2026

Agentic Systems - The Cognitive Architecture (Perception, Planning, Execution & Feedback)

The transition from standard large language models (LLMs) to Agentic AI represents a major shift from static, single-turn responses to dynamic, autonomous problem-solving. Instead of simply generating text, an AI agent operates within a continuous reasoning and execution cycle—empowering it to interact with external tools, self-correct, and complete multi-step workflows.

To successfully build and deploy these systems, engineering teams must move beyond basic prompts and adopt rigid software architectures. This master blueprint structures the transition into four core parts, bridging the gap between theoretical AI concepts and production-grade software.

Every autonomous agent operates on a continuous, four-phase cognitive loop. This loop dictates how the agent understands its environment, formulates a strategy, and learns from its actions.




📚 Series Navigation: Agentic Systems

Part 1: Agentic Systems – The Agentic Triad (You are here)

Part 2: Agentic Systems – The Core System Modules (Stay Tuned)

Part 3: Agentic Systems – Core Agentic Design Patterns (Stay Tuned)

Part 4: Agentic Systems – Enterprise Safeguards and Security (Stay Tuned)

Part 5: Agentic Systems – End to End Working example (Stay Tuned)




1. Perception: Grasping the Context

Perception is the crucial sensory gateway. It dictates what data the system can access and how accurately it interprets the true nature of the problem before attempting to solve it.



Context Ingestion Context ingestion expands far beyond a simple text box, representing the agent's total sensory bandwidth:

  • Multi-Modal Fusion: A capable agent synthesizes distinct data streams simultaneously. It might process a user's frustrated text command ("Fix this bug"), parse an uploaded screenshot of a broken UI, and ingest the raw system error logs all at once. For processing varied inputs simultaneously, agents leverage foundation models capable of multi-modal fusion. This allows the system to ingest a wide spectrum of data.

  • Environmental Awareness: Rather than waiting for a direct prompt, autonomous agents monitor passive triggers. They watch for state changes in a database, incoming webhooks, or scheduled time events, allowing them to intake context dynamically. Autonomous agents are wired into an event bus using message brokers like Apache Kafka, RabbitMQ, or AWS EventBridge. They subscribe to webhooks (e.g., a Salesforce deal stage changing, or a system monitor detecting a 500 error) and react instantaneously as the environment changes.

  • Memory Retrieval: In the context of AI agent architecture, Memory Retrieval is the cognitive mechanism that allows an autonomous agent to access persistent knowledge, historical interactions, and domain data beyond its immediate context window to inform current decision-making. As part of the agent's Perception phase (Context Ingestion), memory retrieval bridges the gap between static model weights and dynamic real-world environments through two primary techniques:

Long-Term Storage (Vector Databases): Knowledge and past interactions are transformed into mathematical embeddings and stored in specialized vector stores (such as Pinecone, Milvus, Qdrant, or pgvector). This functions as the agent's long-term memory bank.

Contextual Fetching (Hybrid Search / RAG): To retrieve the exact context needed for a specific task, Retrieval-Augmented Generation (RAG) combines two complementary search mechanisms: Semantic Search is used to understand broad conceptual meaning, context, and intent. Lexical / Keyword Search (e.g., BM25) matches exact identifiers, code snippets, product names, or error logs.

Signal Processing Signal processing serves as the cognitive filter, translating raw, messy reality into structured blueprints:
  • Noise Reduction: Real-world data is inherently chaotic. Users ramble, API endpoints return 5,000 lines of JSON for one relevant metric, and documents contain formatting junk. The agent must actively strip away this irrelevant data to avoid context window bloat and distraction. Techniques used for Noise reduction are:
      • Prompt Compression: To prevent context window bloat and reduce token costs, developers use prompt compression algorithms (such as LLMLingua). These tools operate at the token level to actively strip out low-signal content, verbose system instructions, and redundant formatting before the context is sent to the LLM's "brain".
      • Metadata Pre-Filtering: Before running an expensive semantic search for memory retrieval, agents apply strict metadata filters (e.g., date ranges, tenant IDs, or document types) directly at the database level. This acts as a massive noise reduction layer, instantly discarding irrelevant data before the ranking stage even begins.
      • Summarization Pipelines: When an external tool or API returns a massive, 5,000-line JSON payload, a smaller, highly efficient "worker" model is often deployed first to extract only the three or four necessary metrics, passing a clean, summarized schema to the primary reasoning agent.

  • Intent Extraction: The agent applies semantic classification to identify the core objective. To convert chaotic human requests into executable blueprints, agents rely on strict schema validation using libraries like Pydantic, Instructor, or native LLM JSON-schema enforcement. This forces the agent to translate a vague intent into a rigid, validated data object. It translates a vague human request like, "Can you maybe check why the web server is acting up and throwing 500s?" into a concrete, executable intent: investigate_server_error(status=500)

  •  Internal State Mapping: Once the noise is gone and the intent is clear, the agent constructs a formal representation of the problem. This often takes the form of a structured schema or state dictionary containing all the variables required to solve the task. This cleanly formatted blueprint is exactly what gets passed downstream to the Planning module. The agent's "Internal State" is orchestrated by frameworks like LangGraph, AutoGen, or CrewAI. These tools structure the agent's workflow as a cyclical graph, continually updating a persistent state dictionary at every step. This ensures the agent always grounds its next tool call or action in a verified, up-to-date map of the problem.
      Summary of Key Tools & Technology Used in Perception



Sunday, 23 August 2026

Beyond Prompt Hacks (Part 3): Enterprise Architecture & Production Pipelines

Moving from a playground prompt to a resilient production system requires a decoupled, enterprise-grade runtime architecture. This post covers the end-to-end stack:

  • Model-Specific Runtime Behaviors & Multi-Model Routing

  • Multi-Tier Caching & Performance Optimization

  • Agentic Workflows & Secure Tool Calling

  • Prompt-as-Code, Versioning & CI/CD

  • Dual-Stage Guardrails & Data Privacy

  • Streaming, Recovery Loops & Output Schema Enforcement

  • Observability, Evals & AI FinOps


📚 Series Navigation: Building Enterprise-Grade Prompt Engineering

📌 New here? If you landed directly on this post, we highly recommend starting with Part 1 to grasp the foundational mechanics of tokenization, attention decay, and sampling parameters before diving into architectural patterns.




1. Model-Specific Runtime Behaviors & Multi-Model Routing


Before building the pipeline, it is critical to handle temperature limits, soft-capping, and Chain-of-Thought (CoT) loops across different providers like DeepSeek, OpenAI, and Gemini.

  • DeepSeek: Standard models like DeepSeek-V3 accept temperatures in the range [0.0, 2.0]. Lower bounds (0.0) crush the token probability mass into a single greedy choice, essential for deterministic output like code or SQL. Upper bounds (>1.3) flatten the probability distribution for creative text or translation.

  • OpenAI & Gemini: OpenAI models (e.g., GPT-4o) enforce a [0.0, 2.0] temperature scale. Google's Gemini models also operate on a [0.0, 2.0] scale (with [0.0, 1.0] historically used as default across Google AI Studio).

  • Implementation: In production API wrappers (e.g., LiteLLM, LangChain), temperature limits are explicitly set during API calls to restrict model drift.

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": prompt}],
    temperature=0.2, # Hard limit for deterministic outputs 
    top_p=0.95 
)

By standardizing these parameters, you can implement intent-based semantic routing to optimize cost vs. intelligence (e.g., routing to small models for classification and frontier models for complex reasoning).


2. Detailed Architectural Considerations


2.1 Multi-Tier Caching Strategy (KV-Cache + Semantic Caching)


Design Principle: Prevent redundant model inference at both the token-processing level and execution level to reduce latency and API costs.

To maximize performance, decouple the caching layer into two distinct operational phases:

Level 1: Upstream Vector Semantic Caching (Execution Bypass)

Positioned directly at the API gateway or orchestration layer, semantic caching completely bypasses LLM inference for semantically equivalent queries.

  • Vector Search Engine: High-performance vector stores like RedisVL, Qdrant, or Milvus are deployed upstream of the orchestrator.

  • Embedding Model: A lightweight, highly optimized embedding model (e.g., text-embedding-3-small or bge-small-en-v1.5) converts incoming user queries into high-dimensional vectors in <10ms.

  • Similarity Thresholding: The vector store queries past request-response pairs using cosine similarity. If the threshold is >= 0.95, it is a high-confidence semantic match, and the system immediately returns the cached output payload in <50ms. If < 0.95, it forwards downstream.

  • TTL & Invalidation Strategy: Set time-to-live (TTL) limits based on data volatility (short TTL for real-time data, longer for static knowledge). Implement LRU eviction to prevent stale responses.

Level 2: Engine-Level Prefix KV-Cache Optimization (Token Processing Minimization)

When a query misses the semantic cache, optimization shifts to reducing compute latency during the prefill phase via KV-cache (Key-Value) reuse in inference engines like vLLM (PagedAttention) or provider APIs.

  • Strict Prompt Payload Structuring: Attention mechanisms require prefix matches to share exact token prefixes from index 0.

    • Top Layer (Static): System instructions, persona definitions, tool schemas, and safety guidelines.

    • Middle Layer (Semi-Static): Few-shot exemplars and broad context documents (RAG context blocks).

    • Bottom Layer (Dynamic): User query, real-time context, conversation history, and ephemeral variables.

  • Radix Tree / Paged Attention Mapping: Inference servers store previously computed key-value pairs of the prefix in GPU memory using Radix trees. Reusing prefixes skips heavy matrix multiplications, reducing Time-To-First-Token (TTFT) by up to 80–90% and slashing input token costs.


2.2 Intent-Based Routing & Active Fallbacks


Design Principle: Allocate compute dynamically based on task complexity rather than sending every query to a high-cost frontier model.

1. Gateway Intent Classification (Compute Allocation)

Positioned at the API Gateway (e.g., LiteLLM, Kong), a lightweight classification stage determines the optimal model tier in <20ms before dispatching the request.

  • Routing Classifiers:

    • Heuristic & Keyword Rules: Fast regular expressions trap deterministic tasks (e.g., JSON extractions).

    • SLM Classifiers: Sub-billion parameter models classify queries into complexity tiers (Low, Medium, High).

    • Embedding Distance: Cosine distance against pre-clustered domain centroids routes queries based on semantic complexity.

  • Model Tier Mapping:

    • Tier 1 (Commodity / Fast): Simple summarization, extraction, classification using Llama 3.1 8B, Mistral 7B, GPT-4o-mini.

    • Tier 2 (Reasoning / Frontier): Multi-step code generation, logical reasoning, multi-document synthesis using Claude 3.5 Sonnet, Gemini 1.5 Pro, DeepSeek-R1.

2. Active Fallbacks & Multi-Cloud Redundancy (High Availability)

  • Circuit Breakers & Retries: Implement exponential backoff with jitter for intermittent 5xx errors. Trigger a circuit breaker if failure rates cross a 5% threshold within a 60-second window.

  • Provider Cross-Routing: Fallback policies reroute traffic seamlessly across alternate cloud providers without throwing client-facing errors (e.g., Primary: OpenAI GPT-4o -> Fallback 1: Azure OpenAI GPT-4o -> Fallback 2: Claude 3.5 Sonnet -> Fallback 3: Self-hosted vLLM).

  • Graceful Degradation: If primary frontier models hit system-wide rate limits, the router gracefully degrades the query to a faster local SLM or returns a structured error message.

2.3 Agentic Workflows & Sandboxed Tool Execution


Design Principle: Treat tool-calling LLMs as untrusted actors operating under a strict zero-trust security model with least-privilege access.

Implementing a zero-trust model ensures non-deterministic LLMs cannot execute unauthorized commands, exfiltrate data, or corrupt enterprise systems.

1. Ephemeral Sandbox Execution Enclaves

To neutralize Remote Code Execution (RCE) and Server-Side Request Forgery (SSRF), execution environments must be completely isolated and disposable.

  • MicroVMs (Firecracker / Kata Containers): Boot dedicated, minimal kernel virtual machines to provide hard hardware virtualization boundaries.

  • Hardened Containers (gVisor / Docker with Seccomp): Intercept system calls at the user-space boundary to prevent kernel exploitation during untrusted python/bash execution.

  • Egress Control & Network Perimeter: Restrict network interfaces using strict iptables and proxy sidecars. The sandbox is blocked from internal VPC endpoints, cloud metadata services (e.g., AWS 169.254.169.254), and non-whitelisted external APIs.

  • Database Access Controls: Application connections operate under dedicated read-only database roles (e.g., SELECT-only permissions). Writes execute strictly against ephemeral staging schemas or transactional dry-run tables.

2. Gatekeeper Control: Schema Validation & Human-in-the-Loop (HITL)

  • Strict Contract Verification: Intercept raw LLM JSON tool payloads and validate them against strict Pydantic models or JSON Schema definitions before runtime. Instantly reject malformed arguments or injected SQL/shell syntax.

  • Idempotency & Risk Matrix:

    • Low-Risk (Automated Execution): Read-only queries, vector retrievals, and data transformations execute automatically.

    • High-Risk (HITL Gate Required): Database mutations, API post requests, external email dispatches, and financial actions require explicitly holding agent state.

  • Asynchronous HITL Approval Loop: High-risk payloads route to an execution broker (e.g., Redis Queue / Kafka), generating a structured approval token. Human operators confirm or reject the payload via interactive webhooks (e.g., Slack action cards).

2.4 Dual-Stage Guardrails & Privacy Pipelines


Design Principle: Enforce safety, compliance, and privacy controls outside the core LLM pipeline at both ingress and egress boundaries.

1. Ingress Stage: Anonymization & Prompt Injection Defense

  • Deterministic PII Redaction & Vaulting: Lightweight NER engines (e.g., Microsoft Presidio, SpaCy) identify PII. Discovered entities are replaced with deterministic placeholder tokens (e.g., John Doe -> <PERSON_1>). The mapping table is stored in an isolated, short-lived cache (e.g., Redis) scoped strictly to the request thread.

  • Prompt Injection & Adversarial Filtering: Fast classification safety models (e.g., Llama Guard, Guardrails AI) scan the raw input payload to detect jailbreaks and indirect Prompt Injection embedded within retrieved RAG documents.

2. Egress Stage: Content Safety & PII Re-hydration

  • Output Safety Inspection: Scans generated outputs for toxic content, brand violations, or hallucinated sensitive system data.

  • PII Re-hydration / Un-masking: The Egress gateway checks the completed payload against the in-memory token map. Placeholder tokens (e.g., <PERSON_1>) are dynamically swapped back with their original values (John Doe), ensuring a seamless, context-accurate payload for the user without ever exposing sensitive values to third-party providers.

2.5 Streaming Architectures & Graceful Schema Recovery


Design Principle: Preserve low-latency user experience through streaming without sacrificing structural validity for downstream application logic.

1. Progressive UI Rendering via Partial Parsing

  • Transport Protocols: Server-Sent Events (SSE) push output chunks to the client as tokens are emitted by the inference server. SSE is generally preferred over WebSockets for unidirectional model streaming over standard HTTP/2.

  • Partial JSON Parsers: Traditional JSON.parse() calls fail on unclosed braces. Client UIs employ stateful streaming AST parsers to complete malformed JSON blocks on the fly, enabling UIs to incrementally render structured components—such as tables or code blocks—without waiting for the full response payload.

2. Egress Gate Schema Validation

  • Stream Buffering: An Egress Guardrail proxy intercepts and buffers the full aggregated response in memory as the stream completes.

  • Schema Verification: The completed payload is verified against a strict schema contract (such as a Pydantic model) using fast, compiled validators.

3. Automated Single-Turn Repair Loop

When a model output suffers from structural truncation or incorrect property types, the Egress gate intercepts the bad payload:

  1. Error Isolation: The system captures the explicit schema violation (e.g., ValidationError: missing required field 'user_id').

  2. Contextual Resubmission: An automated repair request is immediately sent back to the model containing the original schema definition, the malformed string, and the exact validation error string.

  3. Low-Temperature Execution: The repair call is executed at temperature=0.0 (or routed to a smaller, faster model tuned for code repair) to enforce strict determinism.

  4. Fail-Safe Fallbacks: If the single repair turn fails, the request triggers an explicit fallback handler or routes the payload to a dead-letter queue (DLQ).

2.6 Observability, Tracing & Multi-Tenant FinOps


Design Principle: Achieve complete operational transparency into non-deterministic execution paths, cost allocation, and prompt version lineage.

1. Unified Tracing & Telemetry (Observability)

  • OpenTelemetry (OTel) Integration: Standardize all microservices and orchestrators to emit OpenTelemetry spans. Inject unified tracing headers (prompt_id, prompt_version_hash, tenant_id, temperature, input_tokens, output_tokens, latency_ms) as custom span attributes.

  • Prompt Lineage & Registry: Tie the prompt_version_hash directly to a Git-backed prompt registry (e.g., PromptLayer, LangSmith) to see exactly which prompt version and temperature settings caused anomalies.

  • Payload Capture for Evals: Asynchronously log the actual input payload and output response to a data lake for offline "LLM-as-a-judge" regression testing.

  • Trace Context Propagation: Ensure trace IDs are passed through asynchronous boundaries. If an workflow triggers three separate tool calls, all must be parented under the single original user trace.

2. Gateway Enforcement & Rate Limiting (FinOps Controls)

  • Distributed Token-Bucket Algorithms: Implement a Redis-backed token bucket mechanism at the gateway tracking "Tokens Per Minute" (TPM) instead of RPM, assigning each tenant_id a specific refill rate.

  • Concurrency Limits: Limit the number of concurrent connections per tenant to prevent noisy neighbor problems.

  • Circuit Breakers for Agentic Loops: Autonomous agents can get stuck in self-correction loops. Configure circuit breakers at the gateway that automatically break connections if a single session attempts more than a predefined number of sequential calls.

3. Dynamic Expenditure Caps & Cost Allocation

  • Real-Time Token-to-Cost Translation: The API Gateway must translate input and output tokens into dollar amounts immediately upon request completion, using an up-to-date pricing matrix for all models in the routing pool.

  • Multi-Tier Expenditure Caps: Implement Soft Limits (triggering Slack/PagerDuty alerts at 80% budget) and Hard Limits (rejecting requests with an HTTP 429 status once the cap is hit).

  • Chargeback Tagging: Require metadata tags (e.g., environment: prod, team: marketing) on every request to generate detailed chargeback reports.

4. Anomaly Detection & Continuous Optimization

  • Cost Spikes & Rogue Microservice Alerts: Set up anomaly detection on the telemetry stream. If a microservice typically consumes 5,000 TPM and suddenly spikes to 50,000 TPM, the system should automatically throttle the service and alert engineering.

  • Cache Hit-Rate Monitoring: Track the effectiveness of your prefix and semantic caching layers. If a high-volume prompt has a low cache hit rate, engineers can investigate whether the semantic similarity threshold is too strict.