Designing a Plugin-Based Agent Runtime: Architecture and Implementation Practices

    This article explains how to build an agent runtime from replaceable plugins. It covers the core problems a runtime must solve, how to split responsibilities between a minimal meta-framework and plugins, how to make side effects reversible on unload, four common run modes, the control loop and failure handling, security and evaluation, and a suggested implementation order.

    Reliable agents cannot depend only on a model’s single generation. The model handles understanding and planning. The runtime turns those plans into tool calls, state persistence, permission constraints, and failure recovery. A common view in the industry is that a model plus a runtime makes an agent executable. This runtime is often called a harness. It does not replace the model. It provides the loop, context, tool protocol, and security boundaries.

    If tools, interfaces, storage, and scheduling are hard-coded in the same code path, replacing any one of them affects the whole system. A safer approach is an open architecture in which every capability is pluggable. The meta-framework handles only plugin loading, unloading, and dependency resolution. Model access, tools, skill packs, sessions, sandboxes, storage, loop policies, scheduling, and user interfaces all exist as independent plugins. Plugins collaborate through service registration and events, and they are combined in the configuration layer instead of hard-coding vendors or interaction details in the source code.

    Core Problems the Runtime Must Solve

    When developers build agents, the common pain points are rarely about connecting one more model API. The real issues are whether a task can be repeated, whether a failure can be explained, and whether side effects can be undone. Typical gaps include:

    • Tool calls lack a unified protocol, so parameter validation and timeout policies are scattered across business code.
    • Multi-turn sessions and long-term memory are mixed together, which makes context hard to trim as it grows.
    • File writes, command execution, and other side effects have no defined scope. After a plugin is disabled, the environment remains polluted.
    • Scheduling, retries, and human confirmation have no clear layers, so automation and controllability conflict.
    • Interfaces, evaluation scripts, and online services share the same implicit assumptions, so replacing any layer requires changing core logic.

    The value of a plugin-based runtime is that it turns these concerns into replaceable components. A clear lifecycle keeps the combined behavior predictable.

    Division of Responsibilities Between the Meta-Framework and Plugins

    The meta-framework should stay minimal. It does three things: it resolves dependencies from configuration and loads plugins, it notifies plugins to clean up in reverse order when they unload, and it provides service discovery and an event bus. It does not implement specific dialogue strategies, and it is not tied to any one model or tool format.

    Each plugin exposes a stable contract externally and implements one capability internally. The following sketch explains the responsibilities only. It is not the real interface of any specific product:

    1. plugin:
    2. name: tool-runtime
    3. requires: [session, policy]
    4. provides: [tool.invoke]
    5. lifecycle:
    6. load: register tool descriptions and permission declarations
    7. start: establish a connection to the sandbox
    8. stop: reject new calls and wait for in-flight requests to finish
    9. unload: revoke registered services and clean up temporary state

    Collaboration should use two mechanisms. Services suit synchronous capabilities that return values, such as “invoke a tool by name” or “read the current session summary.” Events suit broadcasting state changes, such as “a reasoning round has ended,” “the policy rejected a call,” or “a plugin is about to unload.” The configuration layer decides which plugins are enabled and the direction of their dependencies. Developers can therefore swap the model plugin or the sandbox plugin without changing the runtime core.

    Dependencies should be directed, and cycles should be detectable. A model plugin should not depend back on an interface plugin. A minimal evaluation interface should not assume a full web session exists. This way, the same loop and tool protocol can serve both interactive debugging and batch evaluation.

    Reversible Side Effects and Unload Semantics

    A plugin architecture that supports installation but not clean removal offers only the appearance of composability. File locks, temporary directories, event subscriptions, background timers, and in-process patches should all be treated as reversible side effects. Each one is registered on load and revoked on unload, in reverse order of registration.

    Several constraints apply in practice. Every side effect must have an owner, and anonymous changes to global state are not allowed. Revocation must be idempotent, so unloading twice must not raise unhandled errors. Shared resources across plugins are released according to the dependency graph, so the plugins they depend on are released last. External actions that cannot be undone, such as messages already sent or writes already committed, should go through explicit confirmation or a compensation process before they run. The runtime should not pretend that unloading can roll back everything.

    Reversibility directly supports safe iteration. A team can switch tool sets within the same process or configuration template. Before enabling the new tools, the team can confirm that the old tools’ registrations, subscriptions, and temporary credentials have all disappeared.

    Four Common Run Modes

    A runtime does not need only one loop. Based on task risk and interaction density, engineers commonly use four modes. These can be built as policy plugins instead of forked code.

    Standard mode serves general-purpose assistants. The model plans, the runtime executes tools, and observations are written back into the context until the task finishes or reaches a step limit. This mode suits most office and engineering assistance tasks. Configure a maximum step count, a per-call timeout, and a budget to prevent the loop from spinning without progress.

    Programmatic tool-calling mode hands a batch of tool orchestration to a controlled program. The model generates or selects the program, and the runtime interprets and executes it in a sandbox, then returns the results. This mode suits tasks with many steps, clear branches, and a need to reduce round trips. The risk is that the program’s permissions must be smaller than those of the user interacting with it. Arbitrary network access and arbitrary write paths should be disabled by default.

    Minimal mode removes the interface and extra skills. It keeps only the model, a minimal tool set, and the inputs and outputs needed for evaluation. It suits regression testing and comparing different models under the same protocol, so interface plugins or caching strategies do not distort the results.

    Creative mode relaxes the restrictions on available skills and intermediate artifacts. It encourages multi-step drafts, revisions, and material organization, but it must still keep audit logs and interruption points. This mode suits content production and proposal drafting. It should not connect directly to high-privilege production systems.

    A mode is a strategy, not a security boundary. In every mode, write operations, external sends, and credential use should pass through an additional policy plugin.

    How Key Components Are Separated

    The model plugin handles only inference requests, streaming output, and usage statistics. It does not store business sessions. The tool plugin declares its name, parameter schema, side-effect level, and required permissions. The skill plugin packages reusable prompts, workflows, or tool combinations. Skills should be versioned so they are not tied to runtime upgrades. The session plugin manages short-term context, trimming, and checkpoints. The storage plugin separates temporary workspaces from retained memory, and writing to memory requires an explicit policy. The sandbox plugin provides isolated views of the file system, processes, and network. The loop plugin decides when to call the model again, when to stop, and when to request human confirmation. The scheduling plugin handles concurrency, queues, and timeouts. The interface plugin only consumes events and calls the same services. It does not duplicate business rules.

    With this separation, replacing the model does not require changing the tools. Replacing the sandbox does not change the session structure. Adding a new interface does not change the loop.

    The Control Loop and Failure Handling

    A typical round of the loop can be summarized as follows. Assemble the context, request the model, parse structured actions, validate them against policy, execute or reject them, write the observations, and decide whether to finish. Each step should record a correlation ID so the run can be replayed afterward.

    Failures should be classified rather than retried uniformly. Invalid parameters point to a problem with the model or the tool contract, so the runtime should return an understandable error and limit retries. Timeouts and temporary unavailability of downstream services can be retried a limited number of times. Permission denials should not be retried automatically. If the sandbox crashes, discard that workspace and resume from the last checkpoint. When the step or budget limit is reached, output the current progress and the reason the task is incomplete instead of stopping silently.

    Human confirmation suits high-impact actions such as sending messages, deleting data, making payments, or changing shared configuration. The policy plugin declares the confirmation points, and the loop plugin only pauses and resumes.

    Security, Multi-Tenancy and Evaluation

    Plugins can extend capabilities, but they can also widen the attack surface. Tool descriptions and skill text should be treated as untrusted input, so prompt injection cannot chain high-privilege tools together. The default tool set should be small, and additional tools should be granted per task. Credentials should be kept in the runtime’s secret management. They should not enter the model context or appear in ordinary logs. Sandbox network access and file roots should be disabled by default, and each tool should open only the minimum scope it declares.

    In multi-tenant settings, sessions, storage, and sandboxes are isolated per tenant. Plugin instances can share code but must not share mutable global state. When one tenant’s skill is unloaded, the services registered by other tenants must not be affected.

    Evaluation should use minimal mode with a fixed tool protocol. Record the trajectory, not just the final answer. Compare task completion rate, redundant steps, and policy-violating calls under the same runtime constraints. Do not count interface differences as model capability. Trajectory logs also help with debugging. They show which call was rejected, which round trimmed the context, and which plugin failed to unload.

    Engineering Support for Deployment

    The runtime itself should be stateless or horizontally scalable. Session checkpoints and artifacts should go to object storage or a database. Messages should go through a message queue. Run logs should go to a logging service. Latency, failure rate, and tool-call volume should feed into monitoring and alerting. A container platform suits hosting sandboxes and runtime processes, which makes it easier to reclaim compute resources per task. If a team already uses Baidu AI Cloud, its general cloud services, such as object storage, message queues, container platforms, logging, and monitoring, can serve as the hosting layer. The runtime does not need to be tied to a specific proprietary interface.

    Configuration should be versioned. Put enabled plugins, dependencies, modes, step limits, permissions, and confirmation policies in one declaration file, and ship it with the application. Changes should first pass regression in minimal evaluation before entering the interactive environment. Plugin packages should include compatibility notes, especially for tool parameters and event fields. This prevents the interface and the loop from drifting apart as each evolves independently.

    Step one: fix the tool protocol and session checkpoints, then get a single-task loop running in standard mode. Step two: add policy validation, sandboxing, and reversible unloading, and confirm that plugins can be safely replaced. Step three: add minimal mode for evaluation and controlled programmatic execution for batched steps. Step four: connect richer skills and interfaces. Stacking skills too early only hides unclear boundaries.

    The mark of mature agent infrastructure is not the number of plugins. It is whether, after replacing any single component, task semantics remain stable, side effects can still be reversed, and failures can still be explained. When the runtime is designed around these goals, model upgrades, tool changes, and interface redesigns do not repeatedly disrupt the core path.