Working with AI Agents in a Real Codebase
Software Engineering / AI Tooling · December 2026
What I learned when AI agents stopped being a novelty and became most of the team: context budgets, commit provenance, a worktree per agent, picking a model you can actually call, and prompts that don't leave room to refuse.

How It Started for Me
This year, most of the code on two of my projects was written by AI coding agents. One is a national-scale digital-twin platform; the other is a city-scale twin builder with its own AI assistant inside the 3D scene. Often several agent sessions were running at once, plus an unattended worker on a second machine.
That makes how the agents work on the codebase an architecture problem in its own right: many concurrent writers, a shared repository, and sessions that start with no memory of the last one. Here are four lessons from it.
Lesson 1: The Instruction File That Keeps Growing
Every agent reads an instruction file at the start of a session. Ours was where every rule, lesson and decision ended up, because it was the one place every session was guaranteed to see.
It reached 126 KB, about 30,000 tokens loaded before an agent had done any work.
So we split it:
- A short, tool-neutral rules file: the stack, the commands, the required checks, and a digest of the hard rules
- A tiny agent-specific file
- Long-form notes moved into topic documents, with a "read this before touching that part" table
- Repeatable procedures turned into on-demand skills
Two months later, the rules file alone had grown back to about 56 KB. New decisions and conventions kept landing there, because it was still the one place every session would read.
Turns out, an agent's instruction file behaves exactly like shared config. Adding a rule is cheap; removing one feels risky. I'd treat it like a bundle: give it a size budget, and once a decision stops changing, move it out into docs or a skill.
Lesson 2: Many Agents, One Repository
The second surprise was git. Several sessions were committing to the same checkout, sometimes to the same file. One session committed while another was editing the same file, and two of its changes landed inside the wrong functions. The module crashed on load, and because the working copy looked fine, it took about an hour to find.
Staging rules helped, but the underlying problem was concurrent writers sharing one working area. Two controls dealt with it.
- Isolation: larger or cross-cutting work happens on its own branch in its own git worktree, rebased on main often and merged once each item is finished. That work gets its own folder to build, run and test in, without touching anyone else's.
- Provenance: every commit records which agent, machine and session made it, and is signed per machine. Enforcement is still warn-only, but every commit since carries that record, and it's what made it possible to look back and write this post.
Worktrees have their own costs, though. A fresh one doesn't include the git-ignored local files (config, keys, linked data), and without them our demo quietly fell back to an offline placeholder city. And nobody cleans them up: on the worker machine, hundreds of worktrees from closed tasks piled up until a scheduled job started removing them. Script the setup a fresh worktree needs, and remove it when its task closes.
Once more than one agent writes to a repository, treat it like a team: separate workspaces, clear staging rules, and a record of who changed what, and where.
Provenance is cheap to add early and impossible to reconstruct later.
Lesson 3: The Best Model Is the One You Can Call
On the city-scale builder, the AI assistant turns plain requests like "plant trees here" into tool calls on the 3D scene. The first model ignored the system prompt and refused a feature that did exist, so I went looking for a better one.
In a single day, the model changed five times, and quality decided none of those switches. One provider had no API billing on the account, another's SDK adapter didn't support our AI library's major version, and a third turned out to be unavailable too. What shipped was a larger model from the one provider that already had a working key.
What made that day survivable was a design decision made earlier: the model sat behind a single seam in our code, so every switch was a one-line change.
So before comparing models on quality, check which ones you can actually call: billing on the account, an SDK adapter for the library version you're on, and the region it runs in. Then pick from that list.
Lesson 4: Prompts That Leave Room to Refuse
The refusals also pointed back at our own system prompt. Looking at why the assistant turned down requests it could handle:
- Scene state like "drawn area: NONE" read to the model as a precondition, not information
- A "get scene context" tool became an escape hatch
- A tool described as "open the panel" sounded like a UI action, when it actually ran the analysis
Every rule that asked the model to use its judgment became a place for it to say no.
We rewrote the prompt twice. The first pass led with a list of what the assistant can do and described each tool by what it does. The second replaced the judgment rules with a plain lookup table: this kind of request maps to this sequence of tools. State fields got labelled "reference only, not a gate".
I can't give you a before-and-after refusal rate. The model also changed that same day, so I can't separate the prompt's effect from the model's. But the root causes were real, and the lookup-table style is how I'd write a tool-calling prompt from the start.
Wrapping Up: Treat Them Like a Team
The agents brought the usual engineering problems with them. Context files grow like config, concurrent writers need isolation and provenance, and availability narrows the choice of model before quality even comes up.
If you're building something like the platforms I described in Building Urban Digital Twins, most of these habits carry over directly: budgets for what the agents read, provenance for what they write, a worktree for each piece of work, one seam for the model, and prompts written as explicit request-to-tool maps.
I'll keep sharing what these projects teach me. Peace!