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

    This article compares two goals that teams usually confuse when they connect an LLM to their stack: message routing, which focuses on getting a conversation to the model and the answer back, and closed-loop learning, which accumulates runtime experience as reusable skills, session records, and long-term memory. It explains why stateless routing pipelines hit a ceiling, which three capabilities a closed-loop system needs (prompt assembly, tool registration, and session storage), how to govern memory, skills, and credentials separately, and what boundaries to hold on least privilege, observability, regression evaluation, cost, and deletion. Readers will also get a short checklist for deciding whether they actually need a second assistant layer at all.

    When individuals and teams first connect a large language model, they usually build the most obvious pipeline: a message arrives from a chat or business channel, intent is parsed, the model is called, tools are executed if needed, and the result is sent back to the original channel. This path is clear, ships fast, and is enough to support the “Q&A bot” stage. What really makes a system expensive and hard to maintain is not adding another channel. It is users starting to expect the assistant to remember preferences, reuse work it has already done, stay consistent across multiple tasks, and move skills, credentials, and sessions from one environment to another.

    Separate the Two Goals First

    The first goal is message routing. The core value of the system is “send the conversation to the model, then send the answer back.” Each turn should be as self-contained as possible, tool execution ends when it ends, and almost no retrievable state is left behind. It fits customer-service triage, one-off queries, strict compliance auditing (store as little user data as possible), and the trial period when model vendors are still being swapped frequently.

    The second goal is closed-loop learning, where “learning” should not be understood as online weight training but as runtime accumulation of experience: successful operation steps are written down as reusable skills, failure reasons are recorded in the session, user preferences go into long-term memory, and later turns retrieve this material back into the prompt. The model itself can stay unchanged while system behavior becomes more stable with use.

    These are not mutually exclusive products but two stages on the same evolution path. The routing layer solves “can we connect”; the closed-loop layer solves “can we keep using it.” If the business is still at single-turn Q&A, forcing in memory and a skill library only adds credential, privacy, and consistency costs.

    Why Routing Architectures Hit a Ceiling

    A typical chain looks like this:

    Message ingress → normalization and authentication → assemble the current turn’s prompt → model call → tool execution → response routing → message egress

    Its advantage is clear boundaries and simple horizontal scaling: put a message queue behind stateless worker nodes. The limitation comes from that same “stateless” property:

    • Context is temporary. If multi-turn conversation relies only on the short history on the channel side, the assistant forgets confirmed constraints after a device switch, a reconnect, or context truncation.
    • Tool results are not turned into assets. A successful retrieval, approval, or script execution still has to be described from scratch next time. Teams cannot distill “how to do it right” into standard steps.
    • Observability and replay are hard. Without structured session storage, you can only dig through text in logs, and it is difficult to recover “why we chose this tool last time” by topic.
    • Migration means redoing. Credentials, channel bindings, prompt templates, and skill descriptions are scattered across environment variables and documents, so changing runtime environments requires manual verification.

    The routing model is therefore a good entry point for the control plane, but not suited to carrying the full responsibility of a long-term personal or team assistant on its own.

    Three Capabilities a Closed-Loop System Must Add

    Beyond the conversation loop, you need at least three peer modules rather than piling all logic into one very long prompt.

    Prompt construction. Each turn is not a simple concatenation of the last few sentences but an assembly under a budget: system constraints, the current task, retrieved memory fragments, available tool descriptions, and the user input for this turn. The budget must be explicit: guarantee safety and task instructions first, then high-relevance memory, and only then casual history. Material with low relevance is better left out to avoid leading the model astray.

    Tool registration. Tools should be managed as sets, not stacked endlessly into a single call. Each tool needs a name, purpose, parameter schema, permission scope, timeout, and idempotency notes. The registry should also record “which tool combinations have been validated.” Validate arguments before the call, and write the structured result back into the session afterwards instead of stuffing a paragraph of natural language into history.

    Session storage. Use queryable storage to keep messages, tool traces, skill versions, and memory the user has explicitly confirmed. Full-text search is good for recovering old tasks by keyword; adding vector retrieval enables semantic recall. There must be a clear contract between storage and prompt construction: what is written are facts and operation records, what is read out is trimmed evidence, not the entire database dumped into the prompt.

    A sketch of the loop (structural illustration only, not the interface of any specific implementation):

    1. while task not finished and step limit not exceeded:
    2. context = build_prompt(system_constraints, retrieve(memory_and_skills), current_input)
    3. action = model(context, currently_allowed_tool_subset)
    4. if action is a tool call:
    5. result = execute(action) within permissions and timeout
    6. session_storage.append(action, result)
    7. else:
    8. respond to the user
    9. decide by policy whether to write to long-term memory

    Step limits, timeouts, and the tool allowlist must be enforced outside the loop, not written only in the prompt as “please do not perform dangerous operations.”

    Govern Memory, Skills, and Credentials Separately

    Many people implement “being able to remember” as one large text file, which then cannot be deleted or audited. A safer approach is three separate stores:

    • Session traces: expire by default, used for replay and debugging, with a retention period set by compliance requirements.
    • Long-term memory: store only preferences and facts the user has explicitly approved or the system can verify, and support deletion per item.
    • Skills: write repeated tasks as versioned step descriptions or small scripts, with changes that can be rolled back. Skill descriptions go into the prompt, execution still goes through tool permissions, and the model does not freely generate and directly run arbitrary commands.

    Credentials never enter the memory store or the body of a skill. At runtime they are provided through a secret manager or environment injection, and redacted in logs and retrieval indexes. What you migrate should be a configuration manifest, skill versions, and memory exports authorized by the user, not credentials in plaintext packed into the same bundle. A complete migration must be previewable and rollback-capable: first run read-only validation in the new environment (are all tools present, is the model endpoint reachable, are channel credentials valid), then switch traffic over.

    Runtime differences across environments also matter. Some assistant runtimes depend on a Unix-like environment; on desktop systems you often need a compatibility layer or containers to provide a consistent filesystem and process model. This is not a matter of “installing one more piece of software” but of whether skill scripts, paths, and the permission model are portable.

    Does an Ordinary User Need Another Assistant Layer

    The criterion is not hype but whether the existing routing bot is already blocking work:

    1. Are you frequently repeating the same class of multi-step task, with steps already stable enough to write down as a skill?
    2. Does the same work continue across days and across channels, with the cost of losing context higher than the cost of storage and retrieval?
    3. Is there multi-person collaboration that needs shared, reviewed skills rather than everyone pasting their own prompt?
    4. Do you need to migrate configuration, memory, and skills as assets instead of being bound to one conversation window?

    If most answers are no, improving the prompts, tool permissions, and logs of the existing chain yields more. If most answers are yes, it is worth adding session storage, retrieval, and skill versioning alongside routing, rather than maintaining two parallel conversation entry points that share no state — two entry points only split memory.

    Boundaries Worth Holding in Engineering

    • Least privilege by default. Grant tools temporarily per task, state file and network scope explicitly, and require human confirmation for high-risk operations.
    • Observability. Record which tool the model chose, a parameter summary, elapsed time, and failure type, so you can tell whether retrieval missed, permissions were insufficient, or the model planned wrongly.
    • Regression evaluation. Use a small fixed set of tasks (including multi-turn constraints, tool failures, and users changing their mind) for regression so a skill update does not silently make things worse.
    • Controlled cost. Set caps on the number of retrieved items, the number of history turns, and tool description length; a closed loop does not re-feed the entire history every turn.
    • Privacy and deletion. Classify before writing to memory, support export and deletion, and keep sensitive fields in channel messages out of the full-text index as-is.

    If you deploy in the cloud, ingress traffic can be smoothed with a message queue, sessions and skill packages can go to object storage with versioning, and runtime metrics can feed monitoring and alerting. General capabilities like these on cloud platforms such as Baidu AI Cloud are suited to the infrastructure role of “store, move, and observe”; whether an agent is closed-loop still depends on whether you design prompt construction, tool registration, and session retrieval as one data path, not on installing one more entry point.

    Summary

    Message routing answers the connectivity question: where messages come from and where answers go. Closed-loop learning answers the asset question: which facts, steps, and failures are worth keeping, and how they return to the prompt in a controlled way on the next turn. For most people the right order is to make routing, permissions, and logging solid first, then introduce searchable memory and versioned skills once repetitive tasks appear. What you add is not another chat window but a working memory that can be migrated, audited, and deleted.