Agent Execution Architecture: A Practical Guide to Evolution

    This article explains why the traditional host-and-extension model limits AI agents, and how to evolve toward a self-contained execution loop. It introduces a five-layer decoupled architecture, contract-based role definitions, four-part context engineering, a plan-execute-verify tool loop, layered memory, sandboxing and least-privilege security, observability and cost control, common anti-patterns, and a four-step adoption roadmap with quantifiable acceptance criteria.

    1. Background: Why the Host-and-Extension Relationship Needs Redefining

    In the early days of agent applications, a common pattern was to let a mature coding tool or task scheduling platform act as the controller, packaging newly built agents as callable extension components. This approach is cheap to integrate and fast to validate, but the agent’s capability boundary is tightly locked by its host.

    As task complexity grows, the problems concentrate in four areas:

    • Limited scheduling semantics: the host exposes only a few invocation entry points, so the agent cannot decide for itself whether to verify first or retrieve first.
    • Truncated context: key conclusions from earlier turns cannot be reliably passed back, so the model has to re-understand the task every turn.
    • Difficult failure recovery: exceptions occur on the host side, and the agent has no control over retry, rollback, or compensation.
    • Broken audit trail: logs are scattered across the host and the component, making it hard to reconstruct a complete decision process.

    The core of the architecture evolution is therefore not “swapping out a tool” but taking control back into the agent itself and establishing a self-consistent execution loop.

    2. Target Architecture: A Five-Layer Decoupled Execution Model

    A sustainable agent system can usually be abstracted into five layers:

    1. Role layer: defines identity, capability boundaries, refusal policies, and output contracts.
    2. Cognition layer: handles context assembly, retrieval augmentation, memory read/write, and state management.
    3. Orchestration layer: handles task decomposition, plan generation, tool selection, and flow control.
    4. Execution layer: runs commands, reads and writes files, and calls external capabilities in an isolated environment.
    5. Observability layer: collects traces, metrics, and cost data to support replay and continuous optimization.

    The key principle: upper layers only declare intent; lower layers implement and give feedback. No layer should assume the implementation details of another.

    3. Role Definition: Upgrading “Prompts” into “Contracts”

    A role is no longer a piece of natural language description but a verifiable contract that contains at least an input schema, an output schema, and prohibited actions:

    1. {
    2. "role": "backend-agent",
    3. "input_schema": {"task": "string", "repo_ref": "string"},
    4. "output_schema": {"summary": "string", "changed_files": ["string"], "risk": "low|mid|high"},
    5. "constraints": ["do not modify production config", "do not output credentials", "high-risk operations require a second confirmation"]
    6. }

    The benefits: outputs can be validated programmatically, anomalies can be intercepted automatically, and behavior can be reproduced reliably. In practice, keep the contract under version control together with the code, and route every change through review.

    4. Context Engineering: The Real Variable That Decides Success

    Once model capabilities converge, differences in results come mainly from context quality. A four-part structure is recommended:

    • Stable part: role contract, coding standards, directory conventions; cacheable and reusable.
    • Task part: the current goal, acceptance criteria, and boundary conditions.
    • Evidence part: retrieved code snippets, log summaries, and excerpts from interface documentation, each with a source identifier.
    • History part: confirmed conclusions and open questions, compressed into a structured summary rather than raw dialogue.

    A practical rule: evidence over memory, summary over source text. When evidence conflicts with history, the latest evidence wins, and the conflict should be explicitly flagged in the output.

    5. Tool Orchestration and the Execution Feedback Loop

    Tools should follow a “few and orthogonal” principle: each tool has a single semantic, clear error codes, and is idempotent and retryable. A recommended minimal toolset covers file retrieval, content reading, snippet writing, command execution, and external capability invocation.

    The orchestration logic is best structured as a three-stage plan-execute-verify loop:

    1. plan = decompose(task) # generate verifiable sub-goals
    2. result = run(plan.step, sandbox) # isolated execution
    3. if not verify(result): # judge with tests or static checks
    4. plan = revise(plan, result) # revise the plan with failure evidence

    The point here is that verify must be done by deterministic means, such as unit tests, type checks, or rule validation, not by letting the model grade itself. Self-assessment easily produces “false passes,” which is the root cause of inconsistent results in many agent projects.

    6. Layered Memory: Controlling Cost and Noise

    Memory should be split into three layers serving different time scales:

    • Working memory: intermediate variables of the current task, destroyed when the task ends.
    • Project memory: architecture decisions, interface conventions, known defects; retained long term but consolidated periodically.
    • Archive memory: raw historical traces in cold storage, retrieved on demand only for audits and retrospectives.

    Writes must have a gate: only verified conclusions are allowed into project memory, so that wrong lessons are not amplified repeatedly.

    7. Isolation, Security, and Least Privilege

    The execution layer is untrusted by default. The recommended approach is a containerized sandbox with a read-only root filesystem, an independent network policy, and temporary credentials; high-risk actions such as deletion, deployment, and permission changes require human confirmation. Credentials are injected through the environment rather than written into prompts, and sensitive fields are masked in logs.

    8. Observability and Cost Governance

    A complete trace should record at least: task identifier, plan version, tool call sequence, elapsed time, token consumption, verification result, and a fingerprint of the final artifact. In practice, use a unified logging service to collect structured events, set thresholds on monitoring and alerting for spikes in failure rate and latency, and store artifacts and replay data in object storage.

    Three levers for cost governance: caching the stable context part, truncating evidence snippets on demand, and capping the number of retries. Retries without a cap are often the main source of runaway bills.

    9. Common Anti-Patterns

    • Letting the model act as the judge, with no deterministic verification.
    • Unbounded context growth, so key constraints get drowned out.
    • Overlapping tool semantics, making selection hard for the model.
    • Writing to memory with no gate, so wrong lessons reinforce themselves.
    • Recording only the final answer and not the decision trace, which makes problems impossible to review.

    10. Adoption Roadmap and Summary

    A four-step progression is recommended: first solidify the role contract and output validation; then add evidence-based context and make tools idempotent; then introduce the sandbox and least privilege; finally complete trace collection and cost thresholds. Each step should have quantifiable acceptance criteria, such as validation pass rate, average number of retries, and the cost range per task.

    The essence of architecture evolution is turning uncontrollable “conversational trial and error” into a controllable “engineering pipeline.” Only when roles are verifiable, context is traceable, execution is isolated, failures are recoverable, and the process is observable are agents truly ready for production. With common cloud capabilities such as container platforms, logging services, monitoring and alerting, and object storage, the cost of building and operating this loop can be reduced further.