AI Agent Architecture Evolution: From Message Routing to Closed-Loop Learning
This article compares two common AI agent architecture patterns: centralized message routing and closed-loop learning. It explains how each handles conversation context, tool execution, and persistence, then walks through four key components of the closed-loop design—session storage with full-text retrieval, a layered tool registry, a prompt builder, and portable skills and configuration. It closes with practical selection guidance and a gradual implementation path for developers building agents with memory and retrieval on a general-purpose tech stack.
1. Why It Is Worth Breaking Down the “Next-Generation AI Assistant”
Every so often, a new project appears in the community carrying the label of “next-generation personal AI assistant.” The pitch is almost always the same: install it with a single command, migrate the configuration in one click, extend its capabilities at any time. Most of these projects fade once the hype dies down, but a few genuinely push a new architectural paradigm into the mainstream. For developers, it is more productive to focus on the architectural choices behind a tool than to chase any specific product: is an agent system a stateless message relay, or a closed loop that accumulates experience over time?
This article therefore sets aside product-specific features and discusses only the trade-offs between two typical architectures, along with how to build an agent with memory and retrieval capabilities on a general-purpose technology stack.
2. The Core Difference Between the Two Architectures
2.1 Centralized Message Routing
The first architecture revolves around a message routing layer. It connects to entry points such as instant messaging, team collaboration tools, or a self-built frontend, normalizes incoming external messages, passes them to the large language model, and then delivers the model output back to the original channel based on a session identifier. The main flow can be summarized as:
Message intake → Parsing and normalization → Model call → Tool execution → Response routing → Message output
The advantages are clear boundaries, easy deployment, and straightforward horizontal scaling. The drawbacks are equally obvious: conversation context and tool call results are discarded as soon as the response is returned, and the system retains no reusable information. Every time a user starts a new topic, the agent begins from scratch.
2.2 Closed-Loop Learning
The second architecture treats the “conversation loop” as the backbone and mounts three groups of persistable components around it: a prompt builder, a tool registry, and session storage. A schematic structure looks like this:
┌──────────────────────────┐│ Agent Main Loop (Core) │└──────────────────────────┘│ │┌──────────┘ └──────────┐▼ ▼ ▼Prompt Builder Tool Registry Session StorageContext assembly Capability History and full-textdiscovery retrievaland routing
The difference is not the number of tools, but whether there is a channel for “execution results to flow back”: tool outputs are written to storage, and later conversations can retrieve them through search, forming a closed loop.
3. Four Key Components of the Closed-Loop Architecture
3.1 Session Storage and Full-Text Retrieval
It is recommended to use an embedded relational database to hold sessions and messages, and to enable its built-in full-text indexing capability to create an inverted index over historical messages. During retrieval, perform keyword recall first, then splice the matched snippets into the prompt, rather than blindly stuffing the entire history into the context.
def recall(session_id: str, query: str, top_k: int = 5) -> list[str]:rows = conn.execute("SELECT snippet FROM messages ""WHERE session_id = ? AND messages MATCH ? ""ORDER BY rank LIMIT ?",(session_id, query, top_k),).fetchall()return [r[0] for r in rows]
Three engineering concerns matter here: index updates within the same transaction as writes, snippet length truncation, and deduplication of recall results.
3.2 Tool Registry and Toolset Layering
Change tools from “hard-coded branches” to “registration plus metadata description,” so the main loop only looks up and dispatches based on the invocation intent provided by the model. As the number of tools grows, split them into several toolsets by domain and load them per scenario to control prompt size and the rate of incorrect calls.
REGISTRY: dict[str, Callable] = {}def tool(name: str, group: str = "default"):def deco(fn):REGISTRY[name] = {"fn": fn, "group": group}return fnreturn deco@tool("read_file", group="filesystem")def read_file(path: str) -> str:...
3.3 Prompt Builder
A prompt should not be a static string. It should be assembled in layers: system constraints + long-term memory + retrieved snippets + the current task, with a budget cap set for each layer to prevent context bloat from driving costs out of control.
3.4 Portability of Skills and Configuration
“One-click migration” in a general-purpose implementation is equivalent to serialized export and import of configuration and state: secret-related configuration goes through environment variables or a key management service, memory and skills are exported as structured files, and imports perform version validation and field mapping to avoid cross-version contamination.
4. Selection Guidance and Implementation Path
- If you only need multi-channel Q&A with no accumulation: the centralized routing architecture is sufficient, with the lowest deployment and operations cost.
- If you need long-term companionship and continuous project context: prioritize the closed-loop architecture, landing storage and retrieval first, then expanding the toolset.
- Gradual path: keep the routing layer → add session storage → connect retrieval → move to a tool registry → layer the toolsets.
- Observability: record the duration, a summary of input parameters, and the failure reason for each tool call, and combine log services with monitoring and alerting to locate issues.
- Cloud hosting: stateless entry points can scale elastically on a container platform, state and retrieval data can go into a managed database or object storage, and secrets can be hosted in a dedicated key management capability.
5. Conclusion
Hype fades; architecture remains. To judge whether an agent project is worth investing in, look at whether it has these three characteristics: execution results can flow back, historical context is retrievable, and capabilities can be registered and extended. For ordinary users who already have a working assistant, the benefit of migrating comes mainly from memory continuity. For developers building their own, it is recommended to start with storage and retrieval, then gradually add tooling—this is also the smoothest path for incremental evolution on general-purpose cloud infrastructure such as Baidu AI Cloud.
