AI generates code fast, but code that runs is not necessarily correct, and someone who doesn’t understand the code can’t fix it during an incident. This post is how a backend engineer uses an AI coding agent to go faster without losing control: how to hand out work, where to write conventions for the agent, how to verify, and when to do it yourself. The examples come from a Spring Boot lab written by an agent, where three bugs only showed up when the tests ran.
At a glance:
flowchart LR
RU["Project rules: AGENTS.md"] --> P[Prompt]
P --> G[Generate]
G --> U[Understand]
U --> V[Verify]
G -.->|skip last two| VC["Vibe coding: prototypes only"]
V --> T[Tests are the referee]
V --> D[Read diff as reviewer]
S["Do it yourself: learning, security, core logic"]
1. What AI is good and bad at
Key idea: AI produces code fast and mostly right for common tasks; it doesn’t know your business, your codebase or your risks. The engineer’s role shifts from typing code to steering, reading and verifying.
| AI does well | AI does poorly |
|---|---|
| Boilerplate, CRUD, DTOs, mappers | Business rules that were never written down |
| Explaining code, regexes, stack traces | Knowing what the codebase already has and which conventions apply |
| Test skeletons, obvious test cases | Picking meaningful tests and domain-specific edge cases |
| Suggesting fixes for common errors | Spotting a security hole in context |
| Syntax conversion, version upgrades | Debugging issues that depend on real data, timing, several services |
AI is like a very fast junior who is often confidently wrong. Your value is knowing what the right result looks like, so fundamentals (transactions, indexes, auth flows, HTTP) matter more than before.
2. Vibe coding and the four-step workflow
Key idea: vibe coding means describe, let AI generate, run it, fix the prompt on errors, ship when it runs. A fundamentals-driven workflow keeps that speed but adds two mandatory steps: understand the code and verify it.
Andrej Karpathy coined the term in early 2025. It suits prototypes and one-off scripts; it doesn’t suit production code, team code, or code that must be maintained.
The four steps:
- Prompt: the concrete task, context, input/output, constraints, acceptance criteria.
- Generate: the result is a draft, not the product.
- Understand: be able to say what the code does, why it’s written that way, and what the alternatives are. If you can’t explain it to a colleague, you don’t understand it.
- Verify: run the tests, add edge cases, read the diff with a checklist (section 6); security and payments need a second reviewer.
If anyone else will read this code (including you in six months), don’t vibe code it.
3. When not to use AI
Key idea: use AI to speed up what you already understand, not to replace understanding. In five situations, do it yourself or use AI only as a thinking partner.
| Situation | Why | Using AI the right way |
|---|---|---|
| Learning a new concept | Struggle is how it sticks; AI doing it is like someone else going to the gym for you | Do it yourself once or twice, then ask AI for a better way |
| Security code | Mistakes don’t show until you’re attacked | Use audited libraries, read every line, check against official docs |
| Core business logic | AI doesn’t know your policies, regulations, domain edge cases | You decide the rules; AI only writes the scaffolding |
| Interview preparation | Interviews have no AI | 2–4 weeks before, cut AI use, solve and explain on your own |
| Debugging a complex incident | AI lacks runtime data, timing, cross-service interactions | Use AI to list hypotheses; you measure and eliminate |
4. Three kinds of tools and project rules files
Key idea: chat for questions and explanations, IDE completion for typing faster, agents for reading the codebase, editing many files and running commands. Agents are the most powerful but forget everything between sessions, so conventions have to be written into a file they read automatically.
Three common problems with agents: a new session doesn’t remember the previous session’s conventions; in a long session the beginning falls out of the context window (the number of tokens the model can read at once) so the code drifts into contradictions; the agent reads files one by one but doesn’t know the big picture (“we use JWT, responses are wrapped in an envelope, authorization is permission-based”).
The fix is project rules: a conventions file the agent reads at the start of each session, like onboarding docs for a new hire.
| Tool | File (at the time of writing) |
|---|---|
| Cross-tool convention | AGENTS.md at the repo root |
| Claude Code | CLAUDE.md (repo root, optionally in subfolders) |
| GitHub Copilot | .github/copilot-instructions.md |
| Cursor | .cursor/rules/*.mdc |
| Gemini CLI | GEMINI.md |
What belongs in the rules file:
- Stack, versions and build/test commands: with the test command, the agent can verify its own work.
- Structure: package by feature (
user/,role/,auth/), so one folder gives the agent enough context (DDD and package structure). - Conventions and prohibitions: constructor injection, record DTOs, never return entities; no
EAGER, never disable tests to get a green build. - Settled decisions: links to ADRs (C4, arc42 and ADRs, ADRs and quality requirements) so the agent doesn’t “reinvent” the design.
A sample AGENTS.md for a Spring Boot project
# Project rules
## Stack
Java 21, Spring Boot 4.0.x, Spring Security 7 (OAuth2 resource server, HS256 JWT),
Spring Data JPA + Hibernate 7, PostgreSQL 17, Maven wrapper.
## Commands
- Build + all tests: ./mvnw verify
- One test class: ./mvnw test -Dtest=UserServiceTest
- Integration tests need Docker (Testcontainers).
## Structure (package by feature)
dev.lab.hr.<feature>/ -> Controller, Service, Repository, Dtos, entities
dev.lab.hr.common/ -> ApiResponse, exceptions, GlobalExceptionHandler
dev.lab.hr.security/ -> SecurityConfig, converters, handlers
## Conventions
- Constructor injection, private final fields. No Lombok.
- Request/response DTOs are records. Never return or bind entities.
- Success body: ApiResponse<T>. Errors: ProblemDetail via GlobalExceptionHandler.
- Services own transactions; map to DTOs inside the transaction (open-in-view=false).
- Every new endpoint: @WebMvcTest for HTTP contract + service unit test.
## Never
- FetchType.EAGER, field injection, @Disabled on failing tests, catching Exception to hide errors.
- Secrets in code or application.properties (use env vars).
- New dependencies without asking.
## Decisions
- ADR-001 refresh tokens: opaque, hashed in DB, rotation with reuse detection.
- ADR-003 authorization: permission = (method, path pattern) per role, checked in the filter chain.
## Workflow
Plan first, wait for approval, implement in small steps, run ./mvnw verify, show the diff summary.
Keep the file short (a few dozen to a couple of hundred lines) and concrete. The vaguer a rule (“write clean code”), the less the agent follows it; the more checkable a rule (“every @RequestBody has @Valid”), the easier it is for both the agent and the reviewer to check.
5. Writing prompts for backend work
Key idea: a good backend prompt has five parts: the task, the context, input/output, constraints, and acceptance criteria written as Given/When/Then. The acceptance criteria become tests, and tests are how the agent checks itself.
Two prompts for the same task:
- Weak: “Create a create-user API.”
- Strong:
Task: add POST /api/v1/users in the user/ package.
Context: Spring Boot 4, follow AGENTS.md. UserService and UserRepository exist.
Input: CreateUserRequest(name, email, password, gender) as a record with Bean Validation.
Output: 201 + Location header, body ApiResponse<UserResponse>, no password field.
Constraints: email trimmed + lowercased before the duplicate check; BCrypt via PasswordEncoder;
DB UNIQUE on email stays the real guard; no new dependencies.
Acceptance criteria:
Given a valid request, when POST, then 201, Location ends with /users/{id}, password not in body.
Given an email that exists (any case), when POST, then 409 ProblemDetail, nothing saved, no hashing.
Given name blank, bad email and 3-char password, when POST, then 400 listing all three fields.
Plan first and wait for my OK. Then implement, add @WebMvcTest + unit tests, run ./mvnw test.
The second prompt narrows the solution space, gives the agent criteria to check itself against, and gives you a checklist for review. If you can’t write the acceptance criteria, you’re not ready to hand the task out. For debugging prompts, give the code, expected and actual behavior, the error log and what you’ve tried; always ask for the reasoning, because wrong reasoning usually means wrong code.
6. The verification loop: tests are the oracle
Key idea: “works on my machine” is not evidence. Evidence is tests that actually ran, plus you reading the diff and reading the tests too. The lab for this post had three bugs that got past the eye but not past the tests.
The Spring Boot 4 lab of this series (users, roles, permissions, JWT, refresh tokens) was written by an AI agent in an earlier session. The code read fine; the first mvn test had 3 failing tests: a LazyInitializationException in the refresh-token flow, a UNIQUE test expecting the error in the wrong place, and a 403 turned into a 500. All three were “right line by line, wrong when put together”, exactly the kind of bug AI tends to produce and skimming readers tend to miss.
Checklist for reading an AI-written diff (more on reviewing in effective code review):
- Data: N+1 queries,
EAGER, missing index for a new filter, a transaction wrapping an outbound HTTP call. - Correctness: non-atomic check-then-write, transaction boundaries,
nullhandling, time zones. - Security: does the new endpoint have an authorization check, does it leak sensitive fields, is input validated, are secrets hard-coded.
- Errors: are exceptions swallowed, are HTTP error codes right, do logs leak personal data.
- Scope: did the agent touch files outside the task, did it add dependencies.
The three lab bugs and why the agent wrote them
| Bug | Symptom | Cause | Lesson |
|---|---|---|---|
LazyInitializationException |
POST /auth/refresh returns 500 |
The controller called user.getRoles() after the @Transactional service returned, with open-in-view=false |
Map to a record inside the transaction |
Wrong UNIQUE constraint test |
Error thrown at save(), test expected it at flush() |
GenerationType.IDENTITY forces an immediate INSERT to get the id |
The agent wrote the test from an assumption, not from actual behavior |
| 403 became 500 | @PreAuthorize denied but the client got 500 |
@ExceptionHandler(Exception.class) swallowed AccessDeniedException |
A catch-all must leave framework exceptions alone |
The third bug was confirmed by removing the fixing handler and running again: the test expecting 403 got 500.
All three share one root: the behavior only appears when the parts run together.
- Lazy loading depends on where the transaction ends and whether
open-in-viewis on. Read the controller alone and there’s no transaction; read the service alone and nobody callsgetRoles()afterwards. The model learned from lots of code withopen-in-view=true(the default), where this bug is hidden. - IDENTITY depends on the id strategy: with
SEQUENCE, Hibernate can defer the INSERT until flush; withIDENTITYit can’t. The agent’s test followed the model “Hibernate batches writes until flush”, which is true in many other places. - The catch-all is right in isolation (no stack trace leaks) and
@PreAuthorizeis right in isolation. Only together, when the method-security exception passes through the advice before reaching Spring Security’s filter, does it become a 500.
What it means for the workflow: reviewing file by file isn’t enough. You need integration tests that run the whole chain (real security chain, real transactions), and someone who knows the framework to read the results. The agent fixed all three quickly once given the error logs; what it didn’t do on its own was know it had to run things to find them.
7. Security code, company data and secrets
Key idea: for security code, AI only writes the draft; you understand every line and check against official docs. For data, don’t paste company code or secrets into tools your company hasn’t approved.
For security: use audited libraries (Spring Security, don’t hand-roll a JWT filter), review against OWASP, write attack-scenario tests (tampered token, expired token, reused refresh token) and keep an audit log.
On privacy: free tiers may store data or use it for training, so only use tools your company approved. Don’t paste secrets, customer data or logs with personal data; the agent can read the whole repo, so secrets belong in environment variables. The agent runs commands on your machine: don’t let it push, delete, or touch production credentials on its own.
8. Measuring whether AI actually helps
Key idea: feeling “faster” isn’t data. Measure with delivery and quality metrics compared with a baseline period, not with lines of code.
METR’s July 2025 randomized trial with experienced open-source developers: with AI they were about 19% slower while believing they were faster. It doesn’t apply to every task, but it’s reason enough to measure, over 4–8 weeks before and after:
| Metric | What it measures | Bad sign |
|---|---|---|
| Lead time (first commit to production) | Real speed | Code is generated fast but PRs wait longer for review |
| Review time per PR | Load shifted to reviewers | Bigger, harder-to-read PRs |
| Change failure rate, bugs reaching production | Quality | Goes up after adopting AI |
| Share of code rewritten within 2–3 weeks (rework) | “Runs but wrong” code | Goes up |
The right question isn’t “do we use AI” but “for which kinds of task do the numbers improve”; usually repetitive, well-shaped work like CRUD, migrations and version upgrades.
How to talk about AI in a senior interview
Interviewers often ask “how do you use AI”. A weak answer lists tools. A strong answer describes the workflow and how risk is controlled:
- Triage the work: “I give the agent CRUD, migrations, test skeletons and version upgrades. Auth, payments and business rules I design myself; the agent only writes to the spec.”
- Spec first: “I write acceptance criteria as Given/When/Then in the prompt, and the agent turns them into tests.”
- Conventions in a file: “The repo has an
AGENTS.mdwith the stack, test commands, prohibitions and ADR links, so every session and every teammate produces consistent code.” - Verification: “I run integration tests with the real security chain and real transactions. Once an agent wrote code that read perfectly but failed three tests: lazy loading, IDENTITY flush timing, and a catch-all that turned 403 into 500.”
- Measurement: “We tracked lead time, review time and rework before and after, not lines of code.”
Also be able to state the limit: “I don’t use AI for parts I don’t understand, because when it breaks at 2 a.m. I’m the one fixing it.”
Remember
- Explain the difference between vibe coding and the Prompt → Generate → Understand → Verify workflow.
- Name the five situations where you should do the work yourself, and why.
- Know what a project rules file should contain, and why the test command must be in it.
- Write a prompt with constraints and Given/When/Then acceptance criteria for an endpoint.
- Measure AI’s impact with lead time, review time and rework, not lines of code.
Interview questions
Mid
How do you verify AI-generated code?
Run real tests (including integration tests with real security and transactions), then read the diff and the tests like a reviewer.
Checklist: N+1 and fetching, transaction boundaries, authorization and input validation, swallowed exceptions, scope of the change. Ask “would this test fail if the logic were deleted”.
When do you not use AI to write code?
When learning a new concept, writing security code or core business logic, practising for interviews, and debugging complex incidents that need runtime context.
There AI is still useful as a thinking partner (listing hypotheses, explaining), but the decisions and the verification are mine.
What should a project rules file for a coding agent contain?
The stack and versions, build/test commands, package structure, coding conventions, prohibitions, and links to settled decisions (ADRs).
Keep it short and checkable, in Git. Agents forget between sessions, so this file is the shared long-term memory of every session and every teammate.
Why put acceptance criteria in the prompt?
They narrow the solution, become tests the agent checks itself against, and are your checklist when reviewing.
If you can’t write Given/When/Then for a task, the requirement is still vague; clarify it before handing it to an agent.
Senior
Tell me about an AI-introduced bug you caught. What did you learn?
The agent’s code read very plausibly, but integration tests exposed three bugs: a LazyInitializationException when the controller read a lazy collection after the transaction, a test expecting a UNIQUE error at flush while IDENTITY throws at save, and a catch-all that turned @PreAuthorize’s 403 into a 500.
The lesson: AI bugs are usually integration bugs, not syntax, so file-by-file review isn’t enough; you need tests that run the whole chain and someone who knows the framework reading the results.
How do you know whether AI really makes the team faster?
Measure lead time, review time, change failure rate and rework before and after, per kind of task.
Perception is unreliable: a 2025 randomized trial found experienced developers were slower with AI while believing they were faster. Lines of code is the wrong metric because AI inflates it effortlessly.
A colleague wants an agent to write the whole authorization layer. What do you say?
Use the agent for scaffolding, but the authorization model, the rules and the attack-scenario tests must be designed and reviewed line by line by someone who understands security.
Concretely: use Spring Security instead of a hand-written filter, list the business rules (self-deletion, super admin, audit), write 401/403 and abuse tests first, and require a second reviewer.
What are the security and data risks of running an agent in a company repo?
Code and secrets leaking to an outside service, and the agent running harmful commands on a machine with credentials.
Mitigate with an approved enterprise plan, secrets in environment variables and out of the agent’s reach, restricted commands (no push, no deletes, no production credentials), and reviewing every diff before merging.
Useful phrases for interviews
- “AI is great at producing code and weak at guaranteeing it’s right in our context.”
- “I use AI to speed up what I already understand, not to replace understanding.”
- “Our project rules file is the agent’s long-term memory: stack, commands, conventions and what’s forbidden.”
- “I put the acceptance criteria in the prompt as Given/When/Then, and they become the tests.”
- “I start in plan mode and approve the plan before any file changes.”
- “The test suite is the oracle, not my impression of the code.”
- “The bugs we caught were integration bugs: each line was fine, the combination wasn’t.”
- “For security code the agent writes a draft; I read every line and check it against the docs.”
- “We measured lead time, review time and rework, not lines of code.”
- “One feature per session keeps the blast radius small.”
Notes based on Hỏi Dân IT course material, rewritten and extended in my own words.