Understanding the Inner Workings of AI Coding Agents: System Prompt and Context Management
This article provides a technical deep dive into the internal mechanisms of LLM-based command-line coding agents, focusing on two critical components: how system prompts are dynamically assembled rather than statically defined, and how context management handles window exhaustion through compression and persistent attachment systems. It also examines sub-agent architectures, MCP-based tool integration, and offers engineering principles for teams building their own agent frameworks—covering prompt engineering, permission control, and context lifecycle management to ensure stable, auditable, and extensible behavior.
Why Studying Coding Agent Internals Matters
LLM-based command-line coding agents have become a regular part of many developers’ daily workflows. On the surface, these tools appear simple—you chat in a terminal, modify code, and run commands—but beneath that simplicity lies a complex implementation involving prompt engineering, context management, permission control, process scheduling, and other engineering challenges. Understanding these internal mechanisms helps developers use such tools more effectively and provides a practical blueprint for building custom agent frameworks.
While most open-source agent scaffolds share a similar high-level architecture, the level of detail in how individual modules are handled varies significantly. The following sections examine several core modules in depth.
System Prompt: From Static Text to Dynamic Assembly
Limitations of the Traditional Approach
Most AI coding tools use a static system prompt—a fixed block of text injected when a session starts and never changed throughout its lifetime. This approach has a fundamental problem: the prompt content is disconnected from runtime state. For example, when a tool is disabled, its description still consumes context tokens; when an external service connection changes, the associated instructions cannot be updated synchronously.
Dynamic Assembly Mechanism
More refined implementations build the system prompt dynamically at runtime. When a session starts, a dedicated construction function generates the prompt on the fly, with the final content determined by the current environment, the set of enabled tools, the status of external protocol connections, and user configuration overrides. The prompt typically consists of the following components:
1. Default Behavior Contract. This base prompt defines the model’s operational guidelines:
- Tool-First Principle: Explicitly instructs the model to prefer dedicated tools (file readers, content search, pattern matching) over generic shell commands. The rationale is that dedicated tools offer finer-grained permission controls and more structured result formats that can be programmatically validated.
- Output Style Constraints: Requires concise, direct responses with no redundant expressions, unnecessary confirmations, or echo of user input—essentially reducing noise tokens to increase information density.
- Memory Mechanism Description: Explains how the model automatically discovers project memory files (searching from user-level global config to project root to subdirectories) and the write conventions, enabling cross-session memory.
- Git Safety Protocol: For dangerous operations like force-push or hard reset, explicit user authorization must be obtained before execution—this represents permission enforcement at the prompt layer.
- Skill Invocation Description: Documents slash-command usage so the model understands when to trigger predefined skills.
2. Runtime Injected Content. Beyond the base prompt, additional information is assembled on the fly when the session starts:
- Tool Descriptions: Iterates through all enabled tools’ description methods and concatenates them dynamically. When a tool is disabled, its description disappears from the prompt, keeping the context aligned with the actual capability set.
- External Service Instructions: If MCP-compatible servers are connected, their usage instructions are injected as attachments and re-injected after context compression to prevent the loss of critical directives.
- Skill Index: Aggregates the names, descriptions, and trigger conditions of installed skills so the model knows which commands are available in the current session.
- Environment Information: Platform, date, working directory, and other runtime awareness data injected through environment augmentation functions.
- Lazy Loading Hints: When lazy loading is enabled, the prompt includes instructions on how the model can discover not-yet-loaded capabilities through search tools.
The core philosophy behind this mechanism is: the prompt is a projection of system state, not a static configuration. This is one of the key differentiators between refined implementations and rough scaffolds.
Context Management: Compression and the Attachment System
Long sessions inevitably exhaust the context window. Mature agents handle this by triggering automatic compaction (a “compact” operation) when nearing the threshold: historical conversations are summarized while preserving key decisions and current task states. However, compression has a hidden risk—certain information that must remain continuously effective (such as external service instructions or environment constraints) can be “washed away” by summarization.
Refined implementations therefore introduce an attachment system: information that must not be lost is marked as persistent attachments and re-injected after every compression cycle. Coarser frameworks often perform summarization without re-injection, causing the model’s behavior to drift after compaction. This is one of the common causes of the “the more you chat, the dumber it gets” phenomenon observed in practice.
Sub-Agents and MCP: Two Paths to Capability Expansion
Sub-agent architecture serves to isolate contexts. A main agent can delegate independent subtasks (such as large-scale code search) to sub-agents, which operate in their own contexts and return only their conclusions. This approach prevents intermediate processes from polluting the main context while also enabling parallel execution of multiple tasks.
Tool protocol integration provides ecosystem extensibility. By connecting external servers through standardized protocols, tool descriptions, authentication information, and usage instructions are incorporated into a unified dynamic injection pipeline, receiving the same prompt representation and permission controls as built-in tools. This requirement places high demands on engineering—external tool descriptions vary widely in quality and require normalization by the framework.
Comparative Analysis and Engineering Takeaways
Comparing these mechanisms against typical open-source scaffolds reveals differences along three dimensions: first, whether the prompt is dynamically assembled; second, whether critical information can be restored after compression; and third, whether permission controls operate with sufficient granularity. Refined implementations are notably more thorough on all three points.
For teams building their own agents, several principles stand out: treat the prompt as a “serialization of runtime state”; unify the permission model and result formats across the tool ecosystem; and distinguish between “compressible conversation history” and “non-lossy system constraints” in context lifecycle management. Following these principles is essential for constructing coding agent systems that behave stably, remain auditable, and support extensibility.
