Agent Runtime Framework Design: State Layers, Lifecycle, and Plugin Architecture
This article breaks down the design of an Agent runtime framework, explaining why a runtime layer is needed beyond the model itself. It introduces the three-layer state model of Turn, Step, and Tool, walks through a typical lifecycle chain, and shows how an event-driven plugin mechanism enables context governance, safety guardrails, human approval, and observability. It closes with key engineering practices for building a controllable, production-grade agent system.
1. Why a Runtime Layer Is Needed
In discussions about AI agents, a common misconception is to place all capability inside the model itself. Real engineering practice repeatedly shows that the model only solves the problem of “thinking”—reasoning, planning, and content generation. The problem of “doing”—how context is organized, how tools are registered and scheduled, how files are read and written, how terminal commands are executed, how long tasks are decomposed, how failures are recovered, and how sessions are recorded—belongs entirely to engineering systems outside the model. Abstracting these capabilities together forms the Agent runtime.
It can be summarized with a formula:
Model + Harness = Agent
Here, the Harness (the runtime shell) takes on all responsibilities except reasoning. Its value lies in three points. First, decoupling: the model can be replaced, downgraded, or rolled out gradually while the upper-layer business logic stays unchanged. Second, controllability: all side-effect actions go through a unified channel, making them easy to audit and intercept. Third, composability: the runtime is not a finished application with fixed features but a foundation. The officially preset “intelligent coding” is just one combination; developers can use the same foundation to assemble agents unrelated to coding, such as operations inspection, data analysis, or customer service ticket handling.
2. Three-Layer State Model: Turn, Step, Tool
To make the runtime controllable, the first step is to slice a seemingly continuous conversation into semantically clear state units. A relatively mature approach in the industry is a three-layer division:
- Turn: a user task that is claimed and executed by the agent. It is the smallest closed loop from a business perspective and the natural boundary for billing, timeouts, and quota accounting.
- Step: a single model request and its result, which may be a text message or several tool calls. A Turn usually contains multiple Steps.
- Tool: a single structured capability execution that must be split into a three-stage processing chain: pre-execute, in-execute, and post-execute.
The significance of this division is that each layer has clear start and end semantics, so governance logic can be attached precisely. The Turn layer cares about task boundaries and resource quotas; the Step layer cares about the context window and compression strategy; the Tool layer cares about permissions, sandboxing, and result normalization.
3. Typical Lifecycle Chain
A fully observed chain looks roughly like this (illustrative):
turn/start-> agent/pre-step-> step/start-> assemble system prompt and tool schema-> llm/stream-> assistant/message-> tool/call-> tools/pre-execute (guards / approval / sandbox)-> tools/execute-> tools/post-execute-> tool/result-> step/end-> turn/end
This chain explains several key engineering facts. First, the prompt and tool schemas are assembled dynamically before the request is sent, which means the context strategy can intervene at the “last moment” without polluting business code. Second, model output is streamed, so telemetry and content moderation can be processed while being received. Third, tool execution is wrapped before and after, giving any high-risk action a chance to be paused or rejected before it runs.
4. Event-Driven Plugin Mechanism
The runtime emits events at every key node of the agent loop. Plugins insert their own logic by listening for the events they care about, without modifying the loop source code. An abstract example:
ctx.on('tools/pre-execute', async (event, next) => {if (event.tool === 'shell' && event.args.risky) {return event.reject('Manual approval required');}await next(); // allow, proceed to the execution stage});ctx.on('step/start', async (event, next) => {event.context = await compress(event.context); // context compressionawait next();});
ctx.on() handles subscription and next() handles allowing or intercepting. This pair of semantics forms the basis of the entire extension system. From it, four general categories of plugins naturally emerge:
- Context governance: trim, summarize, and apply retrieval augmentation before the model request to control window usage and cost.
- Safety guardrails: validate parameters, identify sensitive information, block dangerous commands, and isolate in a sandbox before execution.
- Human approval: suspend high-risk tool calls as pending items and continue only after confirmation, enabling human-machine collaboration.
- Observability: turn streaming events into logs, metrics, and traces, and connect them to logging services and monitoring/alerting systems.
5. Key Engineering Practices
- Unified tool protocol: declare all capabilities with a structured schema and enforce strong parameter validation to avoid the unpredictability of free text.
- Idempotency and recovery: tool execution should support idempotency keys and timeout retries, and recover from the most recent stable Step on failure rather than restarting the whole Turn.
- State persistence: persist the intermediate state of Turns and Steps to object storage or a database so sessions can be replayed and audited.
- Least privilege: deny by default, authorize at tool and parameter granularity, and use container platforms for runtime isolation.
- Evaluation loop: use a fixed task set to regression-test prompts, tool descriptions, and compression strategies, and manage “prompt changes” as code changes.
6. Conclusion
When evaluating this kind of Agent runtime framework, the number of preset features is not what matters. What matters are three things: whether the state layering is clear, whether the extension points are semantically well-defined, and whether side effects are governable. Only when the model, context, tools, and approval are split into replaceable parts does an agent move from a demo to an operable production system. If an enterprise wants to build quickly, it can assemble its own runtime on general cloud capabilities such as container platforms, object storage, logging services, and monitoring/alerting, keeping the foundation neutral and capabilities pluggable.
