Why Agent Architecture Is Not Request-Response: A Deep Dive

    This article explains why AI agents cannot be modeled as simple request-response calls and breaks down their actual runtime structure. It covers the perceive-plan-act-observe loop, context engineering, tool invocation safeguards, layered memory, reliability and observability, and common anti-patterns, giving developers a practical foundation for building stable, observable, and scalable agent systems.

    1. First, Clear Up a Common Misconception: An Agent Is Not “One Question, One Answer”

    When developers first work with AI agents, they naturally fall back on the most familiar model: a user sends a message, the program stitches it into a prompt, calls the LLM once, and returns the resulting text. That model is not wrong, but it describes a single inference call, not an agent.

    An agent in real operation is closer to a stateful execution system:

    • The input is not an isolated string but a continuously evolving context;
    • The output is not a final piece of text but a series of action decisions;
    • The intermediate process may involve multiple model calls, external capability calls, state reads and writes, and failure retries;
    • The termination condition is not “the model finished talking” but “the goal is achieved” or “a termination policy is triggered.”

    Treating an agent as request-response leads to direct engineering pitfalls: intermediate behavior cannot be explained, failures cannot be recovered from, and cost and latency cannot be controlled. The following sections break down the real runtime structure layer by layer.

    2. The Runtime Backbone: The Perceive-Plan-Act-Observe Loop

    The most core structure of an agent is a loop, not a straight line. It can be abstracted as:

    1. while not done and step < max_steps:
    2. decision = model.decide(context) # Planning: what to do next
    3. result = executor.run(decision) # Action: call a tool or internal capability
    4. context = context.append(decision, result) # Observation: write back to state
    5. done = evaluator.is_goal_met(context) # Evaluation: has it converged?

    The four stages each carry different responsibilities:

    1. Perception and context assembly: Assemble system instructions, conversation history, retrieved material, the tool list, and the current environment state into an input the model can read.
    2. Planning and decision-making: The model outputs a structured decision, such as “call a certain capability,” “perform an additional retrieval,” “answer directly,” or “ask the user for clarification.”
    3. Action and execution: The execution layer parses the decision, performs parameter validation and permission checks, invokes the real capability, and normalizes the result.
    4. Observation and convergence evaluation: Write the execution result back into the context and determine whether the goal is met, whether the strategy needs to change, and whether the step or budget limit has been reached.

    The key design principle of this loop is: the model handles judgment, and the execution layer handles determinism. Anything that can be guaranteed correct by code (parameter format, permissions, retry counts, idempotency keys) should not be left to the model’s free play.

    3. Context Engineering: The Real Bottleneck That Determines an Agent’s Ceiling

    In practice, when an agent performs poorly, it is usually not because “the model is not smart enough” but because context organization has failed. Four things deserve attention:

    • Layered organization: Separate the instruction zone, long-term memory zone, retrieved material zone, recent interaction zone, and tool result zone to avoid cross-contamination. The instruction zone should be stable and have the highest priority.
    • Compression and summarization: Unlimited history growth consumes budget and dilutes attention. A common approach is to apply rolling summarization to earlier interactions, keeping decisions and conclusions while discarding redundant wording.
    • On-demand retrieval: Do not stuff in all material at once; instead, let the agent proactively initiate retrieval when needed and bring only the fragments relevant to the current sub-goal into the context.
    • Structure over natural-language stacking: Write tool results back in a structured form with fields whenever possible, reducing the model’s parsing ambiguity.

    A simplified context assembly example:

    1. def build_context(system, memory, retrieved, history, tools):
    2. return {
    3. "system": system, # Stable instructions, highest priority
    4. "long_term": memory.summary(), # Compressed long-term conclusions
    5. "evidence": retrieved, # Evidence retrieved this round, with source tags
    6. "recent": history.tail(6), # Recent verbatim text, kept faithful
    7. "tools": tools.schema(), # Capability list and parameter constraints
    8. }

    4. Tool and Capability Invocation: Caging Uncertainty

    Tool invocation is the watershed that distinguishes an agent from a pure conversational model, and it is also where risk is most concentrated. Engineering practice suggests the following constraints:

    • Declarative capability descriptions: Every capability should have a clear purpose, parameter schema, side-effect description, and failure semantics so the model can select correctly.
    • Dual validation: Parameters given by the model first go through schema validation, then business and permission validation; any failure returns a structured error rather than interrupting directly.
    • Idempotency and replayability: Introduce idempotency keys for operations with side effects to ensure retries do not cause duplicate writes.
    • Least privilege: Grant only the minimum necessary permissions within the task scope, and require explicit confirmation for sensitive operations.
    • Result trimming: Trim overly long output before writing back, keeping only the fields needed for decisions to avoid a single call blowing up the context.

    5. Layered Memory: Fidelity in the Short Term, Distillation in the Long Term

    Memory is not “storing all chat records” but layered management:

    Layer Content Lifecycle Purpose
    Working memory The context of the current loop A single task Supports continuous reasoning
    Session memory Recent multi-turn interactions One session Maintains referential consistency
    Long-term memory Distilled preferences and conclusions Across sessions Personalization and experience reuse

    Before writing to long-term memory, filtering and deduplication are required; when reading, results should be ranked by relevance and the injection volume limited, otherwise memory degrades into a noise source.

    6. Reliability and Observability: Making the System Operable

    An agent is a hybrid of probabilistic and deterministic components, so you must assume “some step will fail.”

    • Multiple termination conditions: Goal achieved, step limit, time budget, cost budget, consecutive failure threshold—any one triggers convergence.
    • Classified error handling: Retry retryable errors with backoff; feed parameter errors back as hints so the model can self-correct; report permission errors directly; force a strategy switch when the loop spins idle.
    • Full-chain tracing: Record each step’s decision, parameter summary, execution time, result status, and context version to form a replayable trace.
    • Evaluation and regression: Build a fixed task set and continuously regress on success rate, average steps, and failure type distribution to avoid hidden degradation from prompt changes.

    7. Common Anti-Patterns

    1. Stuffing all logic into one extremely long prompt, making it non-debuggable and non-reusable.
    2. Letting the model directly concatenate and execute external commands or code without a sandbox or validation.
    3. Having no termination condition, resulting in idle spinning of “repeated retrieval—repeated failure.”
    4. Context that only grows and never shrinks, with cost and latency deteriorating linearly as rounds increase.
    5. Recording only the final answer and not the intermediate trace, making faults impossible to locate.

    8. Conclusion

    The essence of an agent is building a controllable execution framework on top of probabilistic reasoning: replacing the straight line with a loop, replacing prompt stacking with context engineering, constraining action with declarative capabilities and dual validation, maintaining continuity with layered memory, and ensuring operability with multiple termination conditions and full-chain tracing. Once you understand these five layers of structure, you will no longer mistake an agent for a single request-response, and you will have the foundation to build a stable, observable, and scalable agent system on top of the general capabilities of cloud platforms such as Baidu AI Cloud (for example, container platforms, message queues, object storage, log services, and monitoring and alerting).