AI Coding Context Management — What to Feed, What to Cut

AI coding quality is mostly a context problem. Here is how to feed agents the right context — and not the wrong one.

The gap between “this AI is brilliant” and “this AI is useless” is almost never the model — it is the context you fed it. Too little context and the agent guesses, inventing functions that do not exist. Too much and the model’s attention dilutes, producing bland code that ignores half your constraints. This guide is for developers using Claude, ChatGPT, Cursor, or Claude Code on any non-trivial task; the goal is shipping a usable first draft, not arguing with the model for forty minutes.

What this tutorial solves

“AI write me X” gets bad results because of context, not capability. The skill is knowing which context categories matter for a given task, in what order to place them, and what to cut. Done right, your first draft is 80% usable. Done wrong, you spend more time fixing the AI’s code than you would have writing it yourself.

Who this is for

Developers using Claude, ChatGPT, Cursor, Claude Code, or Codex for any non-trivial coding task — anything beyond renaming a variable or generating a one-line regex. Especially useful when working in frameworks the model knows less well (small DSLs, internal libraries, very new framework versions) or in codebases with strong unwritten conventions.

When to reach for it

Any prompt longer than two sentences, especially when working with frameworks or codebases the model does not know well. Also reach for this when the AI keeps giving you “close but wrong” output — the cause is usually missing or mis-ordered context, not a stupid model.

When this is NOT the right tool

Truly trivial tasks where the answer is obvious from the function signature (rename, simple regex, one-liner formatter). For those, just paste and ask. Over-engineering the prompt wastes more time than the task takes.

Before you start

  • Know your model’s effective context window. Claude has 200k tokens but quality degrades well before the limit. GPT and Gemini behave similarly.
  • Have your codebase open in a tool that can attach files (Cursor, Claude Code, Continue) rather than copy-paste. File attachment beats pasting for keeping structure.
  • Identify the “anchor” example: one existing file in the codebase that shows the convention you want followed.

Step by step

  1. Enumerate context categories. Before prompting, list: language and version, framework and version, the file you are editing, conventions to follow, hard constraints (performance, security, browser support), success criteria, and any APIs or libraries the change touches.
  2. Decide per category. For each, decide: include inline in the prompt, attach as a file, or skip entirely. The default should be “skip unless it changes the answer.”
  3. Order the prompt. Language and framework first, then file context, then conventions, then constraints, then the goal last. Models anchor strongly to what comes first; the goal at the end stays in working memory.
  4. Cut filler. Everything that does not change the answer comes out. A 12,000-token prompt with 2,000 tokens of relevant info performs worse than a 2,500-token prompt with the same 2,000 plus 500 of buffer.
  5. Fill knowledge gaps with docs. For framework features the model does not know (anything released in the last 6 months, or any internal library), paste the relevant doc page directly. Do not hope the model “remembers.”
  6. Show conventions by example. Attach one existing file that demonstrates the naming, error handling, and structure conventions. One example beats five paragraphs of “we always do X except when Y.”
  7. Prune after the first response. Note which context the model used and which it ignored. Cut the ignored parts and save the rest as a template.

Anatomy of a good prompt

[CONTEXT - put first]
TypeScript 5.4, React 19, Next.js 15 App Router.
Existing component for style reference: attached `Button.tsx`.
Convention: all components export named, not default.
Error handling: throw typed errors from `lib/errors.ts`.

[CONSTRAINTS]
- No new dependencies.
- Must be a server component (no `use client`).
- Tailwind only, no CSS modules.

[GOAL - put last]
Create `Card.tsx` matching `Button.tsx` style.
Props: title (string), body (ReactNode), variant ("default" | "muted").

First-run exercise

  1. Take a task you already did well last week with a manually written prompt.
  2. Rewrite the prompt following the order above: context first, goal last.
  3. Run both versions on the same model. Compare which first draft you would actually use.
  4. Note which context lines the second version used vs. ignored, and trim accordingly.

Quality check

  • Did the model use the version numbers you specified? Look for ?? (newer JS) vs. || (ES5) tells.
  • Did the output follow your attached example file’s conventions, or its own training preferences?
  • Did the model reference any function or import that does not exist? Hallucinated dependencies are the loudest sign your context was incomplete.
  • Did you have to add details in a follow-up message? Move those into the original prompt next time.

How to reuse this workflow

  • Build a context template per repeating task: [FRAMEWORK_BLOCK] [FILE_BLOCK] [CONVENTIONS_BLOCK] [GOAL]. Fill in variables.
  • Save your best prompts in the repo at .ai/prompts/ so the whole team uses the same context recipe.
  • When context exceeds the model’s comfortable window, summarize the less-relevant pieces (e.g., “we use Tailwind with these custom utilities: …”) rather than attaching whole files.

Adding a new React component: include framework versions plus one existing component (as example for style) plus the goal. Skip the entire CSS module unless the goal touches styles. Get a usable first draft in one round. Adding a database migration: include the migration tool version, the relevant existing migration, the schema for the touched tables, and skip everything else. Get a usable first draft.

Common mistakes

  • Pasting the whole file when only one function matters. Wastes context and dilutes attention to the relevant lines.
  • Describing conventions in prose (“we use camelCase except for constants which are SCREAMING_SNAKE_CASE except inside React components where…”) when one example file shows the convention clearly in 30 lines.
  • Putting the goal at the top and code at the bottom. Reverse it — the goal should be the last thing the model reads before generating.
  • Forgetting to mention version numbers. You get ES5 syntax in an ES2024 codebase, or React class components in a hooks codebase.
  • Attaching documentation for features you do not actually use. The model will dutifully try to use them and you will have to delete the code.
  • Skipping success criteria. “Make this faster” with no benchmark is a recipe for plausible-looking changes that do not measurably help.

Advanced tips

  • For repeated tasks, build a context template and store it in .ai/templates/. Onboarding new developers gets faster.
  • When context exceeds the model’s comfortable window (around 50k tokens of code for most models), summarize the less-relevant pieces rather than attaching whole files.
  • For unfamiliar libraries, paste their README plus the exact function signature you need. Hallucinations drop dramatically.
  • Use a smaller, faster model for context triage (“which of these 12 files is relevant to this task?”) and a larger model for the actual code generation. Cheaper and often better.

Output checklist

  • Language and framework version stated.
  • Existing code attached or summarized, not described in prose.
  • Conventions shown by example, not by prose.
  • Constraints listed explicitly with their reasons.
  • Goal at the end, after all context.
  • No filler context that does not change the answer.

FAQ

  • How much context is too much?: When the model starts ignoring parts of it, or when latency feels painful, you have passed the line. For Claude and GPT, code-heavy prompts start to degrade above roughly 50k tokens. Less is usually more.
  • Should I include tests?: Yes if the goal is to make a specific test pass; attach that test. Otherwise skip — tests bloat context and the model often “fixes” the wrong thing.
  • Inline code or file attachment?: File attachment for anything over 30 lines. Models handle attached files better than long pasted blocks because tokenization is cleaner.
  • Do I include README content?: Only the relevant section. A 500-line README pasted whole wastes context; one paragraph about the convention you want followed is the right amount.
  • What about chat history?: Long chat history quietly costs context. Start a new chat when switching tasks instead of letting the previous one bleed in.
  • Does prompt order really matter that much?: Yes, measurably. The model treats early context as background facts and late context as the active task. Reversing the order changes output quality.

Tags: #AI coding #Tutorial #Workflow