8 min read

Claude Code Best Practices for Large Codebases

Claude Code's default file-traversal design causes significant token bloat in large enterprise codebases, leading to unexpectedly high costs. This guide outlines five context-scoping levers, model routing strategies, and governance settings to cut per-seat spend by 40-70% at scale.

Featured image for "Claude Code Best Practices for Large Codebases"

Here’s the number that should rethink how you roll out Claude Code across your organization: the average enterprise bill sits at roughly $13 per developer per active day, and a significant chunk of that cost comes from context window overhead — the roughly 20% of your context budget that’s consumed before you type a single instruction. That second part is the one you can actually do something about. The first — the raw spend — is a function of how Claude Code navigates code at scale, and why the tool that feels magical on a 50-file side project can hemorrhage tokens on a 50-package monorepo.

The core problem is structural. Claude Code doesn’t index your codebase the way Cursor or GitHub Copilot does. It traverses the file system, reads files, uses grep to find what it needs, and follows references — all at runtime, with no centralized index to build or maintain (per Anthropic’s official large codebase guidance). That design avoids the stale-index failures that plague RAG-powered tools when embedding pipelines can’t keep up with active engineering teams. But it means every token of context is a token your organization pays for, and in a large repository, the bill adds up fast.

Why Default Claude Code Breaks Around 30,000 Lines

The default setup works fine for small projects. Once your repo crosses roughly 30,000 lines, the agent can no longer hold a useful mental map. It grep-searches into thousands of false positives, reads dozens of files to triangulate, and burns context on every guess (per the Claude Fast Code Kit playbook). This isn’t a model quality problem. It’s a harness configuration problem.

Claude Code 2.1.x ships with a 200,000 token context window on claude-sonnet-4-6 and claude-opus-4-7. But roughly the first 40,000 tokens are consumed before you type a single message — system prompt, auto memory, skill listings, MCP tool names, environment info, and every CLAUDE.md file from your current working directory up to the filesystem root (per Start Debugging’s monorepo analysis). In a monorepo with a top-level CLAUDE.md plus rules for backend, frontend, mobile, infra, and shared libraries, you can spend 15,000 tokens before Claude reads a single user file.

The fix isn’t better prompting. It’s deliberate context engineering — shaping what enters the agent’s window instead of dumping the whole tree at it.

Scoping Context: The Five Levers That Actually Cut Token Spend

1. Launch from the subtree, not the repo root. Where you start claude determines what loads. If you’re editing packages/api, launch from there. Claude reads that subtree, loads that directory’s CLAUDE.md plus every ancestor’s, and skips siblings entirely. This single change eliminates the bulk of wasted context in monorepos.

2. Split CLAUDE.md into nested, path-scoped files. Keep repo-wide rules at the root — coding standards, layout conventions, commit guidelines. Put stack-specific instructions next to the code: packages/api/CLAUDE.md for API conventions, packages/web/CLAUDE.md for frontend patterns. Claude loads subdirectory files on demand when it reads files there, so you’re not front-loading everything.

3. Use claudeMdExcludes to skip other teams’ files. When you have to work in a shared workspace, add glob-matched exclusions to .claude/settings.local.json to skip ancestor files and packages from other teams. This prevents unrelated CLAUDE.md files from stacking into your context.

4. Delegate exploration to subagents. Reading files to understand unfamiliar code is the fastest way to fill your context window. Hand that work to a subagent — it reads in its own isolated context and reports back only the summary. Your main conversation stays clean.

5. Deny reads on generated code and build artifacts. Content searches respect .gitignore, so node_modules/ and dist/ stay out by default. For checked-in generated code or vendored SDKs, add Read deny rules in .claude/settings.json to exclude ./**/dist/**, ./**/*.generated.*, and ./vendor/.

These five levers work. But they require deliberate configuration, and most teams skip them because the defaults feel fine on small projects. If you’re working with multiple AI coding tools across your team, the configuration layering gets more complex — our guide to Claude Code, Cursor, and AGENTS.md formats covers how to set up a cross-tool source of truth that minimizes duplication.

The Multi-Repo Problem: Remote Sessions Assume One Repo Per Workflow

Here’s a hard constraint most teams discover too late: Claude Code remote and web sessions currently assume one session equals one repository, one branch, and one pull request (per GitHub issue #23627). If your architecture spans dozens of microservices across separate repositories, remote sessions are essentially unusable.

Some practitioners have developed a workaround called the “virtual monorepo” pattern — three files, no new tooling, zero workflow changes — that can give Claude Code full-system context across multiple repositories (per a writeup on Medium). It’s clever, but it’s also a workaround for a limitation that Anthropic hasn’t addressed natively. If you’re running a distributed architecture, factor this into your rollout planning.

Dynamic workflows: When the Juice Is Worth the Squeeze

Dynamic workflows — Claude Code’s ability to write its own orchestration scripts and spin up tens to hundreds of parallel subagents in a single session — are genuinely powerful for tasks that are too big for a single agent pass. Codebase-wide security audits, multi-file migrations, dead code identification across an entire service. These are the kinds of tasks that teams plan across quarters.

But there’s a cost warning baked right into the feature. Anthropic’s own documentation states that dynamic workflows can consume substantially more tokens than a typical Claude Code session. One example from Jarred Sumner shows porting Bun from Zig to Rust — roughly 750,000 lines of code, 99.8% test suite passing — completed in eleven days (per DevOps.com). That’s impressive. It also means that single session consumed tokens at a rate that would blow through most committed enterprise budgets if run without guardrails.

The practical advice: use dynamic workflows for high-value, well-scoped tasks where the output justifies the spend. Don’t use them for routine refactoring. And be aware that as of v2.1.160 (June 2026), the trigger keyword changed from workflow to ultracode to prevent accidental misfires in documentation and review sessions (per the Claude Code briefing).

The Enterprise Cost Equation: Why Model Routing Matters More Than Seat Pricing

Enterprise Claude Code pricing is structured as a $20/seat/month base fee plus usage-based API charges, with reported minimums of 50 seats for annual contracts. The seat fee is predictable. The usage portion is not.

Agentic Claude Code tasks consume 10-50x more tokens than conversational chat, and a single session can consume 500K to 2M tokens across file reads, tool calls, error loops, and context accumulation (per Morph’s enterprise pricing analysis). That’s where the cost divergence hits: you negotiate a committed token volume sized for chat, adoption spreads to coding agents, and the committed budget runs out months before the contract renews.

The single most effective lever: routing routine coding-agent requests to cheaper models (Haiku/Sonnet) can cut per-seat token spend by 40-70% (per Morph’s analysis). Prompt caching can drop input costs to 10% of standard rates (per TIMEWELL’s optimization guide). Combined, these two optimizations can mean the difference between a bill that finance accepts and one that triggers a quarterly renegotiation.

One more thing to watch: Opus 4.8’s tokenizer inflates token counts by approximately 35% compared to Sonnet 4.6 on the same text (per Bind AI’s pricing analysis). And as of June 15, 2026, programmatic Claude Code usage — headless mode, Agent SDK, GitHub Actions — is billed via a separate credit pool at full API rates, distinct from interactive subscription limits. If you’re running automated workflows, the math just changed.

Enterprise Deployment: Governance Features That Actually Matter

Enterprise deployment includes SSO/SCIM provisioning, role-based permissions, compliance API access, and managed policy settings for organization-wide governance (per Claude Code’s documentation). Admins can set per-user and organizational spending limits. Audit logs provide detailed records of user activity.

Recent additions include auto-mode guardrails in v2.1.183 that block destructive commands — git reset --hard, terraform destroy, pulumi destroy, cdk destroy — unless explicitly requested (per the Claude Report). Workload Identity Federation, GA as of June 2026, allows OIDC tokens from AWS IAM, GCP, GitHub Actions, Okta, and Entra to replace static API keys. Enterprise-managed MCP connector auth lets admins provision once via Okta for zero-touch access to Asana, Atlassian, Figma, Linear, and Supabase across all org members.

These aren’t nice-to-haves. If you’re rolling Claude Code out beyond engineering — to product managers, designers, operations teams — per-seat spend controls and role-based permissions are the difference between a governed tool and a liability.

A Decision Framework for Large Codebase Rollouts

The teams that succeed with Claude Code at scale follow a pattern I’d summarize as “scope before scale”:

  1. Start with context scoping. Launch from subtrees, split CLAUDE.md, exclude other teams’ files. This alone can cut token spend by 30-50% before you touch anything else.
  2. Implement model routing. Shunt routine requests to Haiku/Sonnet. Reserve Opus for complex, high-value tasks where the quality delta justifies the cost.
  3. Set per-seat spend controls before expanding access. Enterprise admin controls let you cap what each seat can spend. Use them from day one, not after the first budget overrun.
  4. Use dynamic workflows selectively. Reserve them for codebase-wide audits, migrations, and security reviews — tasks where the output value exceeds the token cost by an order of magnitude.
  5. Plan for the multi-repo gap. If you’re not on a monorepo, remote sessions won’t work for cross-repo workflows. Either adopt the virtual monorepo pattern or accept that some tasks require local CLI sessions.

The bottom line: Claude Code’s agentic traversal model is a genuine architectural advantage for fast-moving codebases — no stale indexes, no embedding pipeline, no cache invalidation. But that advantage only translates to enterprise value if you treat context management as a first-class engineering discipline, not an afterthought. The teams that configure their harness before they scale their rollout are the ones that don’t end up in a procurement renegotiation every quarter.

What’s your team’s breaking point — the codebase size or task complexity where the defaults stopped working, and what did you change to fix it?