Agent Runtime Architecture: A Plugin-Based Design and Implementation Guide

    This article explains why AI agents need a runtime rather than a fixed set of features, and how a plugin-based architecture separates variability from the core. It covers the core design principle that everything is a plugin, the three-layer lifecycle of Turn, Step, and Tool, implementation details for key plugins such as model adapters, context management, tool registries, and session storage, plus governance, observability, and common anti-patterns. Readers will learn how to build agent infrastructure that is easy to assemble, audit, recover, and evolve.

    1. Background: Why Agents Need a Runtime, Not a Feature Set

    In agent engineering, teams often focus on prompts and the number of tools. However, what really determines whether a system can evolve over the long term is the engineering layer outside the model: how context is organized, how tools are scheduled, how files and terminals are executed under control, how tasks are decomposed, how the system recovers after failures, and how sessions are recorded. The industry has gradually converged on a shared formula:

    1. Model + Harness = Agent

    The model is responsible for “thinking,” meaning reasoning and generation. The Harness is responsible for “doing,” meaning turning a user task into executable, auditable, and recoverable system behavior. If these capabilities are hard-coded into a fixed kernel, every new requirement requires changes to core code, and the system eventually evolves into a monolith that is hard to maintain. The value of a plugin-based runtime is precisely to separate variability from the core.

    2. Core Design Principle: Everything Is a Plugin, and the Core Only Schedules Lifecycles

    A typical modern design keeps only a plugin lifecycle scheduling foundation at the bottom layer, while all other components are provided as plugins. The agent loop, model adapters, session storage, tool registry, and UI layer are no longer privileged core components; they are native parts of the system.

    This is fundamentally different from the “fixed kernel plus peripheral extensions” model. In the latter, plugins are “tools within tools” and can only be mounted around the core. In the former, plugins are first-class citizens of the framework and can replace or even remove the default implementations. The criteria are simple:

    • Can you replace the main agent loop without modifying the foundation?
    • Can you replace session storage without affecting tool registration?
    • Can you assemble or unload capabilities without restarting the process?

    3. Lifecycle Layers: Turn → Step → Tool

    Composability depends on precise enough extension points. Splitting a conversation into three layers of state is a common and effective approach:

    1. Turn: A user task claimed by the agent. It is the business boundary and the billing/quota boundary.
    2. Step: A model request and the messages or tool calls it produces. It is the context and prompt boundary.
    3. Tool: A structured capability execution, including pre-call, in-execution, and post-call processing chains.

    A typical event flow looks like this:

    1. turn/start
    2. -> agent/pre-step
    3. -> step/start
    4. -> assemble system prompt and tool schema
    5. -> llm/stream
    6. -> assistant/message
    7. -> tool/call
    8. -> tools/pre-execute (guards / approval / sandbox)
    9. -> tools/execute
    10. -> tools/post-execute
    11. -> tool/result
    12. -> step/end
    13. -> turn/end

    The direct benefit of layering is that extension points are clear: context compression can intervene before the model request; telemetry can subscribe to streaming events; approval can suspend execution before a tool runs; and result post-processing can standardize formatting and redact sensitive data before writing results back to the model.

    4. Implementation Details of Key Plugins

    4.1 Model Adapters

    Model adapters hide protocol differences across inference services and unify streaming events, tool call structures, and error semantics. An adapter should explicitly distinguish retryable errors from non-retryable errors to prevent the upper-level loop from performing meaningless retries.

    4.2 Context Management

    Context management includes system prompt assembly, history trimming, summary compression, and retrieval injection. It is recommended to control budgets at Step granularity:

    1. budget = model_window - reserve_output - reserve_tools

    When the budget is exceeded, compress the oldest tool outputs first rather than truncating user instructions.

    4.3 Tool Registry and Execution Chain

    A tool description is the schema visible to the model. It must ensure unique naming, parameter validation, and idempotency identifiers. The pre-execution chain should include at least parameter validation, permission checks, human approval, and sandbox isolation. The post-execution chain is responsible for result normalization, truncation, redaction, and audit persistence.

    4.4 Session Storage

    Use Turn as the persistence unit and Step as the incremental unit to support resume-from-breakpoint and replay. The storage plugin should support read-only replay for problem reproduction and regression evaluation.

    5. Governance and Observability

    Plugin-based does not mean unrestricted. The foundation needs to provide unified contracts: plugins declare dependencies, capability tags, priorities, and failure policies; the scheduler handles topological sorting and conflict detection. Observability should cover three types of signals: structured logs (who changed the context and when), metrics (number of Steps per Turn, tool failure rate, token budget usage), and traces (the complete path of a tool call). In cloud deployments, you can combine common capabilities such as logging services, monitoring and alerting, object storage, and message queues to achieve audit retention and asynchronous task decoupling.

    6. Common Anti-Patterns

    • Plugins directly call each other, forming implicit strong coupling.
    • Plugins secretly modify global state internally, breaking replayability.
    • Approval logic is scattered across multiple plugins, leading to inconsistent policies.
    • Context compression has no budget constraints, causing silent information loss.

    7. Conclusion

    To evaluate an agent runtime, you should not look at how many features it ships with, but whether it hands “change” to plugins and keeps “order” in the foundation. With the three-layer lifecycle as the skeleton, clear contracts to constrain plugins, and observability as a safety net, you can build agent infrastructure that supports rapid business assembly while remaining evolvable and governable over the long term.