AI Coding Agent Architecture Layers and Code Size Evaluation
This article examines why removing most responsibilities from an AI coding agent kernel rarely reduces total code size as much as expected. It explains how inconsistent counting boundaries distort line-of-code comparisons, which responsibilities must stay inside the kernel, and which can be safely externalized. Readers will find a layered evaluation approach based on change locality, dependency depth, session replayability, fault localization cost, and test coverage rather than raw LoC.
1. The Starting Point: Does Hollowing Out the Kernel Make It Lighter?
A common claim in discussions of AI coding tool architecture is that a more aggressive design removes most responsibilities from the traditional agent kernel, leaving only a replaceable “empty socket” whose capabilities come from external plugins and the host framework. On the surface, this suggests a sharp drop in code volume and a large reduction in maintenance cost.
The conclusion usually does not hold once the counting boundaries are aligned. The term “kernel” has no universally agreed boundary: some include the reasoning loop, others place it in a plugin; some count the model access layer as part of the kernel, others put it in a base library. Different boundaries naturally produce numbers that cannot be compared.
2. Why Code Line Counts Can Look Smaller
Understanding code size differences starts with understanding what a single count actually mixes together.
- Migrating a responsibility is not the same as removing it. Moving loop scheduling, model invocation, and streaming transport to an outer layer does not reduce the total; it only changes the directory.
- Host framework code is usually excluded. If a design depends on a host runtime, the lifecycle management, dependency injection, and event bus it provides count as “external dependencies” and are naturally left out of the count.
- Generated code and configuration files are handled inconsistently. Whether interface definitions, protocol files, templates, and configuration are included can change the result by several times.
- Comments, blank lines, and test code create large errors when no common convention exists.
A more rigorous approach is therefore to define the counting boundary first and then compare orders of magnitude, instead of directly comparing two totals.
3. Order-of-Magnitude Observations After Aligning Boundaries
With non-core responsibilities stripped away and only a minimal skeleton such as “host integration plus session recording” retained, the code volume typically remains in the tens of thousands of lines. A small kernel that keeps the reasoning loop cohesive may be only a few thousand lines. This shows that the benefit of “aggressive stripping” is far smaller than intuition suggests.
Once model access and streaming, coding-specific capabilities, and terminal interaction interfaces are counted as well, the total quickly expands to the hundreds of thousands of lines. What really determines complexity is not the size of the kernel but the width of the surrounding capability surface.
An illustrative counting script can make the boundary explicit:
# Only illustrates the counting boundary; not a specific product implementationINCLUDE_DIRS = ["core", "session", "runtime"]EXCLUDE_PATTERNS = ["*_test.*", "*.gen.*", "vendor/*"]def count_loc(paths):total = 0for p in paths:if any(p.endswith(s.lstrip("*")) for s in EXCLUDE_PATTERNS):continuewith open(p, encoding="utf-8") as f:total += sum(1 for line in f if line.strip() and not line.startswith("//"))return total
4. The Minimal Kernel Responsibility Set and the Replaceable Boundary
Regardless of the style adopted, the responsibilities the kernel must stably carry are roughly fixed: session state and context organization, termination conditions for the reasoning loop, orchestration of tool calls and error retries, ordering guarantees for streaming output, and observability instrumentation. These responsibilities are hard to externalize because they determine system correctness and debuggability.
What can usually be externalized safely: adapters for specific model providers, the form of the interface presentation, and the extension plugin ecosystem. Externalizing replaceable items is reasonable decoupling; externalizing correctness-related items turns complexity into cross-module state consistency problems.
5. A Layered Evaluation Method: From LoC to Maintainability Metrics
Use multiple metrics instead of a single line count:
- Change locality: how many modules must change to add a model adapter.
- Dependency depth: whether the core layer depends in reverse on the interface or plugin layer.
- Session replayability: whether a given session record can reliably reproduce a run.
- Fault localization cost: whether a streaming interruption can be located within a single log path.
- Test coverage structure: whether the core loop has tests that do not depend on external services.
An illustrative dependency constraint (one-way dependencies):
interface / plugin -> application -> kernel -> infra
6. Engineering Recommendations
- Fix the counting boundary in a document first, then discuss “which one is lighter.”
- Abstract model access and streaming into a protocol layer so the kernel stays unaware of specific implementations.
- Use an appendable, replayable log structure for session records to make problem reproduction easier.
- If running on a container platform or cloud host, use general-purpose logging services and monitoring and alerting capabilities to collect runtime metrics, and evaluate architecture evolution with data rather than intuition.
- Treat the “hundreds of thousands of lines” as the cost of the capability surface, not a defect; what matters is whether that code is clearly layered and test-isolated.
7. Conclusion
“Killing the kernel” is more an expression of preference than a quantifiable optimization result. After aligning boundaries, a minimal skeleton and a cohesive kernel are in the same order of magnitude; the real size difference comes from surrounding capabilities. When evaluating architecture, use clarity of responsibility boundaries, dependency direction, replayability, and fault localization cost as the criteria, with code line count serving only as a secondary reference.
