Designing a Pluggable Agent Runtime

    A practical guide to why an agent needs a runtime separate from the model, what that runtime should own, and how plugin composition, append-only session logs, and explicit loop policies keep execution auditable, recoverable, and replaceable without rewriting the core.

    Why a Separate Runtime Is Needed

    Many teams treat an agent as “call a model API, then run a few tools in a fixed order.” That approach is fast for a demo. Once multi-turn conversation, long-running tasks, human confirmation, and failure recovery enter the picture, the problems arrive together: session state is scattered across memory and logs, a failed tool cannot be retried from a consistent state, permission checks are hard-coded in the main loop, and swapping a model or a tool protocol means changing core code.

    A more stable split is this: the model proposes the next intent, and the runtime turns that intent into auditable execution. As an engineering formula, a deliverable agent is the model plus the runtime. The model is the source of power. The runtime is the transmission, steering, and brakes: it decides how requests are assembled, how tools are called, how context is persisted, and how unauthorized actions are stopped.

    What the Runtime Should Own

    A usable agent runtime should reliably cover at least four responsibilities.

    1. Model access. Normalize message formats, trim the context window, and handle retries and timeouts, hiding differences in streaming output and tool-call protocols across models.
    2. Tool execution. Register each tool’s name, parameter schema, timeout, and idempotency. Validate before and after execution, and write results back into the session.
    3. Session persistence. Store user input, model output, tool requests, and tool results so work can continue after a process restart.
    4. Permissions and policy. Allow, deny, or escalate to human confirmation based on identity, environment, and tool risk.

    If these four concerns are coupled inside one irreplaceable main program, every later change becomes a core patch. A better approach is for the core to provide only lifecycle and composition, with concrete capabilities attached as plugins.

    No Privileged Core

    “Everything is a plugin” is not a slogan. It is a constraint on dependency direction. The model adapter is a plugin. The tool registry is a plugin. Session storage is a plugin. Even the loop that drives “think, act, observe” can be a plugin. The core does not implement business policy. It only ensures that plugins start according to their declared dependencies and stop according to their lifecycle.

    Extensions then do not require forking the main repository. To change storage, replace the session plugin. To turn the default loop into “plan, then execute,” attach a different loop implementation. The core stays stable. Change happens at the boundary.

    A practical composition usually depends on three primitives.

    • Dependency declaration. A plugin explicitly declares the services it needs, such as a tool registry or session storage. The framework loads the plugin only after those dependencies are ready. When a dependency is unloaded, dependents stop automatically, so nothing calls into a dangling service.
    • Registration binds to a lifecycle. Handlers, tools, and subscriptions that a plugin registers with the runtime must share that plugin’s lifecycle. On unload, roll them back in reverse order of registration so a hot update does not leave old callbacks behind.
    • Declarative assembly. Configuration describes which plugins are enabled, their parameters, and what they replace. Switching an implementation means changing configuration, not hard-coding branches in code.

    The structure can stay simple:

    1. runtime:
    2. plugins:
    3. - id: model.default
    4. use: model-adapter
    5. - id: tools.registry
    6. use: tool-registry
    7. - id: session.log
    8. use: append-log
    9. - id: loop.default
    10. use: reason-act
    11. inject: [model.default, tools.registry, session.log]

    Configuration expresses composition only. It should not carry secrets or business data. Secrets, network endpoints, and permission policies should come from separate configuration and secret management, and should be validated at load time.

    The Session Log Is Reconstructable Truth

    The place an agent most easily loses fidelity is “what the model actually saw.” If context is stitched together from temporary caches, debugging cannot answer which tool result was truncated, which system prompt was overwritten, or whether a failed retry ran a side-effecting tool twice.

    A more reliable rule is: every input that reaches the model must be reconstructable from a single append-only event stream. Events should at least include session start, user messages, model requests, model responses, tool calls, tool results, permission decisions, and errors. Storage only appends. History is not edited in place. Corrections are made by appending compensating events.

    That yields three benefits.

    • Replay. Rebuild context from the same event prefix and reproduce a specific model request for regression testing.
    • Recovery. After a crash, continue from the last committed event instead of relying on a half-built in-memory state.
    • Audit. Permission denials, human confirmations, and tool side effects have an order that meets internal traceability requirements.

    Keep the event schema small and stable. Do not put large objects such as images or long files directly in the event body. Store a reference and a summary, and keep the body in object storage. Fetch it again under permission checks when reconstructing. Log volume stays bounded, and sensitive originals are not copied endlessly into every event.

    The Loop Is a Policy

    The default loop is usually: assemble context, call the model, and if a tool call appears, execute it and write the result back, until the model produces a final answer or a step limit is reached. Once that loop is a plugin, different tasks can use different policies: a single-turn loop for short Q&A, plan-execute-verify for code changes, and a human-confirmation node for high-risk actions.

    A loop plugin should not bypass the tool registry and the permission plugin to issue commands directly. Every external action follows the same chain: validate parameters, decide permission, execute, and record the result. Replacing the loop then does not open a side path.

    Step limits, timeouts, repeated tool calls, and cost budgets should be explicit stop conditions of the loop. Without those guardrails, uncertain model output becomes an uncontrolled storm of tool calls.

    Hot Updates and Rollback

    The value of declarative composition is a small change surface. When configuration supports hot reload, the flow should be: validate the new configuration, build a new plugin tree, switch traffic once dependencies are satisfied, then unload the old tree and roll back its registrations. If any step fails, the old tree should keep serving. The system should not stop in a half-switched state.

    Hot updates fit stateless adapters and policy plugins. They do not fit changing the event format without a data migration. Version the event schema: new plugins must be able to read old events, and writes should carry the schema version. When compatibility is impossible, dual-write or migrate offline first, then switch the read path.

    A Checklist for Implementation

    When designing or reviewing an agent runtime, these questions are more useful than a feature list.

    • Can the model be swapped by changing only the adapter, without changing tool or session code?
    • Can a new tool be added without modifying the main loop?
    • Can any model request be rebuilt from the event stream alone?
    • Does a permission denial leave an event, rather than only a log line?
    • After a plugin is unloaded, are callbacks, timers, and connections all released?
    • Do side-effecting tools declare an idempotency key so retries do not double-charge, resend messages, or rewrite data?
    • Does a bad configuration block the switch, instead of starting with a broken config?

    Common Mistakes

    Turning the runtime into another giant framework, and putting all logic in one script, are two ends of the same problem. Boundaries that are too fine make one request cross dozens of indirect calls, raising latency and debugging cost. Boundaries that are too coarse return you to an irreplaceable core. A practical grain is to split by reason for change: model protocol, tool protocol, storage, permissions, and loop policy stay independent, while code that changes for the same reason stays in one plugin.

    Another mistake is persisting only the final answer. Intermediate tool arguments and failed attempts are part of the truth. Without them, model hallucination cannot be distinguished from tool failure, and retries cannot be made safely.

    Finally, do not read “replaceable” as “no default implementation.” A default model adapter, local tool execution, and a file-based event log are still necessary, or the system cannot start. Plugin design guarantees the cost of replacement. It does not leave the default path empty.

    Summary

    The long-term cost of an agent system comes mainly from how context is formed, how actions are allowed, and how failure returns to a consistent state. Put those duties in a runtime with no privileged business logic. Assemble plugins through dependency declarations, lifecycle binding, and declarative configuration. Use an append-only event stream so that whatever the model could see can be rebuilt. Teams can then change models, tools, and task policies without repeatedly rewriting the kernel. That is closer to an operable engineering system than wrapping another layer of API calls.