How AI Coding Tools Work in Large Codebases: Technical Principles

    As codebases grow from thousands to millions of lines, an emerging industry consensus is that the performance ceiling of AI coding tools is often determined not by the underlying LLM, but by the engineering scaffolding built around it. This article breaks down, from a general technical perspective, how intelligent coding tools stay reliable in very large repositories: why small-repo experience does not transfer, why real-time retrieval replaces full-codebase indexing, and a seven-layer scaffolding structure covering context specification files, hooks, skills, subagents, toolsets and permission boundaries, external integrations, and observability feedback loops. It closes with practical adoption advice for teams that want to build their own scaffolding.

    When a codebase grows from a few thousand lines to a million, the way intelligent coding tools operate changes fundamentally. An emerging consensus in the industry is that the upper limit of how well these tools perform is often determined not by the underlying large model itself, but by the engineering scaffolding built around the model. From a general technical perspective, this article breaks down the principles that allow intelligent coding tools to work reliably in very large codebases.

    1. Background: Why Experience from Small Repositories Does Not Transfer

    In a sample project of a few hundred lines, a coding assistant can almost “guess” correctly: there are few files, naming is consistent, and dependencies are simple. A real enterprise environment is completely different:

    • Monolithic repositories of a million lines or more, with a history spanning more than a decade;
    • Microservice architectures spread across a dozen or more repositories, with interface contracts scattered everywhere;
    • Thousands of engineers committing continuously every day, so the code is in a constant state of drift.

    In this kind of environment, the model faces only one core contradiction: the context window is limited, while the codebase is nearly infinite. Every engineering technique is, in essence, solving the same problem: “delivering the right small amount of information into the context at the right moment.”

    2. The Key Decision: Abandoning Full-Repo Indexing in Favor of Real-Time Retrieval

    One common approach in the industry is to apply retrieval-augmented generation (RAG): split the entire codebase, vectorize it, and build an index, then recall the most similar fragments at query time. This works well in small repositories that change slowly, but it quickly breaks down in environments with frequent commits:

    1. The indexing pipeline has inherent latency, so new commits cannot enter the index immediately;
    2. Embedding and rebuilding indexes for large-scale repositories is expensive and hard to refresh at minute-level or even second-level intervals;
    3. As a result, the function the model “finds” may have long since been renamed or moved, producing an answer that looks plausible but is out of date.

    A class of approaches that more closely matches how human engineers work has therefore been adopted: no pre-indexing; instead, operate on the file system in real time. Its working loop is roughly to traverse the directory structure, read key files, locate symbols with keyword search, and then hop layer by layer along reference relationships. Illustrative pseudocode:

    1. while not confident:
    2. dirs = list_dir(current_path) # Observe the directory structure
    3. hits = grep(keyword, scope=dirs) # Locate by keyword
    4. file = read_file(pick_most_relevant(hits))
    5. current_path = follow_references(file) # Hop along references

    The advantage of this approach is that it always sees the latest code, with no stale-index problem; the cost is that it needs enough “starting information” to decide where to begin looking. This leads to the first layer of the scaffolding.

    3. The Seven-Layer Scaffolding Structure (Top-Down)

    Layer 1: Context Specification Files (A Map for the Model)

    Convention files are placed in the repository root and in subdirectories at each level, and loaded automatically when a session starts. As the model moves between directories, it looks upward along the way and layers these files together, forming a combined view of “global conventions plus local agreements.”

    The key practice is conciseness. These files are not a documentation repository; they are context injected at high frequency and their size must be controlled. The common community experience is that a single file stays ideally between 60 and 120 lines, with 200 lines as the upper limit; beyond that, later rules tend to be down-weighted by the model, which makes writing them pointless. Recommended content includes: build and test commands, directory responsibility boundaries, modules that must not be touched, and pre-commit checks.

    Layer 2: Hooks (Script-Level Enforcement)

    Specification files are essentially “suggestions,” and the model may ignore them; hooks are deterministic controls that are enforced 100%, and the model cannot bypass them. Typical mounting points include: validation before a file write, interception before command execution, and triggers at the end of a session.

    A counterintuitive but more valuable usage is the “session-end hook”: at the close of each session, have the model review the changes made, automatically summarize the pitfalls encountered and new conventions, and write them back into the specification file. The session ends and experience is deposited; when the next session starts, the tool already understands the repository better.

    1. {
    2. "hooks": {
    3. "PostToolUse": ["format_and_lint"],
    4. "Stop": ["summarize_and_update_guide"]
    5. }
    6. }

    Layer 3: Skills (Expert Knowledge Loaded on Demand)

    A skill is a modular capability package that is loaded into the context only when the task matches, avoiding filling the window all at once. A skill typically contains: a description of applicable scenarios, operational steps, and reference scripts and templates. For example, “adding a new external interface” can be packaged as a skill containing layering conventions, error code conventions, and test case templates. Loading on demand keeps the context budget spent where it counts.

    Layer 4: Subagents and Task Isolation

    For tasks that require large-scale searching, an independent subagent can be dispatched to complete the investigation in an isolated context and return only the conclusions to the main thread. This both protects the main context from being polluted by massive search results and makes it easier to process multiple unrelated investigation tasks in parallel.

    Layer 5: Toolsets and Permission Boundaries

    Tools are the only channel through which the model interacts with the system. The principle is minimum sufficient: the four basic capabilities of read, write, search, and execute cover the vast majority of scenarios; the more tools there are, the higher the selection cost and the greater the probability of misuse. At the same time, permission configuration defines writable directories and a whitelist of executable commands, keeping risk within an acceptable range.

    Layer 6: External Integrations

    Standardized protocols are used to connect to external systems such as requirement tracking, logging services, monitoring and alerting, and container platforms, giving the tool troubleshooting and verification capabilities beyond coding. The key to integration is to converge the volume of returned information, passing back only summaries relevant to the current task.

    Layer 7: Observability and the Feedback Loop

    Record the tool call chain, failure reasons, and manual correction points of each session, forming metrics (such as first-pass rate and average number of rework rounds). This data is in turn used to optimize the configuration of the first six layers, forming a closed loop of continuous improvement.

    4. Adoption Recommendations

    1. Build specifications first, then talk about models: writing build, test, and directory conventions clearly usually yields the fastest returns.
    2. Leave deterministic work to hooks: formatting, static checks, and type validation should not rely on the model’s memory.
    3. Strictly control context volume: keep specification files short rather than long, and split long content into skills loaded on demand.
    4. Keep a human review gate: high-risk changes (data migrations, permission adjustments) should require mandatory human confirmation.
    5. Drive optimization with metrics: evaluate scaffolding quality with rework rate and first-pass rate, rather than looking only at the model version.

    5. Conclusion

    In large codebases, the engineering essence of an intelligent coding tool is a combination of “retrieval strategy + layered context + deterministic constraints.” Abandon full-repo indexing and stick to real-time reading, use concise specifications to provide a starting point, use hooks to hold the baseline, use skills to extend on demand, and then keep refining with an observability feedback loop. This methodology is independent of any specific product: on top of the general container, logging, and monitoring capabilities of cloud platforms such as Baidu AI Cloud, any team can build its own engineering scaffolding.