Designing an Agent Runtime Framework: From Startup to Plugin Extension
When AI reads code, modifies files, and runs tests, the model itself only handles the reasoning. A runtime framework is needed to prepare context, orchestrate tools, handle approvals, record the process, and decide whether a task should continue. This article reviews the design principles of such frameworks from a general engineering perspective, covering startup configuration, the plugin container, the agent loop, tool execution with approval, and session management.
In scenarios where AI reads code, modifies files, and runs tests, the model is only responsible for the reasoning. To deliver these capabilities to users reliably, you need an additional layer of software that prepares context, orchestrates tools, handles approvals, records the process, and decides whether a task should continue. This layer is commonly called the runtime framework. This article reviews the design principles of such frameworks from a general engineering perspective.
What the Model Doesn’t Provide
A model takes text in and produces text out. It cannot read files or execute commands on its own. A complete coding task involves at least the following steps:
- Prompt assembly: deciding which project information, message history, and rules go into the current request.
- Tool execution: turning the model’s call intents into real file reads/writes, command executions, or retrieval operations.
- Approval control: requiring user confirmation or policy-based allowance for risky operations such as deleting files or running external commands.
- Process recording: saving the inputs, outputs, and tool results of each turn for replay, debugging, and recovery.
- Termination judgment: determining whether the model’s output completes the goal or whether another turn is needed.
None of these belong to the model itself, yet they determine whether the system is reliable. The value of a runtime framework lies in organizing them into a maintainable program.
From Startup Configuration to the Plugin Runtime
The startup flow typically has three steps. First, the user selects a runtime mode—for example, a web application with a UI, a headless batch task, or an API service for other programs. Second, the launcher reads the configuration profile for that mode, which declares a set of feature packs. Third, the user can modify the existing configuration through patch files, including replacing a plugin, appending one, or adjusting parameters. After the overlays are applied, the final plugin list is produced.
The responsibilities of each layer can be summarized as follows:
| Concept | Responsibility | Examples |
|---|---|---|
| Runtime mode configuration | Declares which feature packs the mode uses | Web version, headless version, API version |
| Feature pack | Distributes a set of related plugins and their code | Model integration pack, tool pack, session pack |
| Patch | Adjusts existing configuration or inserts new configuration | Replacing the model provider, adding a logging plugin |
| Plugin container | Manages plugin loading, services, events, and disposal | A unified context object |
This layering lets the same base capabilities serve different entry points. All three modes can share model, tool, and session capabilities, with each mode only adding what its entry point requires. For example, the web version needs page-rendering plugins, the headless version needs input/output adaptation for one-shot tasks, and the API version needs external request handling.
Note that the order in which items appear in a configuration file is only for readability; it does not determine execution order. Plugin activation is driven by service availability: a plugin declares the services it needs, and it activates only after those services have been registered. When designing configurations, focus on dependency relationships rather than treating the plugin ordering as a startup sequence.
How the Plugin Container Connects Modules
Once the number of plugins grows, inter-module communication needs a unified mechanism. A common approach is for the container to pass a context object to each plugin, through which the plugin accesses other services. The following code is illustrative—it shows the calling pattern, not the real interface of any specific framework:
// Illustration: the agent loop plugin calls other services through the context objectexport function registerAgentLoop(context: PluginContext) {context.on('turn:start', async (session) => {const prompt = await context.prompt.build(session);const reply = await context.model.complete({prompt,tools: context.tools.list(),});// Subsequent handling of tool calls and termination checks});}
Two types of collaboration need to be distinguished.
Service calls get work done. The caller explicitly requests a capability—for example, asking the model to generate a reply or asking a tool to perform an operation. The caller cares about the returned result.
Events provide extension points. For example, events fire before a request is sent, before and after tool execution, or when a turn ends. A plugin can simply observe the process, such as recording latency; or, for events that support interception, it can modify decisions, such as rejecting a high-risk tool call.
Lifecycle binding is equally important. Tools, event listeners, and services registered by a plugin should be revoked when the plugin is unloaded. Otherwise, the runtime accumulates orphaned tool definitions, or events keep firing on released objects, leading to errors that are hard to diagnose.
The agent loop itself is also a plugin. Together with the model service, tool service, and session service, it forms the runtime. Swapping the loop strategy requires no changes to the model or tool implementations.
How the Agent Loop Drives a Task Forward
What the user sees as a single question often requires multiple model requests internally. This is because the model can only output one piece of text or one set of tool call intents per request. Tool execution results must be fed back into the context before the model can decide the next step.
A typical loop looks like this:
1. Read the session history and assemble the prompt for this turn2. Call the model and get a reply3. If the reply contains tool calls:a. Validate each tool call's argumentsb. Determine whether approval is required; if so, wait for the user's decisionc. Execute the tool and append the result to the sessiond. Go back to step 14. If the reply contains no tool calls:a. Determine whether the goal has been achievedb. If achieved, finish; if not and budget remains, append a hint and go back to step 15. When the maximum number of turns is exceeded or the user aborts,end the task and preserve the current state
Termination conditions generally fall into three categories: the model gives a final answer with no further tool calls needed; the turn or time budget is exhausted; or the user actively aborts, or an approval is rejected. Each case should be handled explicitly in the design. Otherwise, the loop may run indefinitely or exit early with incomplete results.
Tool Execution and Approval
Tools are the interface between the model and the external environment. The framework should treat every tool as an operational unit with input validation, a permission level, and a timeout limit.
In practice, tools can be classified by risk:
- Read-only operations, such as reading files or searching content, can usually execute automatically.
- Reversible write operations, such as creating or modifying files in the workspace, can be allowed automatically based on policy, with a copy of the pre-modification state preserved.
- Irreversible operations or those affecting external systems, such as deleting files, pushing code, or calling network services, should be confirmed by the user.
Tool output is also untrusted data. File contents or text returned from web pages may contain text disguised as instructions. The framework should hand such content to the model as data, rather than executing it directly as new operational commands. When concatenating command-line arguments, external inputs also need to be escaped to prevent command injection.
Session Recording and Context Management
The session service saves the messages, tool calls, and results of each turn. These records serve three purposes: resuming interrupted tasks, replaying the process to locate problems, and providing the context source for subsequent turns.
The context window is limited, so long tasks cannot retain the full history. Common practices include:
- Truncating or summarizing earlier tool outputs while keeping the calls and their conclusions;
- Always retaining the system rules and the current task goal;
- Injecting only the relevant fragments of large files instead of loading them in full.
Recording itself is also
