LLM codebase context, explained
LLM codebase context is the part of a repository a model is allowed to see when it answers a question about that repository. It has to be selected, bounded, readable and current. Copying every file into a prompt is the simplest version, and usually the worst one: it spends the context window on files the question never needed and gives the model no way to tell whether the copy is stale.
This guide covers what "codebase context" means, why raw dumps fail, the four practical approaches, and a repeatable way to build context with codebase-snapshot.
What is LLM codebase context?
A model has no ambient access to your repository. Everything it knows about the code arrives in the request: the system prompt, the conversation, and whatever files or retrieval results the client attaches. That attached material is the codebase context.
Good codebase context has four properties:
- Relevant. It contains the files and symbols that can answer the question.
- Bounded. It fits the window with room for instructions and a reply.
- Attributable. The model can name the file and line a claim came from.
- Current. It is possible to tell whether the context still matches the working tree.
A codebase snapshot is one way to produce all four at once. It is not the only way, and it is not always the right way. A small bug in one file is better handled by opening the file than by packing the repository.
Why a raw codebase dump fails
The arithmetic is unforgiving. A token is roughly 3.5 bytes of source code, so a 1 MB repository is about 285,000 tokens before any instructions. A 128k window cannot hold it; a 200k window can hold the code but not much of a conversation. Real applications are five to twenty times larger than a small library.
The failure is not only size. A dump is usually:
- Unfiltered. Vendored dependencies, generated files, lockfiles and test fixtures ride along because the packer copies what it finds.
- Undifferentiated. The model sees every file with the same weight. The one module the question depends on is buried among hundreds that are irrelevant.
- Unaccounted. A packer that silently skips or truncates a file leaves the model unaware that its picture has a hole. The answer sounds complete and is not.
- Undated. The model cannot know whether the snapshot was generated five minutes or five months ago. A stale snapshot is worse than no snapshot because it reads as current.
The tooling answer is not "write a better prompt". It is to produce a smaller, indexed artefact and to make freshness checkable.
Four ways to give a model codebase context
| Approach | What it is good at | Where it breaks |
|---|---|---|
| Paste files by hand | One file or one symbol; complete control | Does not scale past a handful of files; easy to miss a caller |
| Pack the repository | A stable artefact attached to one conversation; offline and inspectable | Size; stale copies; no principled choice of what matters |
| Bounded retrieval over a local index (MCP) | A question answered from the few files that matter; live edits | Needs a client that speaks MCP; the index has to be built |
| Code graph or embedding index | Cross-repo and semantic questions at scale | Infrastructure, embeddings and a server; overkill for one repository |
Most teams end up combining the second and third: a packed snapshot for a first, broad review, and a live MCP server for follow-up questions. The rest of this page shows the packed route, because it is the one that works in a fresh clone and in any chat client that accepts attachments.
Build codebase context with codebase-snapshot
1. Generate the snapshot
npx codebase-snapshot generate
The default writes ./snapshot with Markdown parts, a manifest, a prompt and reports. The run is deterministic: unchanged input produces byte-identical parts and the same fingerprint. The parts never exceed the configured byte budget.
2. Read the prompt before the parts
PROMPT.md states the fingerprint, the commit, the parts, what was left out and how to attach the files. Paste it as the first message. It is the instruction sheet that turns a pile of Markdown into context the model can use.
3. Attach only what fits
A hosted chat often accepts ten files. A 12-part snapshot cannot be delivered whole. Either raise --max-part-bytes so the same content becomes fewer parts, or scope the run with include/exclude patterns to the directories the question is about. The prompt's delivery section does the arithmetic for the current run.
4. Ask a bounded question
"Using the attached snapshot, trace how authentication reaches the database. Cite the file and line for each hop; if the answer is not in the attached files, say what is missing." The second clause is what stops a model from filling a gap with a plausible invention.
5. Validate before reusing the context
npx codebase-snapshot validate
validate re-hashes the tree and exits non-zero on drift. Put it in CI or a pre-commit hook. When it fails, regenerate; do not keep asking questions against a snapshot that no longer describes the code.
For a live alternative, run the MCP server and let the client call the tools:
npx codebase-snapshot mcp
The read tools use the working tree by default, so an uncommitted edit is visible without a regeneration. See MCP server for a codebase.
How much codebase context fits?
Use the destination, not the tool's default. The snapshot budget should leave room for the system prompt, the question and the reply.
| Window | Practical context ceiling | What that means |
|---|---|---|
| 32k | ~20k tokens | One small module or a slice, not a repository |
| 128k | ~80k tokens | One 300 KB part, or a scoped folder |
| 200k | ~130k tokens | Two or three parts, or a larger slice |
| 1M | ~700k tokens | A medium repository whole, with room for analysis |
The estimates are conservative on purpose. A 128k window filled to 120k leaves no space for a useful answer, and models often perform worse when the relevant material is diluted.
Keep context current
A snapshot is a moment, not a subscription. The repository moves; the file on disk does not. The controls that make freshness visible are:
- Fingerprint. The manifest records a content fingerprint; two runs over the same input agree.
- Validate. The command compares the snapshot with the tree and exits non-zero on drift.
- Git state. The manifest records the branch, commit and dirty state at generation time.
- Live index. The MCP read tools overlay uncommitted edits and new files, and say so in a trust envelope.
Read Stale context is your bug for the CI pattern.
What leaves the repository?
The CLI and the MCP server run locally. There is no account, upload or telemetry. Generated artifacts contain redacted source, not your credentials: sensitive paths are excluded by default, and secret-shaped values in ordinary files are replaced with a marker. The audit report names the file and line but not the value.
Redaction is not a guarantee. If you need a formal data-handling statement, read the security review, which lists the residual risks instead of hiding them.
How to choose a codebase context tool
Ask five questions:
- Does it fit the destination? A tool that produces one 40 MB file has not solved the attachment problem.
- Does it account for omissions? Every skipped or shortened file should have a reason.
- Can it prove freshness? A hash or a validate command, not a promise.
- Does it handle secrets? Default exclusions, span redaction and an audit.
- Can an agent use it without a human? A machine-readable contract and bounded tools, or at least static pages it can fetch.
Those five questions are also the design brief for codebase-snapshot. The comparisons with repomix, gitingest and code2prompt apply the same questions honestly.