Everything Is a Plugin: A Microkernel Architecture for Agent Runtimes

    This article explores a microkernel-style runtime architecture for AI agents, where the kernel keeps only foundational responsibilities and all other capabilities—including the reasoning loop—are delivered as plugins. It covers the kernel’s responsibility boundaries, temporal composability for reversible plugin unloading, spatial composability for capability-based dependencies, engineering practices such as contracts, permissions, unload tests, mocks, and side-effect logs, and the tradeoffs and limitations of plugin-based agent runtimes.

    1. Background: From Loop-Centric to Kernel-Centric

    Early agent frameworks were often centered on the reasoning loop, or agent loop. Model calls, tool dispatch, memory reads and writes, and termination checks were all implemented inside the same loop class. This structure is efficient in the prototype stage, but as the system grows more complex, several problems emerge:

    • Replacing model providers is difficult: When integrating a new model interface, developers often need to modify request construction and response parsing logic inside the loop.
    • Runtime environment coupling: The same file operation tool may have completely different implementations for local disk, containers, or remote storage, and the loop tends to accumulate many branch checks.
    • High testing cost: The entire loop depends on real models and real file systems, making unit tests difficult to isolate.
    • Unclear extension paths: Adding a new capability usually requires changing core code, which makes the core increasingly difficult to maintain.

    Another approach is to design the runtime framework as an operating-system-style microkernel. The kernel handles only the most fundamental responsibilities, and all other capabilities are integrated as plugins. The core judgment behind this design is simple: no component is irreplaceable. The reasoning loop itself is also a plugin, so developers can choose different reasoning strategies based on task type without modifying the kernel.

    2. Kernel Responsibilities and Boundaries

    The value of a microkernel depends on what it does not do. A qualified kernel usually keeps only the following five capabilities:

    1. Plugin loader: Reads plugin declarations, resolves dependency order, and controls the lifecycle of loading and unloading.
    2. Service registry: Maintains the mapping from capability names to implementations and supports registration, lookup, and replacement.
    3. Side-effect ledger: Records all registration actions produced by each plugin during loading, providing the basis for unloading.
    4. Event bus: Broadcasts events before and after tool calls, during session writes, and during prompt assembly, allowing plugins to intercept or modify data.
    5. Context builder: Provides each plugin with a restricted runtime context. A plugin can access only the capabilities it has declared through that context.

    Beyond the kernel, the following capabilities should exist as plugins:

    1. Runtime kernel (loader / service registry / side-effect ledger / event bus)
    2. │
    3. ┌──────┼─────────────┬──────────────┬─────────────┐
    4. │ │ │ │ │
    5. Model adapters Tool collections Session storage Reasoning loop Prompt assembly
    6. │ │ │ │ │
    7. │ Filesystem Sandbox execution Subagents Approval policies

    The key point of this diagram is that all arrows point to the kernel. Plugins collaborate through capability interfaces rather than direct references to one another. As a result, replacing any plugin does not affect other modules.

    Prompt assembly also belongs to the plugin layer. It can register interceptors on the event bus to append safety constraints, trim historical messages, or inject project conventions before a model request is sent. Because interceptors are also plugins, disabling a rule only requires unloading the corresponding plugin.

    3. Temporal Composability: Making Unloading Truly Reversible

    Temporal composability concerns whether all effects produced by a plugin after it is loaded can be fully revoked when it is unloaded.

    Consider a typical plugin. During loading, it may register the following:

    • A tool
    • An event listener
    • A prompt section
    • A model adapter
    • A service

    If residue remains after unloading—for example, if an old tool is still present in the tool list, or an event listener still triggers callbacks—system behavior becomes difficult to predict. Troubleshooting such issues often requires restarting the process, which is costly.

    A common way to achieve reversibility is to “register and return a disposal handle.” Each registration records a reverse operation, and unload executes these operations in reverse order. Reverse order matters because content registered later often depends on content registered earlier.

    The following is an illustrative implementation, intended only to explain the idea:

    1. class PluginScope:
    2. def __init__(self, kernel):
    3. self.kernel = kernel
    4. self._disposers = []
    5. def register_tool(self, name, handler):
    6. self.kernel.tools.add(name, handler)
    7. self._disposers.append(lambda: self.kernel.tools.remove(name))
    8. def on(self, event, listener):
    9. off = self.kernel.events.subscribe(event, listener)
    10. self._disposers.append(off)
    11. def dispose(self):
    12. # Dispose in reverse order to unwind dependencies correctly
    13. while self._disposers:
    14. self._disposers.pop()()

    In practice, three common issues require attention:

    1. Async tasks not canceled: Background polling or timers started by a plugin must be canceled during unload; otherwise, they continue running after the plugin is gone.
    2. Global state leakage: If a plugin directly modifies module-level variables, the unloader cannot identify those changes. Plugins should be required to produce side effects only through scope objects.
    3. Registration order dependency: If two plugins register tools with the same name, the later-loaded plugin overwrites the earlier one. The kernel should detect conflicts during registration and provide a clear error message.

    It is important to note that reversibility applies only to state inside the runtime framework. Emails already sent by a plugin and records already written to a remote database are side effects in the external world and cannot be undone by unloading the plugin. Design should distinguish these two types of side effects clearly; external side effects require compensation mechanisms at the business layer.

    4. Spatial Composability: Depend on Capabilities, Not Implementations

    Spatial composability concerns how plugin A should respond when service B, which A depends on, appears, disappears, or is replaced.

    The key here is to “depend on capabilities.” A tool declares only that it needs a “file read/write” capability and does not care whether the underlying implementation is local disk, a container directory, remote object storage, or an operating-system-level permission system. The same file search tool can work in different deployment environments by replacing only the file system plugin, while the tool code remains unchanged.

    When a capability changes, dependents need to react. There are three common strategies:

    • Availability switching: When a capability is missing, mark the tool as unavailable so the model cannot see it, avoiding invalid calls.
    • Late binding: Automatically complete binding when the capability arrives, and unbind when the capability is revoked.
    • Degraded execution: Use a fallback when the capability is missing, such as using a read-only view instead of write access.

    Illustrative code:

    1. class CodeSearchTool:
    2. requires = ["filesystem"]
    3. def __init__(self, runtime):
    4. self.runtime = runtime
    5. self.fs = None
    6. runtime.services.watch("filesystem", self.on_fs_change)
    7. def on_fs_change(self, provider):
    8. # provider is None means the capability has been unloaded
    9. self.fs = provider
    10. self.runtime.tools.set_available("code_search", provider is not None)
    11. def run(self, pattern):
    12. if self.fs is None:
    13. raise RuntimeError("filesystem capability unavailable")
    14. return self.fs.search(pattern)

    This style keeps responsibilities clear. The tool is responsible only for “what to search and how to present results,” while the file system plugin is responsible for “where to find it.” The two are connected through capability names, and replacing either side does not require modifying the other.

    5. Engineering Practices

    When applying these ideas to real projects, the following practices can be useful.

    1. Define stable contracts for capabilities. Once a capability interface is published, it should be versioned. Adding optional methods can be backward compatible, while changing the semantics of existing methods requires a major version upgrade. Contracts should include input/output types, error types, and concurrency conventions.

    2. Keep the kernel small enough. Once the kernel absorbs business logic, the benefits of pluginization quickly disappear. The test is simple: if a feature can be replaced, it should be a plugin.

    3. Declare permissions for each plugin. A plugin manifest should list the capabilities it requires, such as file reads, network access, or command execution. The kernel injects only declared capabilities into the plugin, which is a runtime expression of the principle of least privilege. Operations involving writes, execution, and external sending should be gated by an approval capability, and the approval policy itself should also be replaceable.

    4. Write unload tests. Unload reversibility can be verified with snapshot tests:

    1. before = kernel.snapshot() # Record registry, listeners, prompt sections, etc.
    2. plugin = load(plugin_module)
    3. unload(plugin)
    4. assert kernel.snapshot() == before

    Such tests should cover every registration behavior of each plugin. They can find missing disposal logic early and are far more reliable than manual inspection.

    5. Use mock implementations to isolate tests. Because models, file systems, and sandboxes are all plugins, tests can inject mock implementations without accessing real services. Reasoning loop logic can also be replay-tested using fixed model responses.

    6. Log side effects. The side-effect ledger serves not only unloading but also troubleshooting. Recording each plugin’s registration actions, version, and load time makes it possible to quickly locate the source when conflicts or abnormal behavior occur.

    6. Tradeoffs and Limitations

    A plugin-based architecture is not without costs. The following points should be clarified before choosing it.

    • Indirection brings understanding cost: A single tool call may pass through the event bus, interceptors, and capability proxies, so newcomers need more time to understand the execution path.
    • Cross-plugin debugging is harder: When two plugins interact incorrectly, the error may originate on either side, and structured logs and the side-effect ledger are needed to help locate it.
    • Order dependencies require explicit management: The execution order of interceptors directly affects the final prompt. Priority should be declared in the plugin manifest rather than relying on load order.
    • Event dispatch has performance overhead: When every tool call passes through multiple interception layers, latency accumulates, so the number of interceptors on critical paths should be controlled.
    • Version drift risk: When plugin and kernel versions do not match, capability contracts may change silently, so version compatibility should be validated at load time.

    Overall, pluginization is suitable for scenarios with many capability combinations, diverse deployment environments, and frequent implementation replacement. If the system is small and its capabilities are fixed, writing a clear reasoning loop directly is often more economical.

    7. Conclusion

    A microkernel-style agent runtime treats replacement as a first-class requirement. Temporal composability ensures that plugin unloading leaves no traces, while spatial composability ensures that dependents care only about capabilities rather than implementations. Together, they allow models, tools, sessions, sandboxes, and reasoning strategies to evolve independently. For developers, what really matters is understanding the kernel’s responsibility boundaries, how side effects are registered and revoked, and how to design capability contracts. These principles do not depend on a specific implementation and can be transferred directly to other agent system designs.