How to Build a Controllable AI Coding Agent
This article explains why controllability is the primary goal when building your own AI coding agent, and how to achieve it with an open-source approach. It covers two complementary runtime forms—local terminal agents and cloud task agents—a five-layer architecture with replaceable interfaces, tool access and permission boundaries, observability and quality feedback loops, human-in-the-loop review, and a phased adoption roadmap with common pitfalls to avoid.
1. Why Controllability Comes First
Software engineering agents are moving from code completion to task execution. They can read a codebase, locate relevant files, propose and apply changes, run verification commands, and even open a code review request when done. As their capabilities expand, the real concern shifts. Teams are no longer mainly worried about whether the model can write correct code; they want to understand, constrain, and reproduce what the agent does.
A closed agent locks teams into several variables they cannot control: the available models are decided by the provider, review policies cannot be aligned with internal standards, integration options are limited, and the runtime lacks observability. The more capable the agent, the less willing teams are to let it work unsupervised.
Building your own agent addresses four core needs:
- Auditable: the reasoning behind each step, tool calls, and file changes can all be traced.
- Modifiable: prompt strategies, tool sets, review rules, and model selection can all be swapped.
- Adaptable: it can connect to internal coding standards, pipelines, knowledge bases, and ticketing systems.
- Observable: call volume, success rate, latency, and failure causes can be measured and trigger alerts.
None of these can be bought from an external vendor. They come only from owning the runtime and the orchestration layer, which is the fundamental reason to take the open-source route.
2. Two Runtime Forms: Local Terminal and Cloud Task
A mature agent system is usually not a single form. It splits work by interaction density and execution duration.
A local terminal agent runs on the developer’s own machine and handles highly interactive, short-feedback exploratory work: quickly understanding an unfamiliar module in the command line, making small changes, and running tests immediately. Its strengths are closeness to local code and the local environment. Sensitive code never has to leave the internal network, and feedback latency is minimal.
A cloud task agent runs in a cloud sandbox and handles long-running, parallel background work. Engineering tasks often do not start in a terminal: a bug is raised in a collaboration tool, a requirement is logged in a task system, or routine maintenance needs to be triggered on a schedule. This kind of agent can be triggered by a collaboration platform, a code hosting platform, a task system, or a standalone interface, then completes code changes in an isolated environment and submits a review request.
The two forms form a complementary loop:
Developer --interactive exploration--> Local terminal agent --> local commit| |+--dispatch background task--> Cloud task agent --> pull request --> human review
The key value of the cloud form is parallelism and isolation: multiple tasks run at the same time without occupying anyone’s local environment, and they do not pollute each other through dependency conflicts. The key value of the local form is privacy and hands-on feel. In real teams, the pattern is usually “explore locally, deliver in the cloud.”
3. Layered Architecture: Designing Controllability into the Structure
Split the system into five layers, each with a replaceable interface.
- Trigger layer: receives task requests from the terminal, collaboration tools, task systems, code hosting events, or scheduled jobs, and converts them into a unified internal task description.
- Orchestration layer: handles task decomposition, plan generation, step scheduling, failure retries, and termination conditions. It is the control center for behavior.
- Tool layer: exposes atomic capabilities such as code search, file read/write, command execution, test runs, and log queries through controlled interfaces.
- Model layer: hides differences between models behind a unified adapter interface, making model selection a configuration item rather than a code change.
- Governance layer: handles identity and permissions, audit logs, metrics monitoring, cost tracking, and policy checks.
The direct benefit of this layering: swapping models does not touch business logic, adding an internal tool does not change orchestration code, and adjusting review standards only changes policy configuration.
4. Tool Access and Permission Boundaries
Tools set both the upper limit of an agent’s capability and the lower limit of its risk. The design principles are least privilege, explicit declaration, and full traceability.
- Tool allowlist: register only tools that are truly necessary; never allow a general arbitrary-command passthrough.
- Path and repository boundaries: limit writable directories and accessible repositories to prevent unauthorized changes.
- Second confirmation for high-risk operations: deletion, force push, dependency upgrades, and production configuration changes require human approval.
- Sandbox isolation: cloud tasks run in disposable containers that are destroyed when the task ends.
- Credential management: secrets are issued as short-lived credentials by a secret management service and are never written into prompts or logs.
An illustrative tool declaration (pseudocode):
@tool(name="run_tests",scope="workspace",requires_approval=False,timeout=600,)def run_tests(target: str) -> dict:"""Run the specified test target in the sandbox and return structured results."""...
5. Observability and the Quality Loop
Without measurement there is no control. Collect at least four kinds of data: task volume and trigger source distribution, success rate and failure attribution, per-task latency and token consumption, and the rework rate (the proportion of submissions that are asked to be changed after review).
Build a loop on top of this: failed samples are automatically archived as regression cases; rework samples are used to improve prompts and review rules; abnormal metrics trigger alerts and support pausing a category of tasks with one click. Logs and metrics can be implemented with general-purpose logging, monitoring and alerting, and object storage capabilities, avoiding heavy self-built infrastructure.
6. Human-in-the-Loop Review: Writing Standards into the Process
Work submitted by the agent must pass the same review bar as human code. Practical measures include: every change must include its task source and an execution summary; per-task change size is capped, and anything beyond the cap must be split; a policy engine first checks standards (naming, directories, dependencies, test coverage) before human review; and high-risk directories have a hard rule requiring human approval.
7. Adoption Roadmap and Common Pitfalls
A recommended four-stage rollout: start with a read-only assistant (search and explanation), then open limited write access (single repository, small changes), then add cloud parallel tasks and multiple trigger sources, and finally complete governance and cost optimization.
There are four common pitfalls: granting too much permission at once; coupling prompts with business logic so the system becomes unmaintainable; looking only at success rate while ignoring rework rate; and lacking termination conditions, which wastes resources. All of them point to the same fix—make “auditable, modifiable, observable” the acceptance criteria, not an afterthought.
The essence of building your own agent is not to replicate a product, but to hold the agent’s runtime in your own hands and make it a first-class member of the team’s engineering system: bounded, recorded, and able to evolve.
