Plugin-Based AI Agent Runtimes: A Dual-System Architecture

    A production-ready AI agent framework needs more than a model-and-tool loop. This article explains a dual-system architecture in which a runtime plugin graph manages capabilities, dependencies, scopes, and resource lifecycles, while an append-only event stream preserves execution history. It covers configuration loading, capability resolution, session projections, recovery, fault boundaries, and a practical implementation checklist.

    Moving an AI agent application from a demo to a product exposes a fundamental challenge: building a minimal execution loop is straightforward; keeping models, tools, prompts, sessions, storage, execution sandboxes, approval policies, host interfaces, and plugin lifecycles working together is not.

    Many frameworks treat “pluggability” as a collection of packages with initialization hooks. Architectural quality, however, depends on how plugins resolve dependencies, isolate scopes, release resources when unloaded, and turn static configuration into an inspectable, updatable runtime structure.

    A useful approach separates these concerns into two systems:

    • The runtime plugin graph describes what the system currently consists of.
    • The append-only event stream records what the system has done.

    The agent loop connects them: it obtains capabilities from the plugin graph and writes execution facts to the event stream.

    1. Why Production Frameworks Need More Than an Agent Loop

    A minimal agent loop typically has four steps: assemble a prompt, call a model, execute tools, and return the results to the model. That is enough to validate an idea, but not to support a production application.

    A production framework must also handle model and credential management, session recovery and context compaction, tool policies and human approval, sandboxes and remote execution, multiple host environments, subagent collaboration, plugin activation and deactivation, and browser interfaces that observe the same execution history.

    Hard-coding these capabilities into the main loop quickly turns it into an oversized module that is difficult to test and evolve.

    A better division of responsibility assigns composition complexity to the runtime plugin graph and historical continuity to the event stream.

    The plugin graph answers:

    • Which capabilities are available now?
    • Which components depend on which others?
    • In which scopes are they available?
    • How are resources cleaned up when a component exits?

    The event stream answers:

    • What happened during an execution?
    • What information did the model see?
    • How can the interface reconstruct the execution?
    • What history supports recovery and branching?

    These responsibilities are distinct, but the agent loop must connect them. Without a plugin graph, capabilities are difficult to replace and isolate. Without an event stream, recovery, auditing, and observability lack a reliable foundation.

    2. Architecture Overview: Separate Composition from History

    The runtime plugin graph

    The runtime plugin graph is a directed graph of capabilities. Nodes represent plugin instances, while edges represent dependency and provision relationships. Each node has a scope, such as global, session, request, or subagent.

    During startup, plugins register capabilities with a context. During shutdown, they release registrations, listeners, timers, processes, and handles.

    The graph also determines whether required dependencies are ready, whether optional dependencies can be skipped, and how capabilities with the same name resolve across scopes.

    The append-only event stream

    The event stream is a log that accepts new records without overwriting existing ones. It records facts such as user input, model requests and responses, tool calls, approval decisions, sandbox output, and plugin state changes.

    The event stream is not itself the model context. Instead, projections derive different views from it:

    • A message sequence for the model
    • An execution trace for the interface
    • Checkpoints for recovery
    • Historical slices for session branching

    Projections can be replayed, trimmed, or compacted while the underlying events remain traceable.

    The agent loop

    The agent loop sits between these systems. It resolves the models, tools, prompts, and session services visible to the current session, constructs context, and calls the model.

    When the model requests a tool call, the loop uses the plugin graph to locate the execution capability, applies approval requirements and sandbox constraints, and appends the result to the event stream.

    The loop owns neither the capabilities nor the history. Its job is orchestration.

    3. From Static Configuration to a Runtime Plugin Tree

    At startup, a framework should not immediately construct a fixed application. It should first read base configuration files, capability packages, and user patches. A loader then mounts configuration entries as plugin instances.

    This process should include at least four steps:

    1. Parse configuration.
    2. Validate dependencies.
    3. Start plugins in topological order.
    4. Expose a runtime snapshot.

    Configuration entries describe which capabilities are needed. The loader determines how to instantiate them. The plugin graph determines when they become visible.

    The following pseudocode illustrates the relationships:

    1. interface PluginNode {
    2. id: string
    3. scope: Scope
    4. requires: CapabilityKey[]
    5. provides: CapabilityKey[]
    6. start(ctx): void
    7. dispose(): void
    8. }
    9. function mount(configLines, graph, ctx) {
    10. for (line of configLines) {
    11. node = instantiate(line)
    12. graph.add(node)
    13. }
    14. graph.validate()
    15. graph.startInTopologicalOrder(ctx)
    16. }

    This example does not prescribe a specific implementation. Its important properties are:

    • Configuration produces inspectable plugin nodes rather than directly constructing business objects.
    • Dependency validation happens before startup.
    • Dependency topology determines startup order.
    • Runtime snapshots are available for diagnostics.

    User patches should support overrides, disabling entries, additions, and parameter changes. Every change must pass through the same validation and startup process so that hot updates cannot bypass dependency checks.

    4. Five Core Concerns in the Plugin Kernel

    4.1 Capability visibility

    A plugin should not retrieve arbitrary capabilities through global singletons. A safer approach gives each plugin a restricted context at startup, allowing it to resolve only declared dependencies or capabilities permitted within its scope.

    This makes it possible to inject different implementations into the same plugin across sessions and to substitute implementations during testing.

    4.2 Startup behavior when dependencies are unavailable

    Missing required dependencies should block startup with a clear diagnostic. Missing optional dependencies should allow degraded operation or deferred activation. Dependency cycles should be detected and rejected.

    Remote capabilities also require a distinction between registered and ready. Readiness checks or lazy resolution can help prevent cascading startup failures.

    4.3 Scope resolution for capabilities with the same name

    Sessions, requests, and subagents may need different implementations of the same capability—for example, model adapters for different tenants or tool policies for different sessions.

    Resolution rules should define an explicit precedence:

    1. Request scope
    2. Session scope
    3. Global scope

    Conflicts within the same scope should produce an error rather than silently overwrite an existing capability.

    4.4 Cleanup responsibility

    The plugin graph should centrally track registrations, listeners, processes, and handles so they can be released when a plugin exits.

    Each plugin should provide idempotent cleanup logic. The graph should unload plugins in reverse dependency order and wait for asynchronous cleanup to finish. Resources that remain unreleased should generate diagnostic events rather than go unnoticed.

    4.5 Updating the runtime tree

    A configuration change should produce a new plugin-tree snapshot. After validation, the runtime can transition to it incrementally. If the update fails, it should roll back to the previous snapshot.

    The runtime tree must remain inspectable. Current nodes, dependency edges, scopes, health states, and cleanup states should all be queryable.

    5. The Agent Loop as a Thin Orchestration Layer

    The agent loop should remain a thin orchestration layer rather than become a module containing every business concern.

    Each iteration follows a consistent sequence:

    1. Resolve the model, tools, prompts, and policies visible to the current session.
    2. Construct model context from an event-stream projection.
    3. Call the model and interpret any tool requests.
    4. Apply approval requirements and sandbox constraints.
    5. Append execution results to the event stream.
    6. Decide whether to continue or finish.

    This separation has two main benefits.

    First, capability replacement does not require changing the loop. Replacing a model adapter, tool catalog, or prompt template is a plugin-graph change.

    Second, historical continuity is independent of execution logic. Session recovery replays the event stream and rebuilds projections rather than relying on mutable state held inside the loop.

    The loop should therefore be as stateless as practical: state belongs in the event stream, while capabilities belong in the plugin graph.

    6. Projections of the Session Event Stream

    The event stream should support at least three types of projection.

    Model projection

    A model projection turns events into a message sequence. It handles context-window limits and compacted summaries so the model receives an appropriate view of the history.

    Interface projection

    An interface projection converts events into a readable execution trace, including tool calls, approval steps, and outputs.

    Recovery projection

    A recovery projection produces checkpoints that support resuming execution from a particular event sequence number or creating a new session branch.

    Branching does not mean copying mutable objects. It means creating a new event-stream branch based on a shared historical prefix.

    The event stream should also record plugin-graph changes, including when a capability was enabled, replaced, or unloaded. When an execution produces an unexpected result, this makes it possible to answer both “What did the system consist of at that time?” and “What happened?”

    For auditing scenarios, events should be appended in a tamper-resistant manner, while sensitive fields should be exposed through redacted projections.

    7. Lifecycle Management and Fault Boundaries

    The most common failures in plugin-based systems are not necessarily model-call errors. Resource leaks and dependency mismatches are often more damaging.

    A practical lifecycle has five stages:

    1. Registration
    2. Readiness
    3. Running
    4. Degraded operation
    5. Unloading

    Each stage should have explicit events.

    A plugin startup failure should remain isolated to the affected node rather than bring down the entire tree. Failure of a critical dependency should trigger session-level degradation. A noncritical dependency failure can be recorded as a diagnostic while execution continues.

    Unloading must respect reverse dependency order: stop dependents before stopping the capabilities they rely on. Asynchronous cleanup needs a timeout, and repeated unload operations must be idempotent.

    For subagents and remote execution, the plugin graph should track handles and processes so that session termination can reclaim them consistently.

    8. Implementation Checklist

    Use the following questions to evaluate an implementation:

    • Do configuration changes pass through a shared validation process?
    • Is capability resolution constrained by scope?
    • Do missing required dependencies fail fast?
    • Are conflicts between capabilities with the same name handled explicitly?
    • Is unloading traceable and idempotent?
    • Is the event stream append-only, with support for projections?
    • Do recovery and branching operate on historical prefixes?
    • Does the agent loop remain a thin orchestration layer?
    • Does the interface observe the same underlying execution facts?
    • Are plugin-graph snapshots available for diagnostics?

    In cloud or self-managed enterprise deployments, persistence and session archiving can map to general-purpose object storage, logging, monitoring, and alerting services. The architecture should remain interface-neutral rather than couple the plugin graph or event stream to a particular service API.

    For frameworks in developer preview, current interfaces should not be treated as stable commitments. Adapter layers can isolate the rest of the system from interface changes.

    The central value of this dual-system architecture is the separation of what the system consists of from what the system has done. A thin agent loop connects the two, creating a maintainable balance between capability replacement, session recovery, scope isolation, and lifecycle cleanup.