← ~/pillars / context-engineering / context-friendly-repos
Context-friendly repos
Vertical slices, hexagonal layers, and descriptive naming keep an agent's working set small, and the three-layer context construction that rescues monorepos past the ~2,500-file ceiling.
updated 2026-06-12 ~4 min read pattern 02/05 Context Engineering
on this page
Context. An agent changing a feature must load enough code to understand it. The context window is the budget, and it degrades before it fills: “context rot” means accuracy drops as tokens accumulate (context engineering). Repo structure decides how much of that budget a routine task costs.
Forces. Agents navigate by just-in-time retrieval (glob and grep for
identifiers, then read), so descriptive names and precise boundaries make
retrieval cheap, and UserRegistrationHandler.java beats Handler17.java in
every scenario. Layer-first structures scatter one feature across several
trees. Monorepos give agents both sides of a cross-service contract in one
place (Augment Code),
but practitioner analysis puts a ceiling at roughly 2,500 files before
retrieval quality degrades. Past it, agents produce syntactically correct
code against outdated method signatures, a structural failure rather than a
reasoning one
(tianpan.co).
Resolution. Organise by problem domain, not technical layer. The Claude Code docs say exactly this (best practices). For Java, that means vertical feature slices with hexagonal layers inside each slice; at scale, add deliberate context construction rather than hoping a bigger window saves you.
Vertical slices, honestly framed
Multiple independent sources converge on vertical slice structure as the most agent-compatible pattern: when a feature owns its directory (endpoint, logic, data access), an agent grasps “the complete picture” within one manageable context (Cloudurable). Be clear about the evidence grade: this is practitioner convergence, not a controlled study. No published benchmark compares vertical slice against hexagonal or layered structures on agent task completion rates. The trade-off is also real: slices accept intentional duplication where DRY would have shared code. Our view: the convergence is strong enough to act on, because the mechanism (smaller retrieval footprint per task) follows directly from how agents load context.
Hexagonal within the slice
Vertical slices and hexagonal architecture are orthogonal, not rivals: slices set feature boundaries, hexagonal sets layer boundaries inside each one. The AWS Architecture Blog recommends the layering for agentic development explicitly: domain/application/infrastructure separation lets agents “modify business logic and validate it locally without touching cloud-specific code” (AWS architecture blog). The combined Spring Boot layout:
com.example.service/ order/ # vertical slice: the Order feature domain/ Order.java # pure POJO, no framework annotations OrderRepository.java # port (interface) application/ PlaceOrderUseCase.java # orchestration, no infrastructure imports adapter/ in/ OrderController.java # inbound: @RestController out/ JpaOrderRepository.java # outbound: JPA implementationAn agent loads order/ and has the whole feature; swapping the database
adapter never touches domain classes. Enforce the boundaries with ArchUnit so
they are tool-owned facts, not prose, the same division of labour as
linters as ground truth.
File size: an open tension
Two fetched sources genuinely disagree. Anthropic’s context engineering guidance favours small, well-named, single-responsibility files, because grep returns precise matches with a minimal context footprint. An April 2026 arXiv paper argues the opposite: human conventions like “one class per file” don’t map to agent needs, and larger, semantically cohesive units may serve transformer attention better (arXiv 2604.07502), a well-argued thesis, but practitioner observation rather than measurement. Working resolution until someone runs the study: choose feature cohesion over layer decomposition at directory level, keep classes separated by responsibility within a slice, and watch what your agents actually fumble.
The ~2,500-file ceiling and three-layer context construction
When a monorepo passes the ceiling, the fix is scoped retrieval, not a larger window: “models lose their ability to attend uniformly across a very long context”. The three-layer pattern (tianpan.co):
- Repo-map index: a ~1,000-token structural index of files and key symbols, ranked by PageRank over the dependency graph (aider’s canonical implementation).
- Dependency-graph traversal: build tooling such as Nx exposes runtime dependencies and blast radius, so the agent loads what a change touches.
- Scoped context construction: domain-level CLAUDE.md files and on-demand retrieval instead of exhaustive loading, the mechanics of which are the monorepo standards pattern.
Services with genuinely independent lifecycles do not need to share a repo at all; cross-repo agent sessions pay a context re-establishment tax, but so does every navigation inside an oversized monorepo.
Worked example
A team’s order service is layer-first: controllers/, services/,
repositories/, 41 classes per tree. The task “add a cancellation deadline to
orders” makes an agent grep three trees, read eleven files, and still miss the
DTO mapping. They restructure into feature slices with hexagonal internals as
above, add ArchUnit rules (adapters call use cases, never repositories;
domain/ imports nothing from adapter/), and put a module map plus
where-things-go table in the root CLAUDE.md: the
architecture map artifact, following
the CLAUDE.md architecture
discipline. The same task now resolves to order/ plus one shared event
class: four files read, boundaries enforced by tests rather than review
comments, and the agent’s first diff compiles against current signatures.
// sources
- https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents
- https://code.claude.com/docs/en/best-practices
- https://arxiv.org/pdf/2604.07502
- https://tianpan.co/blog/2026-04-17-coding-agents-monorepo-context-window
- https://cloudurable.com/blog/a-deeper-dive-when-the-vibe-dies-comparing-codebase-architectures-for-ai-tools
- https://aws.amazon.com/blogs/architecture/architecting-for-agentic-ai-development-on-aws/
- https://www.augmentcode.com/learn/monorepo-vs-polyrepo-ai-s-new-rules-for-repo-architecture