Plugin-Based Architecture: How to Assemble a Customizable AI Agent
This article explains how a plugin-based Agent harness can assemble different AI Agent products from a shared runtime. It covers declarative bundles and patches, replaceable agent loops and model adapters, model-visible logging, composable sub-agents, and a practical approach to selecting observability and context-management capabilities.
1. What Is a Plugin-Based Agent Harness?
When building an AI Agent product, an increasingly common engineering approach is to avoid hard-coding every capability into the main application. Instead, developers design a runtime skeleton, or harness, and progressively add capabilities through plugins to assemble a specific Agent product.
This type of harness is often implemented in TypeScript. Its core abstraction is a plugin tree. At startup, the root node in the configuration is rewritten as an empty list, meaning that no capabilities are enabled by default. The actual product is built by progressively applying capability bundles and user patches to this empty tree.
The value of this design is that the same runtime can produce completely different Agents. Capabilities become pluggable components rather than immutable blocks of hard-coded logic.
2. Layered Assembly: Bundles and Patches
A typical foundational bundle can inject an entire set of core components with only a small amount of code:
- Timers and scheduling
- An LLM integration layer, or LLM seam, with multiple model adapters
- Session management and local database queries
- The Agent itself and its agent loop
- Tool sets and system prompts
- Sandboxing and approval policies
- Skills, goals, and plan mode
- Context compression, sub-agents, workflows, and web search
Different environments can then assemble these capabilities differently. For example, a web-oriented bundle may first disable a group of tool components from the base bundle and then add its own implementations. This means the tools used by the web interface are not located in the host tree. Instead, they are mounted in the Agent preset created for each session.
This is the essence of a plugin-based architecture: enabling, disabling, replacing, and layering capabilities are all expressed declaratively rather than scattered across conditional branches in business logic.
3. Two Key Mechanisms
1. The Agent Loop Is a Factory
The Agent’s main loop is not a fixed procedure. It is a factory registered in an effect slot. When an Agent is created, the runtime requests a factory instance from that slot.
// Example: register the main loop as a factoryregisterEffect('agent-factory', () => createAgentLoop(config))// If the slot is empty, creation fails immediately// -> Error: no agent factory registered
If this registration is removed, the slot becomes empty and Agent creation fails immediately. This demonstrates that even the way an Agent reasons can be replaced as a plugin component.
2. Model-Visible Means Logged: Model-visible ⟺ logged
The logging design follows a simple constraint: everything visible to the model must be fully reconstructable from the logs. The runtime enforces this rule with assertions.
This has significant engineering benefits:
- During debugging, developers can precisely reconstruct the context the model saw for a particular call.
- Auditing and compliance gain a reliable source of truth.
- Any content visible to the model but missing from the logs is blocked by an assertion, preventing hidden behavior.
Logs are no longer merely a troubleshooting aid used after the fact. They become the single source of truth for Agent behavior.
4. Treating Agents as Components: Composable Sub-Agents
A more advanced design introduces sub-agent providers. The runtime can include multiple sub-agent providers, some of which use standardized protocols, such as process-based protocols or official Agent SDKs, to mount external Agents as components in its own plugin tree.
This is different from simply supporting configuration imports. In the former model, another complete Agent is treated as a callable component and incorporated into the same orchestration and logging system. The resulting tree can contain both internally developed loops and external capabilities wrapped as plugins, while using the same scheduling mechanism for all of them.
This type of architecture is often compared to a highly modifiable sandbox game. Its real flexibility does not come from having a large number of plugins. It comes from making the blocks, rules, and rendering data-driven and allowing changes at the core. Similarly, when the agent loop and model adapters become replaceable plugin components, one runtime can grow into Agents with entirely different forms.
5. What Should You Install? From Observability to Context
Once a pluggable foundation is available, capability selection becomes a matter of filling in the layers.
Layer 1: Observability — See What the Agent Is Doing
Agent behavior is dynamic and multi-turn. Without visualization, debugging becomes guesswork. The observability layer typically includes two types of capabilities:
- Context inspection: Provides cross-session insights, context panels, and dedicated commands. It can also display the runtime state of the current Agent, its parent, and each sub-agent in separate real-time cards. Since logs are the source of truth, these capabilities serve as a dashboard for the logs.
- Cost accounting: Tracks session costs, daily costs, budget limits, and remaining balances. It can also support pricing across different time periods, making token costs more controllable and predictable.
Layer 2: Context — Determine How Long a Session Can Run
Context growth is the biggest challenge in long-running sessions. The core function of the context layer is to compress conversation history into hierarchical summaries, preserving important information while reducing token usage.
When evaluating a compression strategy, two engineering metrics are more meaningful than simply asking how many times it reduces token usage:
- Prefix-cache hit rate: If compression frequently rewrites the beginning of the context, it can invalidate the cache and increase costs instead of reducing them. A better approach is to append summaries primarily at the end, preserving a high cache hit rate.
- Compression overhead: Compression also requires model calls. If its cost becomes too high, the optimization is counterproductive. This overhead should therefore remain a small fraction of the overall cost.
6. Conclusion and Selection Recommendations
The essence of a plugin-based harness is to decompose an Agent product into an empty tree, composable capabilities, and replaceable core components. This provides three major engineering benefits:
- Composability: Capabilities are layered declaratively through bundles and patches, allowing different environments to reuse the same runtime.
- Replaceability: Even the main loop and model adapters are plugins, so core behavior can be replaced as a whole.
- Observability: The rule that model-visible content must be reconstructable from logs provides a foundation for transparent and auditable behavior.
In practice, start by completing the observability layer so that behavior and costs are visible. Then strengthen the context layer to extend the useful lifetime of sessions. Finally, introduce composable sub-agents when you need to expand the boundaries of the tasks the system can handle.
In enterprise environments, these capabilities can also connect with general-purpose object storage, logging, monitoring, and alerting infrastructure. Cloud platforms, including the general-purpose capabilities offered by Baidu AI Cloud, can support the persistence and observability needed to operate a self-built Agent runtime more reliably and with greater control.
