Context is King (For your Code) - How a CONTEXT.md file can yield better AI coding results and maintain consistiency across multiple development sessions #191257
mojabbar
started this conversation in
Discover: GitHub Best Practices
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Every time an AI coding assistant opens your repo, it starts from scratch. What if it didn't have to?
Software engineers have always written README files. They explain what a project does, how to install it, maybe how to contribute. But READMEs were written for humans scanning a page. They weren't designed for the way AI coding assistants consume a codebase — and that distinction matters more with every passing month.
When GitHub Copilot or another AI assistant helps you refactor a service layer on Monday, it does good work. By Wednesday, when you ask it to add a new endpoint, it has no memory of the patterns you established together. It re-reads files. It re-discovers conventions. It spends context window tokens rediscovering things it already knew.
There's a simple, practical way to reduce that overhead — and it starts with a single file at the root of your repository.
What is CONTEXT.md?
A
CONTEXT.mdfile is a living document with two audiences:Both matter. But the second is the reason this pattern is emerging now.
Think of it as a briefing document — not the full source code, not the full docs, but the distilled understanding that an experienced team member carries in their head: how the pieces fit together, what the conventions are, where the boundaries lie, and what to watch out for.
The problem: context is expensive
When GitHub Copilot is asked to make a change in a codebase, the first challenge is rarely writing the code. It's understanding the system well enough to write code that fits.
That means answering questions like:
Without explicit answers, the assistant discovers them on the fly — reading files, searching for patterns, inferring architecture from implementation details. Sometimes that works. Sometimes it produces code that compiles but doesn't match the structure and conventions of the repository.
This is also where the context window matters. Even with strong retrieval and search, there's a cost to spending tokens on rediscovering the basics of the codebase in every new session. A concise, well-maintained context document lowers that cost directly.
Why a README is not enough
README files are useful, but they're usually optimized for project introduction, setup, and contribution. They answer: What does this repo do? How do I run it?
That's necessary, but insufficient for AI-assisted development.
A useful
CONTEXT.mdgoes deeper:make test-apiis more useful than "run the API tests." An assistant that knows the exact invocation can verify its own work.Put simply: a README introduces the project. A
CONTEXT.mdbriefs the assistant before work begins.What this looks like in practice
In the OctoCAT Supply Chain Management demo application, the
CONTEXT.mdopens with a tech-stack table, then covers the domain model with an ASCII ERD that both humans and AI parse well:Below that, a table summarizes each entity with its primary key, key fields, and foreign key relationships — enough for an assistant to write a new repository method or migration without first reading every model file.
The conventions section is even more valuable. It states explicitly:
An assistant that reads these four lines before generating code will produce output that is structurally aligned with the rest of the codebase. Without them, it has to infer all of this from scattered source files — or guess.
What to include in your own CONTEXT.md
You don't need to follow a rigid template, but these sections tend to carry the most value:
1. Project overview
What the application is, who it's for, and the major technologies. A table works well — AI assistants parse structured data efficiently.
2. Repository layout
An annotated directory tree — not every file, but the structural landmarks with brief descriptions of what each directory contains and why.
3. Domain model
Entities, primary keys, key fields, and foreign key relationships. If the codebase has one, an ASCII ERD packs a surprising amount of information into a few lines.
4. API surface or application boundaries
Route prefixes, HTTP methods, request/response conventions. This saves the assistant from reading every route file to understand the API shape.
5. Architectural patterns and conventions
This is consistently the highest-value section for AI assistants. Be explicit about where business logic lives, how data access works, how errors are handled, what naming conventions exist, and what patterns must be preserved.
6. Build, run, and test commands
Include the actual commands, not descriptions of commands. Assistants can use these to validate their own changes.
7. Environment variables
A table of variables, defaults, and descriptions. AI assistants frequently need to generate configuration-aware code.
8. Testing strategy
Which tools, where test files live, and what patterns they follow — mocking strategies, test database setup, fixture conventions.
9. Known gaps and constraints
One of the most underrated sections. If authentication is mocked, if a service layer doesn't exist yet, or if a library is intentionally pinned to an older version, say so. This prevents the assistant from generating code that assumes infrastructure that isn't there.
How CONTEXT.md fits with GitHub Copilot configuration
If you're already using GitHub Copilot, you might have a
.github/copilot-instructions.mdfile, path-scoped instruction files, or custom skills.CONTEXT.mddoesn't replace any of these. It complements them.A useful way to think about the split:
CONTEXT.mdcopilot-instructions.mdinstructions/*.instructions.mdskills/*/SKILL.mdCONTEXT.mdis the foundational layer. A repository instruction that says "use the repository pattern" is more effective when the assistant already knows what your repository pattern looks like fromCONTEXT.md.Treat it as a living document
The value of
CONTEXT.mdcomes from staying current. This is not a one-time onboarding artifact. It should be updated when:One practical pattern: update
CONTEXT.mdin the same pull request that changes the architecture. Another is to ask the assistant to update the file as the final step after a substantial change. The assistant that just made the change is the best author for documenting it — and the next session benefits immediately.Some teams add a
Last updatedtimestamp at the top and includeCONTEXT.mdupdates in their PR checklist for architectural changes.Getting started
The easiest way to start is to ask GitHub Copilot to draft the first version for you:
From that prompt, the assistant will explore the codebase and produce a structured document covering architecture, domain model, API surface, patterns, commands, and known gaps. It won't be perfect — you'll catch nuances it missed, unwritten conventions, historical context — but it gives you something concrete to iterate on instead of a blank page.
Then:
The first version doesn't need to be perfect. It just needs to be useful.
The bigger picture
AI coding assistants are becoming genuine collaborators in the development process. But collaboration requires shared context.
A human pair programmer who joins your team spends weeks absorbing the codebase — sitting in meetings, reading code, asking questions, building a mental model. We invest in onboarding because we know that shared context is what makes a teammate effective.
An AI assistant doesn't need weeks. It needs a well-structured file that gives it the map before it starts navigating. That investment is small — a single document, maintained over time — but the return compounds with every session.
CONTEXT.mdis that file. It's simple, it's practical, and it makes your AI assistant better at its job from the first prompt.Add a
CONTEXT.mdto one of your repositories this week. Ask GitHub Copilot to draft it, review the output, and see how it changes the quality of your next coding session. The file is small. The difference is not.Beta Was this translation helpful? Give feedback.
All reactions