How Repository-Level AI Coding Agents Work: Architecture and Engineering Practice
Repository-level AI coding agents are not chat-style tools that spit out code from a single prompt—they are execution systems that place large language models inside a closed software engineering loop. This article breaks down their technical architecture into four layers: the agent main loop, permission boundaries, context isolation through sub-agents, and verification loops. It also covers practical engineering points for making these agents work reliably in real repositories. Developers will learn why these systems outperform single-session tools and how to keep them controllable, auditable, and verifiable.
Repository-level AI coding agents are not chat-style tools that generate code from a single prompt. They are execution systems that place large language models inside a closed software engineering loop. Industry observation shows growing demand for coding agents that can read a repository, understand tasks, call tools, run verification, and write results back. Their value lies not in short demos but in long-term gains across the engineering workflow.
To understand how these systems work, it helps to break them down into four layers: the main loop, permission boundaries, context division of labor, and the verification loop. Together, these four layers determine whether an agent can work stably in a real repository, rather than just producing code snippets that look correct.
1. The Main Loop: From Code Completion to Context, Planning, Tools, and Write-Back
Traditional AI assistance in IDEs focuses on completion, conversation, and local edits—essentially a front-seat copilot. A repository-level coding agent is closer to a repository-level executor: it first reads the codebase, understands the task-relevant files and dependencies, builds a to-do list, then calls tools to read and write files, search code, run commands, and execute tests, adjusting its plan based on what it observes. The core is not single-prompt generation but a continuously running agent loop.
An abstract main loop looks like this:
Read task and repository context-> Create or update the plan-> Select and invoke tools-> Observe tool results-> Decide whether the plan needs correction-> Write back changes or output a decision-> Enter the next iteration until completion criteria are met
The key to this loop is “observe and correct.” The model does not write all the code in one pass; after every step, it reads real feedback: whether files exist, whether tests pass, whether the build fails, and whether the diff is reasonable. Tool calls connect the model’s text capabilities to a real engineering environment, enabling multi-file edits, dependency analysis, and command execution. This is why many developers feel on first use that it behaves less like a plugin and more like a capable engineering assistant living in the terminal or development environment.
The main loop also needs stopping conditions. Without clear completion criteria, an agent may keep revising endlessly; without limits on turns and cost, it may fall into unproductive exploration. Production systems therefore need maximum turn limits, task completion checks, failure escalation, and human takeover points, making the loop controllable, auditable, and terminable.
2. Permission Boundaries: The Real Danger Is Writing Without Seeing the Boundaries
The real danger of AI coding is often not that the model cannot generate code, but that it makes blind or unauthorized changes without understanding the blast radius, or executes commands it should not. A repository-level agent must not be given unrestricted access by default. For each turn, the system must decide: what the agent can see, what it can change, and which actions require stopping to ask a human.
Permission policies can be abstracted into several levels: default confirmation, auto-accept edits, read-only planning, auto-execution, no-prompt mode, and full authorization. The difference between these levels is not just naming—it reflects product philosophy: collect evidence before executing, or execute while confirming along the way; default to least privilege, or grant temporary elevation per task. The “read-only planning mode” is particularly representative: in this mode, the agent reads code, does research, and forms a plan without rushing to modify files. This fundamentally reduces the probability of “fixing A and breaking B” by making the model gather evidence before entering execution.
Permission boundaries should also include tool-level and environment-level constraints. For example, read tools can be open, write tools should require change-set confirmation, command execution should run in a sandbox or whitelist, and network access and sensitive configuration access need additional approval. For high-risk actions, the system should record audit logs and support diff review and rollback. Least privilege, explicit confirmation, change isolation, and traceable auditing are prerequisites for deploying agents in real engineering environments.
3. Context Division of Labor: Isolating Context with Sub-Agents
Where large models most often break down on engineering tasks is cramming “finding clues, understanding the repository, writing the implementation, running verification, and explaining the reasoning” into a single context window. As turns accumulate, the context fills with noise, conflicting information, and irrelevant details, degrading until the model forgets its goal or makes bad judgments.
The core of the sub-agent mechanism is context isolation. Different sub-agents have independent contexts, independent tool constraints, and independent permissions. For example, an exploration sub-agent reads the repository and locates clues, a planning sub-agent researches solutions and breaks down steps, an implementation sub-agent makes local edits, and a verification sub-agent runs tests and reviews diffs—while the main session retains only key decisions and final results. This splits one large, messy task into multiple smaller tasks with cleaner contexts.
This division of labor brings three benefits. First, it reduces context pollution, letting each sub-agent focus only on its own evidence and goals. Second, it enables parallel exploration—multiple read-only sub-agents can analyze different modules simultaneously. Third, it isolates failures: if one sub-agent misjudges, it does not contaminate the entire state of the main session. But it also introduces coordination costs: sub-agents need unified task boundaries, result formats, and permission scopes, and the main session must aggregate evidence, resolve conflicts, and decide whether to continue. Otherwise, the more sub-agents you add, the harder state consistency becomes to maintain.
4. The Verification Loop: From Generation to a Verifiable Closed Loop
A repository-level agent cannot stop at “generating code”—it must enter a verification loop. Verification should include at least static checks, unit tests, integration tests, builds, diff review, and rollback plans. After each round of changes, the agent should read verification results and judge whether the task goal is met; on failure, it should locate the cause and fix it, rather than stacking more changes on top.
The value of the verification loop is turning “looks correct” into “verified by evidence.” For example, after changing an interface, check its callers; after modifying configuration, run the relevant tests; after refactoring, confirm the build and critical paths. For tasks that cannot be verified automatically, the system should output a change summary, risk points, and a manual review checklist. In production, teams can also combine logging services, monitoring and alerting, and continuous integration capabilities from general cloud environments—such as Baidu AI Cloud—to bring agent runtime logs, change audit records, and verification results into a unified observability system, forming a traceable closed loop.
5. Key Points for Engineering Deployment
- Build repository indexing and task decomposition mechanisms so the agent understands the structure before acting.
- Configure permissions on a least-necessary basis, with tiered approval for writes, command execution, and sensitive access.
- Keep key decisions in the main session, and assign exploration, planning, implementation, and verification to dedicated sub-agents.
- Make testing, builds, diff review, and rollback a fixed verification loop.
- Record turns, tool calls, change sets, and failure reasons to support auditing and cost control.
- Set human takeover points, escalating to people promptly when risk is high, evidence is insufficient, or verification fails.
Overall, the technology behind these systems is not mysterious: use the main loop to connect the model to engineering tools, use permission boundaries to control risk, use context division of labor to reduce pollution, and use verification loops to ensure results are checkable. Only when all four layers work together can repository-level coding agents be more stable, more controllable, and closer to a true engineering assistant in production environments than single-session tools.
