Building an In-House AI Agent System from Scratch

    This article explores why software engineering teams should build their own AI agent systems and outlines a comprehensive five-layer architecture model: local agents, cloud agents, review agents, repository memory, and observability. It details the design principles, engineering key points, and the advantages of open-source solutions to help teams construct a transparent, customizable, and evolvable digital workforce.

    1. Why Teams Need an In-House AI Agent System

    As the capabilities of large language models (LLMs) advance, software engineering agents are no longer just simple code completion tools. A mature engineering agent can read code, edit code, review merge requests, participate in defect triage and CI pipelines, and become a true “digital colleague” within the team.

    However, directly adopting closed, managed agent solutions often leads to several practical challenges:

    1. Model Restrictions: The models available to you depend entirely on the provider’s support, making it difficult to flexibly choose or switch models based on specific task characteristics.
    2. Black-Box Behavior: It is hard for teams to audit or intervene in how the agent conducts code reviews or the criteria it uses to suggest modifications.
    3. Rigid Integration: Integration with terminals, code hosting platforms, project management systems, and CI pipelines is defined by the vendor, making it difficult to adapt to existing team workflows.
    4. Insufficient Observability: Without comprehensive tracking records for every decision and tool call made by the agent, troubleshooting becomes impossible when issues arise.

    Consequently, an increasing number of teams are opting to build their own agent systems, taking control of the architecture, model selection, review standards, and integration methods. Below is a step-by-step breakdown of a complete setup path, using software engineering as the primary scenario.

    2. Overall Architecture: A Five-Layer Capability Model

    An agent system designed for engineering scenarios can typically be divided into five layers:

    • Local Agent Layer: Runs on the developer’s terminal to directly operate the local codebase.
    • Cloud Agent Layer: Handles long-running, compute-heavy tasks such as large-scale refactoring and multi-file modifications.
    • Review Agent Layer: Conducts automated reviews and quality control on the code generated by agents.
    • Repository Memory Layer: Maintains documentation and knowledge bases synchronized with the code to provide context for agents.
    • Observability Layer: A cross-system tracking, logging, and evaluation system.

    These five layers work together to form a complete “agent factory.” Let’s break down each layer.

    3. Local Agent: The Terminal Programming Assistant

    The core design principle of a local agent is “runtime on the local machine, model in the cloud.” The CLI and agent runtime environment are deployed on the developer’s own machine, while LLM inference is offloaded to the cloud.

    This architecture brings two key benefits:

    1. Data Proximity: The agent directly reads the local codebase without needing to upload the entire project to a remote environment, minimizing the exposure of sensitive code.
    2. Model Flexibility: Large programming models often cannot run on laptops. However, through the combination of a local runtime and cloud inference, developers can utilize more powerful, larger-parameter models while retaining the flexibility of local operations.

    In terms of implementation, a typical local agent needs the following capabilities:

    1. Input Parsing -> Understand the developer's natural language instructions
    2. Context Collection -> Retrieve relevant files, dependencies, and historical changes
    3. Task Planning -> Break down complex requirements into executable editing steps
    4. Tool Calling -> Read/write files, execute commands, run tests
    5. Result Returning -> Output diffs and explain the rationale for modifications

    Tool calling is the core of the local agent. It is recommended to encapsulate file I/O, command execution, and test running capabilities into structured tool interfaces, accompanied by strict permission controls (e.g., requiring confirmation for write operations, whitelisting dangerous commands) to ensure the agent “can get the job done without causing trouble.”

    4. Cloud Agent: Handling Long-Running and Heavy Tasks

    Not all tasks are suitable for local execution. Tasks like large-scale refactoring, cross-repository modifications, and batch test generation are time-consuming and compute-heavy, which can block daily work if run on a developer’s laptop. Cloud agents are designed specifically for such tasks:

    1. Task Submission: Developers initiate tasks from their terminal or project management system, including requirement descriptions and target scopes.
    2. Environment Preparation: Clone the repository, install dependencies, and prepare the runtime environment in a cloud container. Container platform capabilities can fully leverage the enterprise’s existing infrastructure.
    3. Autonomous Execution: The agent plans, edits, and runs tests in an isolated environment, iterating until the goal is achieved.
    4. Delivery: Submits the results as a branch or patch, returning to the human developer’s review process.

    The key engineering points here are isolation and idempotency: the cloud execution environment should be an ephemeral container. If a task fails, it can be entirely rerun without polluting the shared environment.

    5. Review Agent: Adding a “Second Pair of Eyes” to AI

    Having an agent write code is only the first step; ensuring code reliability requires automated review. The review agent automatically runs on every merge request, taking on the following responsibilities:

    • Consistency Check: Verifying whether the modifications align with the codebase’s existing conventions and style guidelines.
    • Regression Risk Identification: Analyzing the impact of changes and flagging potential broken callers.
    • Test Completeness Assessment: Determining whether the new logic has sufficient test coverage.
    • Modification Suggestion Generation: Providing specific, actionable improvement suggestions as comments.

    The review agent itself is an agent and needs governance. Teams should make review standards explicit—writing them as rule files or prompt templates managed in version control, rather than hiding them inside a black-box service. This makes the review behavior auditable, modifiable, and evolvable, which is the greatest advantage of an in-house system over closed solutions.

    6. Repository Memory: Keeping Documentation in Sync with Code

    An agent’s performance heavily depends on context quality. If the repository documentation consists of outdated static pages, the agent will make decisions based on incorrect premises. Therefore, a “living documentation” mechanism is required:

    1. Docs as Code: Documentation is stored as files in the repository, reviewed and modified alongside the code.
    2. Auto-Sync: When structural changes occur, the agent initiates documentation updates to ensure tight coupling between docs and code.
    3. Structured Organization: Organize knowledge by modules, interfaces, and conventions for on-demand retrieval, rather than dumping everything into the context at once.
    4. Accumulating Agent Memory: Write the experience accumulated by agents during historical tasks (e.g., “how this module is built” or “common pitfalls”) into a memory base for reuse in future tasks.

    This mechanism essentially builds an “organizational knowledge foundation” for the agent, where documentation serves both humans and machines.

    7. Observability: Making Every Decision Traceable

    Agents are autonomous systems; without tracking, they cannot be operated or maintained. It is recommended to establish a unified observability system:

    • Full-Link Tracing: Record the input and output of every reasoning round and tool call by the agent, unified and correlated across local, cloud, and review endpoints.
    • Structured Logs: Centrally ingest logs into a logging service, supporting retrieval by task ID and session ID.
    • Evaluation and Monitoring: Continuously evaluate metrics like review accuracy and task success rate, and configure monitoring alerts to detect abnormal behavior promptly.
    • Cost Visualization: Track token consumption and inference costs at each stage to inform model selection and optimization.

    Mature tracing protocols in the industry can be reused directly, or you can implement your own based on general LLM development frameworks. The focus should be on “cross-system integration” rather than the tool itself.

    8. Balancing Open Source and Autonomy

    When building an agent system, an unavoidable decision is whether to use open-source or closed solutions. The value of the open-source route lies in:

    1. Auditable: The agent’s prompts, planning logic, and tool definitions are entirely transparent, so the team knows exactly why it behaves a certain way.
    2. Modifiable: You can swap models, adjust review standards, add or remove tools, and change integration methods, tailoring the agent perfectly to your workflow.
    3. Migratable: No vendor lock-in. Models can be freely combined between open-source models and commercial cloud models.

    Of course, the open-source route also means the team needs to invest engineering effort to maintain this system. A pragmatic approach is to start from a single point: first deploy a local agent in the terminal scenario, get tool calling and tracing working, then gradually expand to cloud and review agents, and finally complete the repository memory layer. Developers can also leverage existing cloud platforms that provide model services, containerization, and storage to quickly set up the infrastructure, allowing them to focus their energy on the agent logic itself.

    9. Conclusion

    AI agents are becoming infrastructure for engineering teams, rather than isolated tools. Building your own agent system essentially answers three questions: where does the agent run (division of labor between local and cloud), how is it trusted (review and observability), and how does it evolve (repository memory and open-source customization). When a team can audit, modify, and control every aspect of the agent, the agent truly transforms from “someone else’s product” into “our own colleague.”