A New Practice in Local AI Programming: Building a Front-End Development Agent with Self-Correction
Recently, a technical practice on building a localized AI programming agent (AI Agent) has garnered attention. This practice focuses on the front-end development domain, successfully enabling the agent to independently complete coding tasks and perform self-correction by equipping it with a series of carefully designed tools and workflows. This exploration not only showcases the potential of local models running on consumer-grade hardware for specific tasks but also provides valuable experience for the engineering implementation of AI agents.
Design Foundation: Building the Agent’s Core Toolset
To transform an AI agent from a mere code generator into a “developer” capable of actually working on projects, it must be given the ability to interact with the development environment. This practice begins by defining a basic set of file system operation tools for the agent, forming the foundation of its core capabilities.
This toolset includes:
write_file: Writes content to a file, the basis for the agent to persist its work.
read_file: Reads file content, enabling the agent to understand existing code and project context.
make_dir: Creates a directory, used for organizing project structure, such as separating static assets.
read_dir: Reads the directory structure, helping the agent to grasp the overall project layout.
By design, this solution intentionally chose to explicitly define available tools using an allowlist approach rather than providing an unrestricted bash command-line environment. This decision was primarily driven by security and controllability considerations, effectively preventing the agent from executing potentially destructive operations like rm (remove) or kill (terminate process).
Closing the Loop: Introducing Compilation and Static Analysis for Self-Correction
File operation capabilities alone are not enough, as AI-generated code is inevitably prone to errors. A key innovation is the establishment of an automated validation and correction loop for the agent. This borrows from the concepts of continuous integration and quality assurance in software development.
The specific implementation involves providing the agent with common front-end development compilers and static code analysis tools (linters)—such as deno check for TypeScript, or htmlhint, stylelint, and eslint for front-end projects—as tools. Simultaneously, the System Prompt explicitly instructs the agent to call the corresponding checking tool for verification after each code modification.
When a check tool reports an error, the agent receives feedback containing the error type, location (e.g., line number), and description. Based on this information, the agent re-analyzes the problem, attempts to generate a fix, and then repeats the verification process until all checks pass. This “generate-validate-correct” cycle endows the agent with a powerful self-correction capability, enabling it to autonomously resolve most coding errors and significantly improving the reliability of task completion.
Optimizing Interaction: Iteration and Alignment of Partial File I/O Tools
In practice, the developers found that even for minor code changes, the agent needed to read and rewrite the entire file. This led to significant token consumption and efficiency bottlenecks when dealing with large files. To address this, the project began developing tools that support partial file operations.
The read_file tool was first extended with from_line and to_line parameters, allowing the agent to precisely read the context of the problematic code based on the error line number returned by the linter. However, designing a similar feature for the write_file tool went through a complex iterative process:
Initial Design: Mimicking the interface of JavaScript’s Array.prototype.splice() function, from_line and delete_count parameters were designed. However, tests showed that the AI model struggled to accurately understand and use this set of parameters, frequently resulting in incorrect code insertion or deletion locations. Although the self-correction loop could eventually fix the error by completely rewriting the file, this defeated the purpose of the optimization.
Parameter Alignment: To simplify the model’s comprehension, the parameters for write_file were changed to match read_file: from_line and to_line. The situation improved, but “off-by-one errors” still occurred frequently, where the modified code would leave one or more lines of old code behind.

- Behavioral Alignment: After analysis, the developers inferred that the model might cognitively prefer a closed interval
[from_line, to_line] that includes both boundaries, whereas the tool’s underlying implementation might be a half-open interval [from_line, to_line). This mismatch caused discrepancies in boundary handling. After adjusting the tool’s implementation logic to align with the model’s “mental model” (i.e., processing a closed interval), the success rate of partial writes was fundamentally improved.
This debugging process profoundly reveals the importance of “interface alignment” in human-computer interaction, especially in the interaction between AI and its tools. The design of a tool must not only conform to human logic but also adapt to the “cognitive habits” of the AI model.
Industry Reflection: The Parallel Between AI Agent Development and “Browser Compatibility”
This experiment has sparked deeper reflection. The process of debugging and adapting tools for black-box AI models is strikingly similar to the experience of early web front-end developers writing “polyfills” to ensure compatibility across different browsers like Netscape and Internet Explorer.
- Lack of Standards: Currently, different major model providers lack a unified specification for tool calling, much like how early browsers had their own interpretations of web standards.
- Need for a Compatibility Layer: The “harness” that developers write for AI agents to control and adapt tool calls is essentially a “polyfill.” It serves to smooth out the behavioral differences between various models, enabling them to perform tasks consistently.
- Ecosystem Evolution: Just as web standards eventually unified, but polyfills remained an essential skill for front-end developers for a long time, the harness layer for AI agents may evolve into a significant engineering field in the future. As model capabilities advance, some harness functions may be natively replaced, but the work of ensuring compatibility and adaptation will persist for a long time.
This observation suggests that the development of AI will not simply “devour” all engineering jobs. Instead, it may give rise to new fields that require deep engineering experience. At the same time, this also raises questions about the current business model of token-based pricing for cloud LLM APIs: if small, locally-run models can efficiently complete complex tasks in specific domains through clever engineering, will the commercial moat of large cloud-based models be challenged?