AI Agent Architecture: From Message Routing to Closed-Loop Learning

    This article compares two AI agent architecture patterns: centralized message routing and closed-loop learning. It explains why conversational ability alone does not make an agent intelligent, breaks down the four subsystems of a closed-loop design (prompt builder, tool registry, session store, and skill consolidation), and offers four dimensions for evaluation. It also covers migration steps, credential handling, consistency checks, and selection guidance for teams choosing between the two approaches.

    1. The Problem: Why “Being Conversational” Is Not the Same as Being Intelligent

    New agent tools appear almost every few weeks, each branded as the “next-generation assistant” and claiming seamless migration from existing solutions. For developers, the real criterion is not the marketing claim but whether the architecture resolves the core contradiction of agents: context is disposable, while experience should be cumulative.

    Many early agent implementations were essentially a message routing layer: receive messages from various instant messaging channels, parse intent, call a large language model, execute a few tools, and return the result along the same path. This chain is clear and easy to debug, but it has a structural flaw: every turn runs in an isolated context, tools are discarded once they finish executing, and nothing is retained. Users must repeat the same background, re-authorize the same actions, and correct the same mistakes every time.

    This article abstracts two architectural paradigms, breaks down their differences layer by layer, and provides a practical evaluation method.

    2. Paradigm One: The Centralized Message Routing Architecture

    The typical chain looks like this:

    1. Message ingress → Message parsing → Model call → Tool execution → Response routing → Message egress

    2.1 Advantages

    • Clear boundaries: each stage has a single responsibility, which makes unit testing and fault isolation straightforward.
    • Low integration cost: supporting a new channel only requires extending the ingress adapter layer.
    • Predictable latency: the chain is short and carries no extra memory retrieval overhead.

    2.2 Inherent Limitations

    1. Stateless: everything is forgotten when the session ends, including user profiles, preferences, and prior conclusions.
    2. Tools coupled to prompts: prompts are often hard-coded in the routing logic, so any change requires a code change.
    3. No self-improvement: error patterns are never recognized, so the same class of problem keeps recurring.
    4. Hard to scale horizontally: as channels multiply, the routing layer quickly grows into an unmaintainable central node.

    3. Paradigm Two: The Closed-Loop Learning Architecture

    The closed-loop architecture splits the agent into four subsystems that can evolve independently, forming a loop of perceive, decide, act, consolidate, and perceive again:

    1. ┌──────────────────────────────────────────────┐
    2. │ Agent Core Loop │
    3. │ (Main Controller) │
    4. └──────────────────────────────────────────────┘
    5. │ │ │
    6. ▼ ▼ ▼
    7. Prompt Builder Tool Registry Session Store
    8. (Dynamic assembly) (Capability list) (Persistence + full-text search)
    9. │ │
    10. ▼ ▼
    11. Context Window Grouped tool sets

    3.1 Prompt Builder: From Static Templates to Dynamic Assembly

    The prompt is no longer a fixed block of text but is assembled in real time for the current task: base role definition + relevant long-term memory fragments + applicable skill descriptions + a summary of recent sessions. The key is retrieval-based injection, meaning only memory relevant to the current task enters the context, so the window is not crowded out by unrelated information. In engineering practice this is usually paired with layered summarization: recent turns keep the original text, mid-term turns get a rolling summary, and long-term memory retains only structured facts and preference entries.

    3.2 Tool Registry: Capabilities Become Discoverable Instead of Hard-Coded

    Tools are registered as metadata (name, purpose description, input schema, permission scope) and selected by the model on demand at runtime. Grouping them by business domain into several tool sets and loading them lazily can significantly reduce prompt size and the rate of wrong tool calls. A schematic definition:

    1. @dataclass
    2. class ToolSpec:
    3. name: str
    4. description: str # Helps the model decide when to call
    5. input_schema: dict # Parameter constraints, used for validation
    6. group: str # Tool set it belongs to, supports lazy loading
    7. permission: str # Read/write permission, used with auditing

    3.3 Session Store: A Retrievable Memory Foundation

    A relational store holds structured data, and full-text search capability is added to index historical fragments so the agent can recall rather than restate. Retrieval quality determines how well the loop works; in practice it is advisable to keep both a time dimension and a source dimension, so results can be weighted by time decay and by credibility.

    3.4 Consolidation Loop: Turning One Success into a Reusable Skill

    When a class of task is completed successfully, its flow can be abstracted into a skill record (trigger condition + steps + checkpoints) and reused directly the next time a similar task appears. This step is the dividing line between a closed loop and routing: the former produces assets, the latter only produces responses.

    4. Four Key Evaluation Dimensions

    Dimension Routing architecture Closed-loop architecture
    Memory None, discarded with the session Layered memory + full-text search
    Tools Hard-coded calls Registry + lazy-loaded tool sets
    Prompts Static templates Dynamically assembled via retrieval
    Evolution Relies on manual code changes Relies on skill consolidation and feedback

    When evaluating, three questions are worth asking: where is state stored? how are capabilities discovered? how is the same error prevented from happening twice?

    5. Migration and Adoption: Keeping the Cost Low

    If agent assets already exist, migration should focus on four kinds of objects: configuration items, memory data, skill definitions, and credentials. Credentials must go through a key management service or be injected via environment variables, and must never be written into configuration files or code repositories. After migration, run a consistency check: sample and compare historical question-answer pairs, confirm tools are discoverable, and verify retrieval hit rates. For production, it is advisable to start with a canary scope and observe the call chain and failure rate with logging and monitoring in place.

    On Baidu AI Cloud, the core loop can run on a container platform, skill and configuration snapshots can be stored in object storage, a message queue can decouple channel integration, and a logging and monitoring system can provide full-chain observability, forming an operational and rollback-capable engineering loop.

    6. Selection Guidance

    • If the requirement is only multi-channel notifications and simple question answering, the routing architecture is enough; do not over-engineer.
    • If the requirement involves long-term companionship, continuous error correction, and process reuse, prefer the closed-loop architecture.
    • If data security is a concern, confirm where memory is stored, how it is encrypted, and how deletion works.
    • If cost is a concern, focus on the retrieval strategy and context length control rather than unit price.

    The conclusion is not about which project is more popular, but whether the architecture can consolidate experience. A system that accumulates experience becomes more valuable over time; a system that only forwards messages has almost zero replacement cost.