Adding features to an existing codebase π§©
Adding a feature to an existing codebase is the most common use case β and the one where mistakes cost the most. The agent does not know your internal patterns, your unwritten conventions, your architectural compromises. If you do not explain them, it invents them. And when it invents them, the result looks correct but is out of sync with the rest of the code.
The challenge is not getting the agent to write code: it is getting it to write code that looks like your team wrote it.
When to use the agent for this case π―
- New feature in an existing module β API endpoint, UI functionality, business logic.
- Interface extension β adding fields, methods, parameters to an existing interface.
- Inter-module integration β connecting two subsystems with a new flow.
- Contextual bug fix β the fix requires understanding the architectural context before intervening.
When NOT to use the agent β
- The project has no tests β without a safety net, every modification is a gamble. Add tests first, then delegate.
- You do not know the patterns β if you do not know how the target module handles errors, you cannot constrain the agent. Study first, delegate after.
- Tasks touching more than 5 modules β break into independent sub-tasks, each with its own context.
Opening prompt π
“Add [feature] to module [x]. CONSTRAINTS: use the same pattern as [reference file], do not modify [boundaries/y]. TARGET FILES: [list]. CRITERIA: [tests to pass].”
Concrete example:
“Add a
POST /api/v2/ordersendpoint in theorders/module. CONSTRAINTS: use the same error handling pattern aspayments/handler.go, do not modify the existing API contract. TARGET FILES:orders/handler.go,orders/service.go,orders/model.go. CRITERIA: all existing tests pass + new test for the endpoint.”
Context setup π§
- Open reference files β the similar existing module, interfaces to respect, reference tests.
- Explicitly indicate patterns β “use the same error handling pattern as
payment.service.ts”, “follow the layered structure of the other Go handlers”. - Define boundaries β what must NOT be touched. The agent is a precise executor, not an autonomous architect.
Workflow π‘
- Plan: the agent analyzes reference files and proposes a plan (modules involved, imports to update, tests to run).
- Approve: review the plan before the agent writes code. This step prevents rushed or incorrect writes.
- Execute in micro-increment: after each modified module, verify intermediate build and tests.
- Reset context between tasks β do not carry refactoring residue into a bugfix.
- Final review: generated code must be reviewed like any PR.
Plan approval is the most valuable moment of the cycle. It is where you prevent 90% of errors.
Acceptance criteria β
- Existing tests still green after each increment.
- New tests for the added feature.
- No out-of-scope files touched.
- Adherence to repository patterns (naming, structure, error handling).
Further reading π
- Understanding an existing codebase β before adding, understand what is already there. For large repositories, see the section on indexing and persistent indexing.
- Testing and TDD β the safety net before every modification.
- Blog: Question-driven specification β how to turn questions into executable specifications.