All posts

article

Mastering Context Sizing — The Hidden Variable in AI-Native Engineering

Larger contexts don't make models smarter — they make them more confidently wrong. Here's the physics of attention decay, why your AI pipeline is hemorrhaging signal, and how to engineer around it.

§ 01 — The Setup: Context Is Compute

When engineers first encounter large language models, the context window feels like free real estate. A 200,000-token window? Throw everything in. The whole codebase. Every design doc. The last six months of Slack threads. If the model can see it, the thinking goes, it can use it.

This intuition is almost exactly backwards.

Context windows are not filing cabinets where you deposit information for later retrieval. They are the entirety of the model’s working memory — and like all working memory, they degrade under load. The more you pack in, the thinner the model spreads its attention. Signal competes with noise. Critical facts get outweighed by voluminous irrelevance. And the model, lacking any ability to tell you it’s overwhelmed, does what LLMs always do when uncertain: it confabulates with confidence.

This is not a temporary limitation waiting to be engineered away in the next model version. It is a structural property of transformer attention. Understanding it is table stakes for anyone building AI-native software development workflows in 2026.

“The model cannot tell you it’s overwhelmed. It just starts sounding like someone who read the SparkNotes instead of the book — fluent, plausible, and subtly wrong.”

§ 02 — Attention Physics: Why Large Contexts Hallucinate More

To understand context-induced hallucination, you need a working model of how transformers allocate attention. Every token in the context window attends to every other token — but attention is a zero-sum game. The total attention budget is fixed. When context doubles, the average attention any single token receives is cut roughly in half.

The Primacy / Recency Effect

Empirical research consistently shows that models disproportionately weight tokens at the beginning and end of a context window. Content buried in the middle — the “lost in the middle” phenomenon documented across GPT-4, Claude, and Gemini families — receives statistically less attention during generation. For software engineering tasks, this is catastrophic: your most important architectural constraint, buried 40,000 tokens into a 128k context, may as well not exist.

// Conceptual attention weight distribution across context positions
// Normalized; actual curves vary by model architecture

Position:    START ████████████████  weight: ~0.28
             EARLY ████████████      weight: ~0.21
             MID-A ████              weight: ~0.08  ← danger zone
             MID-B ████              weight: ~0.07  ← danger zone
             MID-C █████             weight: ~0.09  ← danger zone
             LATE  ██████████         weight: ~0.18
             END   ████████████████  weight: ~0.26

// Key insight: "MID" zone = ~40–75% of tokens in a typical 128k window
// This is where most of your codebase files live when you naively stuff context
    

Fig. 1 — Conceptual attention weight curve. Critical constraints buried in the middle receive a fraction of the attention given to system prompt and most-recent message.

The Distractor Problem

Beyond positional bias, context pollution introduces a more insidious failure mode: distractor tokens. When you load a repository’s entire source tree into context to ask a focused question about one module, the model must differentiate relevant from irrelevant information. Transformers do not have an explicit “ignore this” mechanism. Every token participates in attention computation. Highly similar but ultimately irrelevant code — a different service that shares naming conventions, for instance — actively competes with the relevant code for the attention weights that shape the response.

Empirical finding: Studies on long-context retrieval show accuracy on embedded fact questions drops from ~95% at 1k tokens to below 60% at 100k tokens, even when the answer is present verbatim in the context. The model doesn’t lose access to the fact — it loses the ability to weight it appropriately during generation.

Hallucination as Attention Interpolation

When a model cannot find high-confidence signal for a generation step, it interpolates from its parametric memory — the knowledge baked in during pretraining. In short contexts, this is rarely the problem path; the context dominates. In large, noisy contexts, the model constantly falls back to interpolation without signaling that it has done so. The resulting text sounds like accurate retrieval but is partially or wholly fabricated.

For SDLC tasks, this manifests as: invented API signatures that almost match your SDK, fabricated function parameters, plausible-but-wrong config values, and dependency version hallucinations that pass casual review but fail at runtime.

Context SizeNoise Ratio*Distractor RiskHallucination Risk
< 4k tokensLowMinimalLow
4k – 16kModerateManageableModerate
16k – 64kHighSignificantHigh

64k| Very High| Severe| Very High

*Noise ratio = proportion of context tokens not directly relevant to the immediate generation task.

§ 03 — The SDLC Problem: Where Context Breaks Your Pipeline

Software development is, at its core, a long-chain reasoning task. A feature request travels through requirements, architecture, interface design, implementation, testing, and deployment — each step building on the last, each consuming and producing structured knowledge. This is precisely the kind of multi-step, high-stakes workflow where context mismanagement causes compounding failures.

Stage 1: Requirements → Architecture

The failure often starts here. Engineers prompt an AI to generate architecture from a PRD, helpfully including “everything the model might need” — the PRD, existing architecture docs, relevant ADRs, API contracts, team conventions, sprint context. The resulting context is 60,000+ tokens of partially-relevant documentation.

The model produces an architecture that looks comprehensive. But the non-functional requirements buried on page 12 of the PRD — the latency SLA, the data residency constraint, the specific retry budget — received minimal attention weight. They don’t appear in the architecture output. No one notices until load testing, or worse, until a compliance audit.

── SIGNAL LOSS ACROSS AI-ASSISTED SDLC STAGES ─────────────────

  Requirements     ████████████████████  100% signal preserved
  (raw doc, 8k)

  Architecture     ███████████████░░░░░   75% — latency SLA missed
  (AI, ~60k ctx)

  Interface Spec   █████████████░░░░░░░   65% — error states thinned
  (AI, ~80k ctx)

  Implementation   ████████░░░░░░░░░░░░   40% — auth edge cases lost
  (AI, ~100k ctx)

  Test Generation  ██████░░░░░░░░░░░░░░   30% — tests for phantom spec
  (AI, ~120k ctx)
──────────────────────────────────────────────────────────────────
  Each stage inherits and amplifies prior signal loss.
  By test generation, the AI is partly testing an imagined system.
    

Fig. 2 — Compounding signal loss. Each SDLC stage consumes the prior stage’s output plus accumulated context, growing context size and degrading fidelity.

Stage 2: Specification Drift

AI-assisted spec writing compounds the problem. When the model is asked to generate an interface spec from the architecture, it receives a context that now includes the (already-degraded) architecture output plus reference material. The specification fills in gaps using parametric memory — standard patterns, typical API shapes, conventional error codes — rather than the actual requirements. The spec feels complete. Engineers accept it without noting what was never in the source PRD.

Warning pattern: The most dangerous hallucinations in SDLC contexts are not obviously wrong — they are plausible defaults. When an AI invents an error handling strategy or a default timeout value, it picks something reasonable. That “reasonable” value may conflict with your infrastructure constraints in ways that only surface under specific load conditions.

Stage 3: Implementation Context Explosion

By the implementation stage, the context chain has grown dramatically. A typical agentic coding session might include: the system prompt and tool definitions, the architecture spec, the interface spec, relevant existing source files, test fixtures, dependency manifests, and recent git history. It is routine for this to exceed 100,000 tokens.

At this scale, the model is essentially working from vibes about what your codebase does. It has seen enough to sound authoritative. It has not attended enough to be accurate. The code it generates compiles. It may even pass unit tests — tests that the model also wrote against the same degraded context it used for implementation.

// What the engineer asked for: // “Add retry logic to the payment processor using our standard backoff policy” // What the model knew from parametric memory: const BACKOFF_BASE = 1000; // milliseconds, typical default const MAX_RETRIES = 3; // What was actually in the codebase (in the middle of 80k ctx, ~12k tokens in): // config/backoff.ts → PAYMENT_BACKOFF_BASE = 250ms (SLA-driven) // config/backoff.ts → PAYMENT_MAX_RETRIES = 2 (downstream rate limit) // Result: Code compiles. Tests pass (model wrote them too). // Fails in prod when downstream hits rate limit on 3rd retry. // Incident postmortem: “AI wrote code that ignored our config.”

Stage 4: Test Generation Against a Ghost Spec

The cruelest failure mode is test generation. When an AI writes tests for code it also wrote, using the same degraded context, the tests validate the implementation’s behavior rather than the original requirements. The test suite goes green. Confidence is high. The product ships with a comprehensive test suite that proves only that the code is internally consistent — not that it does what the business needed.

This is context-induced hallucination completing the loop: the system has built a coherent, internally validated fiction of the intended software.

§ 04 — The Mechanics: Context Failure Taxonomy

Not all context failures are equal. Understanding the distinct failure modes allows you to apply targeted mitigations rather than blanket context reduction.

Type I — Attention Dilution

The target information is present in context but receives insufficient attention weight to significantly influence generation. Root cause: context volume. Mitigation: context reduction, information front-loading.

Type II — Distractor Interference

Semantically similar but functionally different content competes with the target. Root cause: irrelevant-but-related content. Mitigation: aggressive context filtering, retrieval specificity.

Type III — Positional Burial

Critical information sits in the attention-poor middle of a long context. Root cause: context structure, not volume. Mitigation: restructure context with critical constraints at start/end.

Type IV — Parametric Fallback

Model cannot find confident signal in context and substitutes pretrained priors. Root cause: context gaps + high generation pressure. Mitigation: explicit uncertainty prompting, retrieval augmentation, staged generation.

Type V — Compounding Inheritance

Output of one AI stage becomes noisy input to the next, with hallucinations treated as ground truth. Root cause: pipeline design. Mitigation: human review gates, structured output validation, context resets between stages.

FAILURE TYPE DETECTION DIFFICULTY BLAST RADIUS Type I Dilution Medium Single task Type II Distractor Hard Single task Type III Positional Hard Single task / session Type IV Parametric Very Hard Propagates downstream Type V Compounding Extremely Hard Entire SDLC stage ← most dangerous

§ 05 — Engineering Remedies: How to Size Context Properly

Context sizing is not about finding the smallest window — it’s about maintaining a high signal-to-noise ratio throughout every AI interaction in your pipeline. Here are the engineering disciplines that matter.

Principle 1: The Minimum Viable Context

For every AI interaction, identify the minimum set of information required to complete that specific task. Not what might be useful — what is necessary. This discipline produces dramatically better outputs and forces you to think clearly about what you’re actually asking the model to do.

// Anti-pattern: Load everything “just in case” const context = [ systemPrompt, entireCodebase, // ❌ 40,000 tokens of noise allDesignDocs, // ❌ 20,000 tokens, 90% irrelevant sixMonthsOfGitLog, // ❌ 30,000 tokens, nearly all noise userRequest ]; // Pattern: Minimum viable context const context = [ systemPrompt, // ✅ Role + constraints targetModule, // ✅ The file being modified (~500 tokens) directDependencies, // ✅ Only imported interfaces (~800 tokens) relevantADR, // ✅ The specific decision governing this change userRequest // ✅ Clear, scoped task ]; // Total: ~2,500 tokens. Signal density: high.

Principle 2: Structure Your Context for Primacy

Given that models over-weight the start and end of context, put your most critical constraints where attention is highest. Non-negotiable requirements, hard constraints, and the specific task go first. Supporting reference material follows. The user’s immediate request is last — at peak attention weight.

// Optimal context structure for SDLC tasks [1] SYSTEM PROMPT ← Critical role, rules, output format [2] HARD CONSTRAINTS ← SLAs, compliance rules, architectural boundaries [3] TASK-SPECIFIC FILES ← Only directly relevant code/docs [4] INTERFACES/CONTRACTS]← Type signatures, API shapes the task touches [5] REFERENCE MATERIAL] ← Supporting context (keep minimal) [6] SPECIFIC REQUEST] ← Exact task, at end = peak attention

Principle 3: Stage and Reset

Agentic SDLC pipelines should treat context as a resource that expires. At each major phase transition — requirements to architecture, architecture to spec, spec to implementation — perform a context reset. Distill the output of the prior stage into a structured, compact summary. Begin the next stage from that summary, not from the accumulated conversation history.

This breaks the Type V compounding failure mode. Each stage starts with a clean, high-density context rather than an ever-growing, ever-noisier conversation window.

Principle 4: Retrieval Over Stuffing

When tasks require access to large corpora — entire codebases, large documentation sets — use retrieval-augmented generation rather than context stuffing. A well-tuned retrieval layer returns the 3–5 most relevant chunks for any query. This keeps context tight and signal density high, regardless of how large the underlying corpus is.

Principle 5: Explicit Uncertainty Elicitation

Prompt the model to explicitly flag when it is uncertain or when context doesn’t contain information needed to proceed confidently. This surfaces Type IV failures before they propagate. A model instructed to say “Context does not contain X; proceeding with assumption Y — please verify” is far safer than one that silently invents X.

// Add to system prompt for SDLC agents: “If the provided context does not contain sufficient information to confidently complete a step, explicitly state: ASSUMPTION REQUIRED: [what you’re assuming] — [what context would be needed to verify this]. Do not proceed silently from assumptions. Surface them.”

Principle 6: Context Budgeting

Treat context tokens like compute budget. Set explicit limits for each component of your context: system prompt gets N tokens, task-specific code gets M tokens, reference material gets P tokens. When retrieval returns more than the budget, truncate or summarize. This discipline prevents the gradual context bloat that infects most agentic pipelines over time.

01

Minimum Viable Context

Load only what is necessary for the exact task. Question every token.

02

Primacy Engineering

Hard constraints first. User request last. Reference material in the forgettable middle.

03

Stage Resets

Distill and restart at each SDLC phase boundary. Never let context compound indefinitely.

04

RAG over Stuffing

Retrieve semantically relevant chunks. Don’t load full documents to find one function.

05

Elicit Uncertainty

Prompt the model to surface assumptions. Don’t let it hallucinate silently.

06

Token Budgeting

Treat context components as budgeted resources. Enforce limits. Prevent bloat.

§ 06 — The Bigger Picture: Context Discipline as Competitive Moat

The irony of the current AI tooling landscape is that teams with the best models often don’t get the best outputs. The teams winning with AI in 2026 have figured out something counterintuitive: discipline beats capability. A well-orchestrated pipeline using focused, scoped, context-aware prompts consistently outperforms a naive pipeline with a more powerful model.

Context sizing is a first-class engineering discipline. It should be codified in your team’s AI usage guidelines, audited in pipeline reviews, and treated with the same rigor as memory management or cache invalidation. Because that’s exactly what it is: memory management for a probabilistic machine.

The teams that understand this — that context is not free storage but a precision instrument — will build AI-augmented workflows that are genuinely reliable. The teams that don’t will spend their debugging cycles wondering why the model “forgot” a constraint it was clearly given.

“A 4,000-token context with 95% signal density will outperform a 128,000-token context at 15% signal density. Every time. This is not about model capability — it’s about basic information physics.”

Build your AI SDLC with this in mind. Treat context as a depletable resource. Design your pipelines around context hygiene. Establish human review gates at phase boundaries where context has been allowed to grow. And instrument your pipelines to measure context size and estimated signal density over time — the way you’d measure memory utilization or p99 latency.

The context window is your model’s entire world at the moment of generation. Make sure that world contains exactly what you need it to, nothing more, and nothing less.

↓60%

Accuracy drop on embedded facts from 1k → 100k token context

More likely to use parametric fallback in noisy 100k+ contexts vs. clean 4k contexts

Key Failure Types

  • Attention Dilution
  • Distractor Interference
  • Positional Burial
  • Parametric Fallback
  • Compounding Inheritance

Context Budget Guidelines

System prompt: < 800 tokens
Hard constraints: < 500 tokens
Task-specific code: < 2,000 tokens
Interface refs: < 1,000 tokens
Reference material: < 1,500 tokens
User request: < 300 tokens

Target total: < 6,000 tokens
Maximum: 16,000 tokens for complex tasks

Warning Signs

  • AI output references things not in requirements
  • Tests written by same model that wrote the code
  • Context growing across conversation turns without resets
  • No human review between SDLC stages
  • “The model forgot” is a common postmortem phrase

On Agentic Pipelines

Every tool call result added to an agentic loop’s context is additional noise. Pipelines that compact tool outputs before continuing preserve signal. Pipelines that append raw tool results compound noise with each step.