Agent Runtimes: From Hard-Coded Loops to Composable Kernels

    An agent runtime, or harness, sits between a language model and its environment, governing tool access, state transitions, failure recovery, and policy enforcement. This article explains why the conventional thought–tool–result loop eventually becomes restrictive, and presents a composable runtime built around plugin lifecycles, structural consistency, controlled reflection, policy enforcement, replayable state, and a clear separation between the runtime kernel and the client interface.

    A language model is like a “horse” that provides the power, while the interactive client is more like the carriage: it manages sessions, editing, and result presentation. The component that actually sits between the model and its environment—and determines what the model can call, how state moves, and how failures are handled—is the agent runtime, often called a harness.

    The runtime is similar to a hardware abstraction layer in an operating system. It exposes stable capabilities upward while adapting downward to files, commands, retrieval systems, messages, and external services. Delivering only an installable client lets users get started immediately. But when the runtime is welded to the interface, replacing the model, inserting enterprise policies, or introducing runtime governance becomes difficult.

    Why a Hard-Coded Single Loop Works at First—and Then Stops Working

    Most current agents use a similar architecture. At startup, the system provides a long prompt that defines the agent’s role and constraints, along with structured descriptions of tools such as file access, file writing, command execution, and search. After reasoning, the model produces a tool call. The local host executes it, converts the result into text, appends that text to the context, and starts the next round.

    This “think, call, append” loop is simple and understandable. It is also sufficient for many coding and office-automation tasks.

    The problem is that all logic becomes embedded in the same loop. The tool list is fixed at startup, while prompts and policies are mixed into one block of text. Procedural host code connects permissions, retries, truncation, and logging. As capabilities grow, prompts become bloated and tool descriptions begin to conflict. Adding an approval step or changing the memory mechanism requires modifying the host’s main loop.

    The model cannot see how plugins are loaded, nor can it adjust its capability boundaries under controlled conditions. As a result, every client develops its own self-contained runtime. The interfaces may look different, but the underlying kernels remain rigid.

    Three Things the Runtime Should Actually Abstract

    The goal is not to add an even longer prompt. It is to split the loop into replaceable mechanisms.

    1. Composability Across Space and Time

    In space, capabilities should be assembled as plugins. File systems, command execution, retrieval, ticketing, and approvals should remain independent and be enabled or disabled for a particular task.

    In time, plugins should have lifecycle states and transitions, including loading, activation, suspension, unloading, and failure cleanup. A plugin should not remain available merely because its process is still running. The composition of capabilities should be able to change during a session rather than only through a new software release.

    2. Structural Consistency

    The data used to describe plugins should be represented in the same serializable structure that the runtime actually executes. If the model or control plane reads which plugins are available, what they depend on, and what state they are in, it should be reading the same tree held in runtime memory—not a separate description that can become stale.

    This structural consistency enables auditing, replay, and migration. Persisting the plugin tree should allow another execution node to restore it with the same semantics.

    3. Controlled Reflection

    The model should be able to query its own runtime: which tools are available, how much quota remains, and which policy rejected the previous call. In an explicitly authorized creation or adjustment mode, it may also propose adding or removing plugins or changing routing.

    However, every proposal must pass through a policy engine. The model must not modify runtime memory directly. Reflection addresses the problem of the model not knowing which tools it has been given; policy addresses the problem of the model granting itself access that it should not have.

    A Lifecycle Microkernel for Plugins

    The runtime can be designed as a small kernel with four responsibilities:

    • Scheduling turns
    • Managing plugin lifecycles
    • Passing messages
    • Enforcing policies

    Business capabilities remain outside the kernel as plugins.

    At minimum, a plugin should declare an identifier, version, dependencies, permissions, and lifecycle hooks. During loading, the runtime validates configuration and dependencies. During activation, the plugin registers tool schemas and event subscriptions. Before every call, the kernel performs authorization, quota checks, and parameter validation. After the call, it records an audit event and decides whether the result should be added to the model context.

    During unloading, the plugin releases handles, cancels subscriptions, and removes temporary files. Failures must have explicit semantics: whether the operation can be retried, requires human confirmation, or should immediately trip a circuit breaker for that plugin.

    The structure does not need to be tied to a particular programming language:

    1. plugin:
    2. id: repo-reader
    3. permissions: [read_workspace]
    4. hooks: [on_load, on_call, on_unload]
    5. kernel.loop:
    6. event = schedule(session)
    7. decision = policy.check(event, plugin_tree)
    8. if decision.allow:
    9. result = plugin.invoke(event)
    10. context.append(redact(result))
    11. else:
    12. context.append(decision.reason)

    The kernel does not know how to read a repository. It only knows how to invoke a plugin according to a contract. The plugin does not know when a turn ends; it only handles its own request. The two communicate through events and structured results, so the main loop no longer becomes a large function filled with business-specific branches.

    Self-Modification Must Stay Within Boundaries

    Runtime self-modification is easily misunderstood as allowing the model to change itself freely. In engineering terms, it should be limited to submitting patches to the plugin tree. For example, the model might propose enabling read-only retrieval, disabling high-risk command execution, or routing long output through a summarization plugin.

    The kernel must validate whether the patch satisfies dependency requirements, whether its permissions exceed the session token, and whether it touches an immutable plugin. Identity, auditing, and billing probes, for example, are typically not removable. Once approved, the runtime creates a new version of the plugin tree while retaining the previous version until the current turn ends, enabling rollback.

    Under this design, creation mode produces a versioned configuration change rather than arbitrary code execution. High-risk actions still require human confirmation or two-person approval.

    If an organization already has unified identity, secret management, and audit logging, plugins should consume those shared capabilities rather than creating another account system inside the runtime. Object storage can hold plugin packages and session snapshots, message queues can deliver asynchronous tool results, and logs and monitoring can track latency, rejection rates, and tool error rates. These are general infrastructure capabilities and should not be tied to a particular interface.

    Dividing Responsibilities Between the Runtime and the Client

    The client is responsible for the user experience:

    • Streaming output
    • Diff views
    • Multiple sessions
    • Shortcut actions

    The runtime is responsible for correctness:

    • Tool contracts
    • Permissions
    • Retries
    • Context trimming
    • Plugin lifecycles
    • Replay

    The two should communicate through a stable protocol containing session identifiers, events, tool calls, and policy decisions. The protocol should not expose interface-specific state.

    Both official and third-party teams should be able to build text or graphical clients. Even without a user interface, the same runtime should be callable by a headless task scheduler. Building the kernel first and the shell later prevents every new interface from duplicating the agent loop.

    Existing clients with embedded runtimes are often more complete because they have evolved through real user feedback. The value of creating a separate runtime is not that it will immediately be easier to use. Its value is that it separates the loop from the product shell, allowing policies, plugins, and models to evolve independently.

    In the short term, such a runtime may resemble an early operating-system kernel: limited capabilities, a small ecosystem, and incomplete documentation. Its value depends on whether the plugin contract is stable, lifecycle behavior is testable, and reflection is auditable. Merely moving the same prompts and tool list into another repository does not create a new abstraction.

    Common Implementation Gaps

    Context Remains a Scarce Resource

    Plugin results should be tiered. By default, the model should receive summaries and key fields, while the original content is stored for traceability and retrieved only when needed. Otherwise, adding more plugins will make each turn slower and increase the chance of writing secrets or personal information into the conversation.

    Permissions Should Default to Deny

    Reading the workspace, writing to the workspace, executing commands, and accessing the external network are four different capabilities. They should not all be granted simply because the task involves software development.

    Command execution should also restrict the working directory, environment variables, and timeout. Standard output and standard error should be truncated separately.

    State Must Be Replayable

    In addition to model-visible text, the runtime should record the plugin-tree version, policy decisions, and hashes of raw tool responses. During troubleshooting, engineers should replay events rather than infer the runtime state from a chat transcript and guess which plugins were active.

    Testing Should Be Layered

    Plugins should use contract tests to validate inputs, outputs, and error codes. The kernel should use scenario tests to verify loading order, hot updates, and rollback. Policy tests should use case tables covering rejection, degradation, and confirmation.

    Do not rely only on manual conversations to decide whether the system “feels smarter.”

    Keep Observability Metrics Simple

    Useful metrics include:

    • Turn latency
    • Tool success rate
    • Policy rejection rate
    • Context utilization
    • Number of external calls per task

    Without these measurements, it is impossible to determine whether the model is underperforming or the runtime is retrying without limits.

    A Practical Evolution Path

    Start by extracting tool calls from the existing loop as explicit events, with unified error codes and audit fields.

    Next, separate role definitions, tool descriptions, and security rules from the prompt. Move rules into a policy engine, leaving the prompt responsible only for task semantics.

    Then introduce a plugin manifest and lifecycle management so that adding a capability no longer requires changing the main loop.

    Only after that should controlled reflection and creation mode be enabled, with high-risk self-modification disabled by default.

    The point of this sequence is that composability is not a slogan. It means replacing a welded while loop with a versioned plugin tree. The interface can come later, or different teams can implement different interfaces. The kernel’s contracts, permissions, and replay mechanisms must be established first.

    When models, plugins, and policies can be replaced independently, an agent can evolve from a demonstration loop into a runtime suitable for production.