Plugin-Based Agent Runtime Architecture: A Composable Design for AI Agent Platforms
This article breaks down a plugin-based Agent runtime architecture that separates a runtime plugin graph from an append-only event stream. It explains how layered configuration enables capability replacement without code changes, how scoped dependency resolution and lifecycle cleanup keep dynamic plugins safe, and how event projections support context compression, recovery, forking, and consistent UI observation. Readers will also find practical guidance for designing capability contracts and keeping the execution loop independent of specific models, storage, or sandboxes.
1. The Problem: Why Agent Platforms Need a Composable Runtime
In AI coding tools and automation Agent platforms, the most common need is not “one more tool.” It is the ability to let model adaptation, the tool catalog, prompts, sessions, storage, sandboxes, the execution loop, and UI observation evolve independently.
The traditional approach often hardwires all of these capabilities into a fixed application: model invocation is coupled with business logic, tool registration is scattered across the codebase, session recovery depends on a specific storage implementation, and the UI reads runtime state through private interfaces. As a result, replacing any single capability can ripple through a large amount of code.
A design worth borrowing splits the platform into two systems that run at the same time:
- The first maintains a runtime plugin graph. It describes which capabilities make up the system, how those capabilities depend on each other, which scope they take effect in, and how resources are cleaned up on unload.
- The second maintains an append-only event stream. It records what actually happened during a task, and uses that record to project model context, UI traces, and the data needed for recovery and forking.
The execution loop sits between the two. It obtains model, tool, prompt, and session services from the plugin graph, then writes the process back to the event stream.
The core value of this split is straightforward: the plugin graph answers “what is the system made of right now,” while the event stream answers “what did the system just do.” The former addresses composition complexity; the latter addresses historical continuity. Combining the two is what supports a production-grade runtime rather than just a minimal runnable loop.
2. The Configuration Tree: Replacing Capabilities Without Changing Code
At startup, the runtime should not directly construct a fixed application. Instead, it should read layered configuration, apply user patches on top, and let a loader mount plugin instances line by line. This configuration is not a simple key-value table; it is an updatable, inspectable runtime tree.
2.1 Layered Overlay and the Final View
A typical configuration has three layers:
- The base profile provides the minimum viable capabilities.
- Optional bundles introduce the tools and policies required for a specific scenario.
- User patches override specific parameters or replace implementations.
The loader merges them in order to form the final view. Because each layer can be maintained independently, replacing a model adapter or a tool catalog only requires adjusting the configuration selection, not modifying the execution loop source code.
Illustrative configuration logic:
base_profile-> bundle(tooling, session, sandbox)-> user_patch(model=..., approval=...)-> runtime_plugin_tree
2.2 Scoped Dependency Resolution
The same service name may resolve to different implementations across sessions or scopes. For example, one session may use a local sandbox while another uses a remote execution environment; one subtask may use a read-only toolset while another is allowed to write.
When declaring dependencies, a plugin should bind them to the current scope rather than to a global singleton. This way, replacing a capability does not contaminate other sessions.
2.3 On-Demand Startup and Required Capability Checks
Whether a plugin starts depends on whether its declared capabilities are visible at the current location and whether its required dependencies are already present. A sound design avoids preloading everything. Instead, a plugin starts once its dependencies are satisfied and stops once they disappear. This requires the configuration tree to support dependency graph checks, so missing items or circular dependencies can be detected before loading.
2.4 Lifecycle and Resource Reclamation
When a plugin exits, its registrations, listeners, child processes, file handles, and timers should all be reclaimed by the runtime in a unified way. Leaving this to the plugins themselves tends to cause omissions.
A practical approach is to register resources in the cleanup list of the current scope and release them in reverse order on unload. This prevents handle leaks and duplicate listeners when plugins are started and stopped dynamically.
3. The Event Stream: Making Runtime Facts Recoverable and Observable
The event stream uses append-only writes to record task planning, model requests, tool calls, result returns, approvals, and errors. It does not store a single snapshot of “current state.” Instead, it stores a sequence of facts, and a projection layer generates different views from that sequence.
3.1 Context Projection and Compression
Model context should not be identical to the raw event stream. The projection layer can truncate, summarize, or compress historical events according to policy, controlling length while preserving key constraints. The compression process itself can also be written as an event to keep it traceable.
3.2 Recovery and Forking
During recovery, the system replays the event stream to a target position and rebuilds the required service references. During forking, it copies a branch after a given event to form a new task line.
Because the plugin graph supplies the implementations and the event stream only describes facts, the two are decoupled. Recovery therefore does not have to be bound to a specific storage or model.
3.3 The UI Observes the Same Facts
The UI should not maintain an independent runtime state. It should subscribe to projections of the event stream. This way, the CLI, desktop, and web clients all see the same runtime facts, avoiding inconsistent state across clients.
4. The Position and Boundaries of the Execution Loop
The execution loop does only three things:
- Resolve the model, tools, prompts, and session services from the plugin graph.
- Invoke the model and execute tools.
- Write the process to the event stream.
It should not know the model vendor, storage type, or sandbox implementation. A minimal loop can handle only prompt assembly, model invocation, and result return. A production-grade loop relies on the plugin graph to fill in approvals, policies, remote execution, and subtask orchestration.
5. Practical Recommendations
- Define capability contracts before implementing plugins, so plugins do not depend directly on concrete implementations.
- Keep the layered configuration to a minimal base set, and add scenario capabilities through optional bundles.
- Register all external resources in the scope cleanup list to ensure safe dynamic unloading.
- Record only facts in the event stream, not implementation details, and keep projection policies replaceable.
- Run static dependency graph checks before loading to surface missing items and conflicts early.
- Have the UI and recovery logic consume the same projections rather than creating another state source.
Overall, this architecture turns “capability replacement” from a code problem into a configuration and dependency problem, and turns “runtime history” from a state snapshot into projectable facts. For platforms that need multiple models, multiple tools, multiple hosts, and recoverable tasks, this coordination between the two systems offers a clear evolution path.
