How to Build Autonomous and Self-Contained AI Agents
This article explains how to design AI agents as distributed systems rather than simple prompt wrappers, covering the five core layers: interaction entry points, local execution, cloud execution, automated review, and knowledge memory, plus observability. It details how to embed agents into existing developer workflows, normalize task inputs, run tasks in local and sandboxed cloud environments with resource quotas, enforce review gates, build reliable codebase memory, and keep control through open-source and full-chain tracing. A four-stage adoption roadmap is also provided.
1. Introduction: Bridging the Gap From “Can Chat” to “Can Deliver”
Many teams stop at a conversational program that answers questions in a terminal when they first start building agents. But once it is placed into a real software engineering workflow, the problems surface immediately: it cannot read the full codebase, cannot submit changes, cannot be reviewed, and cannot be traced. Bridging the gap from “can chat” to “can deliver” essentially means designing the agent as a distributed system, not as a prompt.
A mature software engineering agent system typically consists of five roles: an interaction entry layer, a local execution layer, a cloud execution layer, a quality gate layer, and a knowledge memory layer, with observability spanning the entire pipeline. Each is described below.
2. Interaction Entry: Embedding the Agent in Existing Developer Workflows
The most common reason agents fail is not insufficient model capability, but an entry design that works against human habits. Developers will not switch contexts just to use a tool, so the entry must be embedded in the interfaces they already use: the terminal, instant messaging groups, project management boards, code hosting platforms, and CI pipelines.
The key design principle is “one agent, multiple entries, unified session state.” Regardless of whether a request comes from a terminal or a group chat, it should ultimately be normalized into the same task description structure, for example:
{"task_id": "swe-20240612-001","trigger": "chat | issue | pr_comment | schedule","repo": "org/service","intent": "fix_null_pointer_in_payment_flow","constraints": { "branch": "main", "max_files": 5 }}
The benefit is that all subsequent execution, review, and tracing logic depends only on this single structured input, so the entry layer can be extended freely without affecting the core.
3. Local Execution Agent: Interactive Exploration and Fast Validation
The local agent runs on the developer’s own machine, with the runtime and command-line interface local, while model inference is hosted in the cloud. This split of “local runtime plus remote inference” is critical: it lets developers operate on the real codebase, real dependencies, and real debugging environment, while still accessing large models that local compute cannot support.
Local agents are suited to three types of work: first, interactive code exploration, such as “find all call sites that do not handle timeouts”; second, small incremental changes with immediate validation, running tests right after each edit; third, implementation tasks that turn a clear plan into code. Because it uses the developer’s own permissions and environment, the security boundary is clear, but operation auditing is needed to prevent accidental changes.
4. Cloud Execution Agent: Moving Long Tasks Out of the Local Environment
Many engineering tasks do not start in a terminal: a bug reported in a group chat, a requirement logged on a board, or a periodic maintenance task that must run on schedule. If such tasks occupy the local environment, they cause serious resource contention and state pollution.
Cloud agents run in isolated sandboxes and can be triggered by group messages, board cards, code platform events, or a web interface, then automatically create a merge request containing change descriptions and test evidence. Sandboxing brings two direct benefits: dozens of tasks can run in parallel without interfering with each other, and environments can be standardized and rebuilt, avoiding the “works on my machine” problem.
For cloud tasks, it is recommended to set clear resource and behavior quotas:
limits:max_runtime_minutes: 25max_changed_files: 8require_tests: trueallowed_commands: [build, lint, unit-test]
Quotas do not limit capability; they cage uncertainty and make agent output predictable.
5. Automated Review Agent: Strict, Detailed, and Coupled With the Code Platform
As the cost of generating code drops, review becomes the new bottleneck. The review agent should do three things: first, give actionable line-by-line suggestions rather than vague comments; second, tightly integrate with the code hosting platform’s comment mechanism so feedback lands on the exact lines; third, enforce clear gate criteria, blocking directly when tests are missing, the change surface is too large, or high-risk directories are touched.
A structured template for the review prompt can significantly stabilize output quality:
Role: Senior code reviewerInput: diff, relevant file context, project conventionsOutput: classified into blocking / suggestion / question, each with location + reason + fix exampleConstraint: do not comment on historical code unrelated to this change
The last constraint is especially important; otherwise review feedback quickly degrades into noise.
6. Codebase Memory and Documentation: Giving the Agent Reliable Context
The ceiling of an agent is often determined by context quality. Maintaining documentation and code in the same change process, and automatically generating structured memory for the agent (module responsibilities, call relationships, build and test commands, historical decision records), can greatly reduce hallucinations.
The memory layer should be tiered: long-term memory stores architecture conventions and standards, short-term memory stores summaries of files relevant to the current task, and retrieval results include source paths for traceability. Searchable, citable, and invalidatable are the three bottom lines of memory design.
7. Open Source and Observability: Keeping Control in the Team’s Hands
If the agent is a closed system, the team can only passively accept the vendor’s supported models, review policies, and integration scope. Open-sourcing lets the team review, modify, and adapt to their own standards: custom review rules, code conventions, model selection, and integration with internal tools.
At the same time, full-chain tracing is indispensable. Each task should record the prompt version, model identifier, tool call sequence, elapsed time, and failure reasons, forming a replayable execution trace. Observability is the only way to turn “occasionally useful” into “reliably usable.”
8. Adoption Roadmap
A four-stage approach is recommended: first, get a single-entry local agent working and complete the read-and-modify code loop; then introduce cloud sandboxes to handle long and parallel tasks; next, add review gates and knowledge memory; finally, complete tracing and evaluation baselines. When elastic compute, containerized sandboxes, and logging, monitoring, and alerting support are needed, you can build the foundation on general cloud product categories offered by providers such as Baidu AI Cloud, keeping components replaceable and avoiding tight coupling to any single implementation.
