Three Field-Tested Methods for Getting AI to Reliably Modify Code in Complex Projects
AI coding assistants can easily handle single-file scripts, but making them work reliably across large projects with dozens of files and hundreds of changes is far harder. Based on experience from a platform migration project, this article shares three proven engineering practices: a disciplined workflow, sub-agent dispatching, and document preprocessing—none of which depend on any specific tool or model.
Three Field-Tested Methods for Getting AI to Reliably Modify Code in Complex Projects
As AI coding tools and code-focused large language models become mainstream, having AI write code is no longer remarkable. The real challenge is getting it to complete tasks in a complex project—dozens of files, hundreds of changes—in a stable, controlled, and verifiable way. During a platform migration project, I worked extensively with the combination of an AI-assisted coding environment and a mainstream code LLM. This article does not discuss any specific product; it focuses purely on engineering methodology. Here are three lessons I have validated repeatedly: a disciplined workflow, sub-agent dispatching, and document preprocessing.
1. Why Letting AI Write Code Directly Often Fails
When developers first use a code LLM, the approach is usually straightforward: hand the requirement to the model, wait for the code, run it, hit an error, and paste the error back. This works well enough for single-file scripts. In a real project, problems quickly pile up:
- Requirement drift: The model’s interpretation of an ambiguous requirement often diverges from what the developer intended. The gap typically only surfaces after the code is written, making rework expensive.
- Uncontrolled scope of changes: The model may modify code all over the repository with no sense of boundaries, introducing side effects unrelated to the goal.
- Untrustworthy completion claims: The model may claim “compilation passed” or “tests passed” without ever running the corresponding commands.
All three problems share the same root cause: there is no working discipline constraining the AI’s behavior. Human engineering teams use code reviews, test-driven development, and continuous integration to ensure quality. The same thinking can be carried over into human-AI collaboration.
2. A Disciplined Workflow: Align First, Then Act
An effective approach is to break “getting something done” into several fixed phases. A typical pipeline is: brainstorming, requirements specification, implementation plan, execution, and review. The core idea is to get the AI and the developer aligned on requirements and the implementation path before any code changes.
The requirements for each phase:
| Phase | Input | Output | Acceptance Criteria |
|---|---|---|---|
| Brainstorming | Raw requirement | Open questions, candidate approaches and trade-offs | Developer confirms the understanding is correct; no code produced |
| Requirements specification | Confirmed consensus | Written spec covering functional boundaries, constraints, and acceptance criteria | Every acceptance criterion can be tested or checked |
| Implementation plan | Spec document | Ordered task list, each with input, output, and verification method | Every task has an explicit verification command |
| Execution | Implementation plan | Code changes and verification results | Only files within the planned scope are modified |
| Review | Spec and changes | Item-by-item check record | No omissions, no deviations, or deviations are documented |
The brainstorming phase is the cheapest point to correct misunderstandings. Once execution begins, the cost of course correction multiplies, so the first two phases must not be skipped.
The workflow should also embed three hard constraints:
- Test-driven development: Write the failing test first, then the implementation. Every change has a verifiable expectation.
- Systematic debugging: When a problem arises, require the AI to reproduce it first, form hypotheses, and verify the root cause one by one—rather than repeatedly guessing at code changes.
- Verification before completion: Before the AI claims that a build or test passed, it must actually run the command and paste the output. It cannot draw conclusions from how the code looks. In embedded and firmware engineering, this rule significantly reduces false “delivered without building” reports.
This discipline does not depend on any particular tool. Any AI coding environment that supports custom instructions can implement it.
3. Sub-Agent Dispatching: Isolating Context
Another problem in complex migration projects is context pollution. If the AI expands implementation details file by file and function by function in the main conversation, the intermediate process quickly fills the context window. Early key decisions then get forgotten, and output quality degrades.
The solution is a sub-agent mechanism. Split the implementation plan into tasks, and hand each task to a sub-agent with its own independent context. The main conversation retains only the plan, task definitions, and result summaries. The actual code searching, modification, and verification all happen inside sub-contexts.
The main conversation should keep the following fields:
- Overall goal and scope boundaries
- The task list and the status of each task
- Confirmed design decisions and their rationale
- Result summaries returned by each sub-task
- Open issues and items pending confirmation
When dispatching a sub-task, the task description should include:
- Task ID and a one-sentence objective
- Files or directories the agent is allowed to read and modify
- Explicitly forbidden operations, such as “do not modify public interfaces” or “do not touch unrelated modules”
- The verification command used to determine completion
- Return format: list of modified files, raw output of verification commands, and remaining issues
This design has three benefits. The main conversation always maintains a high-level view. Sub-tasks do not interfere with each other, so the context budget is concentrated on the current task. And task granularity naturally forms checkpoints: when something fails, you only need to rerun a single sub-task instead of restarting the entire session.
4. Document Preprocessing: Making AI Actually Understand Datasheets
In chip and firmware development, LLMs have a subtler problem: retrieval accuracy against raw PDFs is very low. In an electrical specification or register manual spanning hundreds of pages, tables get read as scattered fragments, the row-column correspondence of bit fields is frequently misaligned, and key information embedded in figures is lost entirely. Asking the AI to answer questions from a raw PDF is essentially asking it to guess.
An effective improvement is to convert PDFs into structured Markdown in advance and place them in the project’s reference documentation directory. Plain text eliminates layout ambiguity. Tables become explicit rows and columns of text, and heading hierarchies form a searchable structure.
The conversion typically requires a three-step pipeline:
| Step | Target | Method | Common Issues |
|---|---|---|---|
| 1. Watermark and header/footer cleanup | Text and graphics repeated on every page | Use PyMuPDF to read text block coordinates and font attributes; identify blocks that appear at consistent positions on most pages and remove them. Watermarks are often semi-transparent graphics or text with specific opacity attributes and can be filtered accordingly | Accidentally deleting legitimate margin notes |
| 2. Table reconstruction | Discrete text distributed by coordinates | Use table detection from pdfplumber or PyMuPDF; borderless tables require adjusting tolerance parameters. Output Markdown tables | Tables split across pages break; merged-cell information is lost |
| 3. Image extraction and placeholders | Figures and diagrams | Export as separate files with page number and figure number in the filename; insert references as placeholders in the Markdown and keep captions | Images become separated from their captions |
Tool names and parameters change between versions—refer to whatever version you actually have installed. Scanned documents have no text layer and require OCR first; OCR results must be reviewed separately.
The pipeline output still needs manual review, focusing on:
- Register bit-field tables: Spot-check the first row, last row, and rows containing merged cells. Verify that bit ranges such as [7:4] are correctly recognized and that reset values match the original.
- Cross-page tables: Confirm that continuation tables are correctly stitched together.
- Image references: Confirm that every reference opens correctly and that figure numbers match the descriptions in the text.
