NEWn1n v2.0.1 is live! Enterprise Unified LLM API Gateway with 500+ AI Models, up to 90% off, Try now

Scaling AI Coding Agents with Git Worktrees for Complex Projects

Authors
  • avatar
    Name
    Nino
    Occupation
    Senior Tech Editor

Software development doesn't always come with comfortable timelines. Recently, I was handed a 29-page requirement document containing 30+ issues, ranging from existing bugs and functional problems to improvements and entirely new features. Handling everything sequentially with a single AI coding agent wasn't practical. Some tasks required investigating existing implementations, others involved changes across multiple modules, and a few required developing entirely new functionality.

To tackle this challenge without disrupting my workflow, I leveraged high-speed LLM APIs accessed through n1n.ai to power multiple concurrent agent sessions. This setup allowed me to increase development throughput without turning the codebase into a collection of conflicting AI-generated changes. Across the workflow, I used approximately 10 branches, multiple Git worktrees, and several OpenCode sessions running parallelly. I also experimented with assigning the same feature to two different agents to compare their implementations.

Here's how I approached task distribution, isolated agent environments, branch management, integration, and verification.

The Technical Foundation

This approach relies on three primary components working in tandem:

  • OpenCode: To run multiple AI coding sessions simultaneously.
  • Git branches: To maintain independent development histories.
  • Git worktrees: To provide separate working directories for different branches.

It's important to understand how these components work together. A Git branch represents an independent line of development. A Git worktree provides a separate working directory associated with that branch. An AI agent, powered by models routed via n1n.ai, performs the actual implementation inside that specific directory.

Instead of making multiple agents work inside the same project directory, I could give each one its own workspace. One major advantage of Git worktrees is that you don't need to clone the entire repository multiple times. You can create separate working directories directly from your existing repository:

git worktree add ../project-task-a -b feature/task-a
git worktree add ../project-task-b -b feature/task-b
git worktree add ../project-task-c -b fix/task-c

Each command creates a new branch and checks it out in a separate directory. You can verify your worktrees using:

git worktree list

Now, launch separate OpenCode sessions from each directory:

cd ../project-task-a
opencode

Repeat this for the other worktrees. Each session now operates on its own working directory, branch, and uncommitted changes. One important distinction: Running multiple OpenCode sessions doesn't automatically isolate them. The isolation comes from explicitly assigning each session to a separate Git worktree.

Task Distribution and Implementation Contracts

Creating multiple worktrees is relatively easy. Deciding what should run inside them is the more important engineering decision. Before assigning tasks, I considered three things:

  1. Functional independence: Can the task be implemented without waiting for another task?
  2. Code ownership: Which modules, directories, or files will the agent modify? Are multiple agents likely to touch the same files?
  3. Dependencies: Does the implementation depend on another feature, API contract, database schema, or shared component?

For example, implementing a new backend API and building an unrelated frontend component can happen concurrently. However, if a frontend feature depends on an API that hasn't been defined yet, both implementations need coordination.

I also treated agent instructions as implementation contracts rather than simple prompts. Each task brief needed to communicate the actual problem, expected behaviour, relevant modules, implementation boundaries, existing functionality that must remain unaffected, dependencies, and testing expectations. A vague instruction such as "fix the dashboard" leaves too much room for interpretation. A task with defined expected behaviour and acceptance criteria gives the agent a much clearer direction.

The Dual-Agent Experiment

One of the more interesting experiments involved assigning the same issue to two different AI coding agents. I had two approaches in mind, so instead of choosing one immediately, I assigned each approach to a separate agent utilizing different underlying LLM instances accessed via n1n.ai.

Both received the same requirement but worked independently in separate Git worktrees, exploring their respective approaches and producing different implementations. I deliberately didn't share one agent's implementation with the other to see how differently the same problem could be approached without one implementation influencing the other.

Once both implementations were ready, I compared them based on:

  • Correctness: Did the implementation satisfy the requirement, including edge cases?
  • Architectural compatibility: Did it fit the existing application structure?
  • Maintainability: Was the code understandable without unnecessary abstractions?
  • Testing: Were there meaningful tests covering the expected behaviour?
  • Integration complexity: How much additional work was required to introduce it into the existing codebase?

Branch Management and Integration

In an ideal parallel development workflow, every task would have its own branch created directly from master. My actual Git history was more complicated. Only two lines of work started directly from master, while several other branches were created on top of existing feature branches, resulting in a stacked branch structure.

Stacked branches are useful when one implementation depends on another. Instead of duplicating unfinished changes or waiting for everything to merge into master, a dependent branch can build on an existing implementation. I also used git cherry-pick to selectively move commits between development branches:

git cherry-pick <commit-hash>

Worktrees solve the problem of multiple agents modifying the same local working directory, but they don't eliminate integration conflicts. Two agents can independently modify different worktrees and still produce incompatible changes. During integration, I encountered three merges that required conflict resolution. The largest conflict involved 17 files. I used a combination of Git commands to understand and manage these changes:

git diff main...feature/task-a
git log --oneline --graph --all
git status
git merge feature/task-a

When conflicts occurred, I resolved them by understanding what each implementation was trying to achieve rather than blindly accepting one side. Git can identify conflicting changes, but it cannot determine which implementation is architecturally correct.

Verification and Lessons Learned

One of the biggest mistakes in multi-agent development is assuming that if every agent reports successful implementation, the application is ready. An agent can successfully implement and test its own changes without guaranteeing that the combined application works. After integration, I still needed follow-up commits to fix TypeScript errors and update test snapshots.

My verification process involved reviewing the integrated Git diff, resolving implementation and type errors, checking test snapshots, running the project's test suite, and distinguishing existing failures from newly introduced issues. Agent-level verification and system-level verification are two different things.

If you are scaling your own AI-assisted engineering workflows and need reliable, low-latency model routing for parallel agents, get a free API key at n1n.ai.