How to Build Your Own AI Agent: A Practical Guide for Software Engineering
This guide explains how to build AI agents that solve real software engineering problems instead of trying to do everything. It breaks down four agent roles—local interactive, cloud background, automated review, and codebase memory—along with their boundaries, collaboration model, a layered reference architecture, key design tradeoffs, observability and security governance, and a phased rollout path.
1. Start With the Problem, Not the Agent
Many teams fail at building agents not because the model is weak, but because they define the goal as “an assistant that can do anything.” A practical approach works backward from the workflow: list high-frequency, repetitive engineering actions with clear inputs and outputs, then decide whether the agent needs to be interactive or run in the background for long periods.
A useful test uses three dimensions: interaction frequency, execution time, and blast radius. Tasks with high interaction frequency and short execution time fit a local terminal where a human confirms each step. Tasks that run long, need parallelism, or produce side effects such as code commits fit a cloud sandbox running in the background. Read-only tasks that need broad coverage fit a standalone review agent. Knowledge that must be reused across sessions should be captured in a codebase memory layer.
Under this framework, a complete software engineering agent system usually has four roles: a local coding agent, a cloud coding agent, an automated review agent, and a codebase documentation and memory agent. They are not four isolated tools but a closed loop that goes from “human initiates” to “machine executes” to “machine reviews.”
2. Responsibilities and Collaboration Across the Four Agent Types
1. Local Interactive Agent: Working Close to the Codebase
The core value of a local agent is low latency and complete context. The command-line runtime is deployed on the developer’s machine, while model inference can run in the cloud. This keeps the local file system, build scripts, and test commands immediately available, while still allowing the use of larger models that a laptop cannot host.
It fits three kinds of work: exploring an unfamiliar codebase and generating a call-relationship overview; making small changes and immediately running tests to verify them; and executing migration tasks that require multiple rounds of confirmation. The key design principle is “every step is reversible.” Have the agent work on a separate branch or working copy, and keep change granularity at a reviewable commit level.
2. Cloud Background Agent: Triggered by Events, Not by a Terminal
In practice, many engineering tasks do not start from a terminal: a bug ticket is filed, a feature request is logged, or a scheduled maintenance task fires. The point of a cloud agent is to expand the trigger source from “a human opens a terminal” to “execution starts when an event arrives.”
It runs in an isolated sandbox and automatically opens a pull request when finished, leaving the final merge decision to a human. Because environments are independent, multiple tasks can run in parallel without competing for local resources or polluting the developer’s dependency environment. Engineering-wise, you must configure resource quotas, execution timeouts, an outbound network allowlist, and an artifact archiving policy for it. Otherwise, parallelism becomes a source of runaway cost.
3. Automated Review Agent: Strict, Read-Only, and Tied to Code Hosting
The first principle of a review agent is read-only. It does not modify code; it only outputs structured feedback: issue location, severity level, rationale, and suggested fix. It needs tighter constraints than a general coding agent—for example, requiring references to specific files and line numbers, prohibiting speculation about content it has not read, and explicitly marking confidence on uncertain items.
It should integrate closely with the merge request process, running checks of different intensity at three points—commit, review, and merge—and turning conclusions into trackable quality metrics rather than scattered comments.
4. Codebase Memory Layer: Giving Agents Stable Project Context
When an agent behaves inconsistently, the cause is usually missing context rather than weak reasoning. The memory layer needs to provide three kinds of information: architecture and module boundary descriptions, coding and review conventions, and records of past decisions and pitfalls. It must evolve from the same source as the code and be updated with each commit; otherwise, it quickly degrades into stale documentation.
3. Reference Architecture and Key Design Tradeoffs
A reusable layered structure looks like this:
Trigger layer Terminal / collaboration tools / tickets / code hosting events / scheduled jobsOrchestration Task parsing -> plan generation -> tool calls -> result validationExecution layer Local runtime | cloud sandbox container | read-only review runtimeContext layer Code retrieval + memory store + convention storeModel layer General LLM + code-specialized model (self-hosted or managed)Governance layer Unified tracing, logging, metrics, permissions, and audit
A few tradeoffs that are easy to get wrong:
- Tool granularity: tools that are too coarse (such as “modify the project”) make planning uncontrollable, while tools that are too fine stretch the context. It is recommended to converge on five categories: read, search, edit, execute, and commit.
- Context budget: retrieval results must be ranked and trimmed, leaving the limited window for the most relevant files and convention entries.
- Prefer determinism: anything that can be confirmed by static checks, type systems, or unit tests should not be left to model judgment.
- Human in the loop: merge authority always stays with humans, and the agent is responsible for producing reviewable changes.
A sample task description structure (used to unify local and cloud inputs):
task:goal: Fix the specified bug and add regression testsscope: [directories allowed to change]constraints: [files that must not change, checks that must pass]acceptance: [test commands, expected output]mode: interactive | background | review
4. Why “Controllable and Modifiable” Matters More Than “Out of the Box”
Once an agent enters the main engineering workflow, the team must be able to explain every decision it makes. If the system is closed, the replaceable model, review strictness, integration approach, and observability are all constrained by external limits, and the team can only adapt passively.
Open implementations bring three practical benefits: review standards can be written as the team’s own rules; models can be selected in tiers by task difficulty, balancing quality and cost; and internal toolchains can be integrated as first-class citizens instead of being stitched together with workarounds. If building every component in-house is not feasible, you can adopt a combination of “general runtime + your own rules and memory layer,” keeping the controllable boundary at the most critical orchestration and governance stages.
5. Observability, Evaluation, and Security Governance
Observability is not optional. At minimum: every run has a unique trace ID that links the trigger, plan, tool calls, changes, and validation results; key nodes record input summaries and elapsed time; and failures can be replayed. Unified tracing should cover all three paths—local, cloud, and review—otherwise cross-form issues cannot be located.
For evaluation, build a small, stable regression set: pick real historical bugs and feature requests, fix the acceptance commands, and compare pass rates, change size, and manual rework across different models and prompting strategies. Without a regression set, model upgrades are flying blind.
On security, be explicit about: sandbox isolation and least privilege, secrets kept out of the context, a network access allowlist, redaction of artifacts and logs, and auditable scope for code leaving the environment. When internal code is involved, also define data retention and deletion policies.
6. A Phased Rollout Path
Phase one: build a local interactive agent and get the “retrieve—modify—verify” loop working. Phase two: add the memory layer and convention store to stabilize context quality. Phase three: introduce the cloud background agent, starting from a single trigger source and expanding gradually. Phase four: add the read-only review agent and unified tracing to form a quality loop. Finally, establish regression evaluation and a cost dashboard for continuous optimization.
If you want to accelerate delivery using general cloud capabilities, you can use a container platform to host the sandbox runtime, object storage to archive artifacts, a message queue to handle event triggers, and logging and monitoring services for observability; the model service can simply be selected from the general category. Baidu AI Cloud offers corresponding capabilities in these general categories, so teams can focus their effort on orchestration logic and their own rules instead of rebuilding infrastructure.
