AC
Chapter 02Start With the Codebase

Why your repo matters

A confused codebase produces confused output. No amount of prompting fixes bad structure.

25 minLesson 1 of 4

Most people think the key to agentic coding is writing better prompts. They spend hours crafting elaborate instructions, adding examples, specifying edge cases. Then they wonder why the output is still wrong.

The real issue is almost never the prompt. It's the codebase.

The agent reads before it writes

When you give Claude Code a task, the first thing it does is read your code. It uses tools — Glob, Grep, Read — to understand the project before generating a single line. This isn't optional behavior. It's how the agentic loop works.

If your code is messy, the agent's output will be messy. It mirrors what it sees. A codebase with inconsistent patterns, ambiguous naming, and tangled dependencies doesn't just confuse the agent — it actively teaches it the wrong patterns.

I've seen the same prompt produce great code on a well-structured repo and garbage on a poorly-structured one. Same model, same wording, completely different results. Structure is the 80% factor.

What "agent-friendly" means

An agent-friendly codebase isn't a perfect codebase. It's a readable one. Three things matter:

Clear directory layout

Every directory has a purpose. A developer (or agent) can predict where a file lives before looking.

Consistent patterns

Files that do similar things are structured the same way. No surprises.

Explicit boundaries

What can import what is defined, not implied. The agent shouldn't have to guess.

The agent processes your codebase in seconds. It doesn't have the luxury of asking a colleague "hey, where do we put API routes again?" It either finds a clear answer in the structure, or it guesses. And guessing is where things go wrong.

The before and after

Here's something I see constantly in consulting. A flat src/ directory with 200+ files:

Messy project structure
src/
  Button.tsx
  useAuth.ts
  api.ts
  utils.ts
  UserProfile.tsx
  helpers.ts
  types.ts
  useProducts.ts
  AdminDashboard.tsx
  formatDate.ts
  ... 190 more files

When you ask the agent to "add a new settings page," it has to read dozens of files to understand the patterns. Where do pages go? Are hooks colocated or separate? What's the difference between utils.ts and helpers.ts? The agent can't tell, so it picks one at random.

Now compare that with a structured monorepo:

Structured monorepo
apps/
  web/
    app/           # Pages and layouts (Next.js App Router)
    components/    # Feature components (admin/, blog/, contact/)
packages/
  db/              # Database client + generated types
  shared/          # Utilities, schemas, validation
  ui/              # Shared UI primitives
  api/             # TRPC routers

Same prompt — "add a new settings page." The agent immediately knows: pages go in apps/web/app/, components go in apps/web/components/, if it needs a new API endpoint that's packages/api/. No guessing. No reading 200 files to figure it out.

This isn't about perfection

I'm not saying your codebase needs to be immaculate before you start using Claude Code. I'm saying that spending an hour reorganizing your project will save you ten hours of fighting bad output.

The bar isn't "perfect." The bar is "predictable." Can the agent look at your project and know, without being told, where new code belongs? Can it read one component and correctly infer the patterns for the next one?

If yes, prompting becomes almost effortless. If no, no amount of prompt engineering compensates.

Layered rules: ECC in practice

Claude Code uses a hierarchy of configuration files to understand your project. This is part of the Effective Claude Code (ECC) methodology — rules cascade from general to specific:

Rule hierarchy (most general to most specific)
~/.claude/CLAUDE.md          # User-level: your global preferences
.claude/CLAUDE.md             # Project-level: shared team rules
CLAUDE.md                     # Repo root: project-specific instructions
apps/web/CLAUDE.md            # Package-level: scoped overrides

Each level overrides the one above it. Your global preferences set a baseline. Project rules refine them. Package-level rules get specific.

This is powerful because it means you can define common rules once at the user level — "I prefer functional components," "always use TypeScript strict mode" — and override them per project when needed.

In practice, the repo root CLAUDE.md does 90% of the work. But as your setup matures, the layered system lets you scale without repeating yourself.

The payoff

A well-structured codebase doesn't just help the agent. It helps you. When the agent's output is predictable and consistent, you spend less time reviewing, less time correcting, and less time frustrated.

I restructured a client's codebase — took about four hours. Their next sprint with Claude Code was 3x faster than the one before. Not because the model got smarter. Because the codebase got clearer.

The rest of this chapter covers the specific tools that make this work: CLAUDE.md, auto memory, and the conventions that agents parse best. But none of those tools matter if the foundation — your repository structure — is chaos.

Start with the codebase. Everything else follows.