Decoupling the Model and Execution Layers in AI Agent Architecture

    AI agents become reliable in production when language models are separated from the systems that execute their decisions. This article explains how to define the responsibilities of each layer, design replaceable tools, sessions, sandboxes, and control loops, manage state and cost, enforce security boundaries, and validate that models and execution components can be changed without destabilizing task behavior.

    An AI agent can complete real-world tasks for more than just generating text. The model interprets intent, creates a plan, and proposes the next action. The execution layer is what applies that action to files, APIs, data, and permission boundaries.

    When these two layers are tightly integrated, a product may be quick to build and easy to use. However, changing the model, tools, or runtime environment can cause the entire behavior of the system to drift. A more robust approach is to define an agent as a testable composition:

    Agent = model + execution layer

    The model should be replaceable. The tools, sessions, sandboxes, and control loop in the execution layer should be replaceable as well.

    Define the Responsibilities of Each Layer

    The model layer handles uncertain language and reasoning. It converts natural language into structured intent, selects the next step in a multi-step task, and suggests how to recover from failures. It should not directly hold host-machine privileges or implicitly decide where logs are stored, how secrets are injected, or how concurrent tasks are isolated.

    The execution layer provides deterministic engineering capabilities. Typical components include:

    • Tool adapters: Standardize operations such as reading files, running tests, querying logs, and calling APIs. The interface should include parameter validation, timeouts, retries, and result truncation.
    • Task planning and scheduling: Break long-running work into recoverable steps, record dependencies, and avoid rerunning steps that have already completed successfully.
    • Sessions and memory: Distinguish short-term context, task state, and long-term knowledge. Clearly define which information may persist across sessions.
    • Sandboxing and permissions: Restrict filesystem, network, and process capabilities so failures can be rolled back and the blast radius remains predictable.
    • Loop control: Decide when to call the model again, when to stop, and when to request human confirmation.
    • Observability: Record every tool call, model decision, and resource cost as a searchable trace.

    Once the boundary is clear, evaluation becomes meaningful. If the model changes, evaluation should focus on reasoning quality. If the execution layer changes, evaluation should focus on tool reliability, isolation strength, and recovery behavior. When the two layers are mixed together, no metric can be reliably attributed to either one.

    Tightly Coupled and Replaceable Architectures

    Two patterns are common in the industry.

    The first packages the model, toolchain, and user interface as a complete product. The default model, permissions, and control loop are fixed in the product. This creates a short path for individual users, but makes it difficult for enterprises to integrate existing identity systems, repository policies, and audit requirements.

    The second removes the model from the center of the architecture and treats it as a pluggable component. Session storage, sandbox implementations, and even the agent loop are connected through stable interfaces.

    Replaceability does not mean unlimited flexibility. If interfaces are unstable, plugins simply move complexity into adapter layers. A practical principle is to expose only:

    • A minimal action protocol to the model
    • A minimal lifecycle to plugins
    • Tasks and permissions to users

    Internal prompts and temporary file layouts should not become part of these interfaces.

    An action protocol should answer at least four questions:

    1. What is the action name and parameter schema?
    2. What structure is returned on success?
    3. Is the error retryable?
    4. What permissions does the action require?

    The model may choose only actions defined by the protocol. It must not construct arbitrary commands to bypass the execution layer. Otherwise, “everything is a plugin” quickly degenerates into “everything is an executable string,” and the security boundary disappears.

    Where Plugins Belong

    “Everything as a plugin” is a useful architectural slogan, but plugins must be separated by domain. All extensions should not use the same loading path.

    Tool Plugins

    Tool plugins address capability requirements. Every external system introduces new failure modes, including timeouts, insufficient permissions, oversized responses, and semantic inconsistencies.

    Tool descriptions should be short, parameters should be narrowly scoped, and the execution layer should enforce allowlist validation. The model should see business-level semantics such as “find the cause of the most recent build failure,” rather than a long sequence of low-level commands.

    Session Plugins

    Session plugins address state management.

    A short task may need only a message list. A refactoring task that runs for hours needs checkpoints containing information such as:

    • Paths that have been modified
    • Checks that have already been run
    • Decisions waiting for confirmation

    The storage medium may be a local file or object storage. The interface should promise only that checkpoints can be read and written by task identifier. It should not promise that a particular serialization format will remain unchanged forever. Format versions must be migratable.

    Sandbox Plugins

    Sandbox plugins address isolation. Read-only code browsing, a writable workspace, and integration testing with access to an internal network represent three different trust levels.

    With replaceable sandbox implementations, the same control loop can use lightweight isolation on a developer laptop and stronger isolation in continuous integration. Changing isolation strength should not require rewriting the planning logic.

    Loop Plugins

    Loop plugins address control behavior. Some tasks are best handled as “the model proposes, a human confirms, and then the action executes.” Others can iterate automatically when a known test fails.

    The loop itself can be replaced, but stopping conditions must remain outside the model. Maximum steps, maximum tokens, maximum cost, and circuit breaking after repeated identical errors should not exist only in a prompt.

    The Execution Path of a Task

    Consider a task such as adding input validation to an existing service and getting its tests to pass. The execution layer should not send the entire request to the model in one operation.

    Step 1: Establish the task identity and permission scope. Define the directories that may be read, the directories that may be modified, secret paths that must not be accessed, and the test commands that are available.

    Step 2: Create bounded context. Provide the repository structure, relevant interfaces, and a summary of failing tests. Avoid putting the entire codebase into the model context.

    Step 3: Request only the next action. The model outputs the next action. The execution layer validates it, runs it in the sandbox, and writes the structured result back to the trace.

    Step 4: Handle test failures. If a test fails, the loop reads a failure summary rather than the complete log. It then decides whether to modify the code, add a test case, or stop and request human judgment.

    Step 5: Archive the result. At the end of the task, archive the diff, commands, duration, and unresolved risks.

    The key is not an elaborate prompt. Every step must be replayable. Replayability depends on three things:

    • An immutable input snapshot
    • Versioned tool definitions
    • A time-ordered trace

    Without any one of these, it becomes difficult to determine whether a production issue was caused by model drift or tool drift.

    Action results should use stable fields rather than an unstructured block of free text:

    1. action: run_checks
    2. status: failed
    3. retryable: true
    4. summary: 2 tests failed, both in input validation
    5. artifacts: [check-log-id]

    With stable fields, different models can consume the same result. If the logging service changes, only the plugin that generates check-log-id needs to be replaced.

    State, Context, and Cost

    A context window is not a memory system. Appending the entire message history to every request is expensive and can cause the model to focus on outdated information.

    The execution layer should actively compress context by retaining:

    • The goal
    • Constraints
    • The current diff
    • The most recent failure

    Earlier exploration should be summarized, with the source of each summary recorded for later review. Compression is an execution-layer policy, not a private trick tied to a particular model. This helps prevent behavior from losing important context when the model changes.

    Cost should also be governed by the execution layer. Per-step limits, per-task limits, and early termination after repeated failures are more effective than calculating the bill after the task is complete.

    Tool responses should support truncation and tell the model when a result has been truncated, along with an identifier that can be used to retrieve the complete result. This prevents the model from making excessive inferences from incomplete text. These controls do not depend on model-specific parameters; they depend on consistent interception before and after each call.

    Security and Permissions Must Be Designed Early

    Once the execution layer can modify files, run commands, or access internal APIs, it becomes a high-privilege component.

    Least privilege must be applied at the task level:

    • Default to read-only access; grant write access explicitly.
    • Disable network access by default and enable only declared dependencies.
    • Inject secrets into the sandbox through the execution layer rather than placing them in model-visible context.
    • Bind human confirmation to a specific diff or external call instead of prompting for approval at every step.

    If users are asked to confirm every operation, confirmation quickly becomes a formality.

    Plugin provenance must also be auditable. Loading a local plugin and loading a remote plugin represent different trust levels. At minimum, record the plugin identifier, version, and hash, and prevent plugins from being replaced while a task is running. Otherwise, the trace cannot prove that the code executed at the time was the same code being inspected later.

    How to Validate Replaceability

    An architecture that claims the model and tools are replaceable needs acceptance tests rather than slogans.

    • Keep the task set and execution layer fixed, change only the model, and compare completion rate, unnecessary modifications, and the number of human interventions.
    • Keep the model fixed, change only the sandbox or session store, and confirm that task results remain consistent and permission denials still work.
    • Inject recoverable failures, such as a test command timeout, and verify that the task resumes from a checkpoint instead of starting over.
    • Sample execution traces and confirm that secrets, tokens, and unrelated personal data never enter the model context.

    When acceptance tests fail, fix interfaces and permissions before adding more business rules to the prompt. Prompt patches are difficult to reuse across models and can conceal defects in the execution layer.

    A Practical Implementation Order

    A team does not need to implement every plugin at once. A reasonable sequence is:

    1. Standardize the action protocol and execution traces.
    2. Add frequently used file and test tools to an allowlist.
    3. Introduce checkpoints so long-running tasks can resume.
    4. Replace the sandbox and loop strategies as needed.

    Each step can deliver value independently. Trying too early to make every model, tool, and loop simultaneously pluggable often produces a framework with no default path, leaving developers unsure where to investigate failures.

    The maturity of agent engineering is not measured by whether conversations feel more human. It is measured by whether task boundaries, permission boundaries, and audit records remain stable when the model or tools change.

    The model is responsible for judgment. The execution layer is responsible for constraints, execution, and traceability. Once the two are decoupled, plugins have clear integration points. With those integration points defined, teams can place agents into real development workflows instead of stopping at one-off demonstration scripts.