Troubleshooting and Governing Context Drift in AI Agents

    Context drift happens when multiple project documents fall out of sync, causing coding agents to answer confidently with outdated values. This article explains how drift differs from context rot, why it occurs during normal collaboration, and how to fix it with layered documentation, a single source of truth, context budgets, explicit conflict disclosure, and regression tests. It includes a practical acceptance checklist for keeping agents reliable in long-running projects.

    1. Where the Problem Starts: Two Documents, Two Answers

    One refund policy document says 14 days. Another says 30 days. You ask a coding agent to answer a customer based on the project documentation, and it quickly returns a number. As the person reviewing the output, can you tell from that sentence that the agent also read a conflicting answer?

    Most likely not. The model’s output is fluent, confident, and presented as a single version. It does not include a side note saying, “I referenced document A, but document B states something different.” This is exactly what makes context-related problems so hard to catch in engineering practice: the failure does not show up as an error. It shows up as overconfidence.

    An engineer working in developer relations at a cloud provider ran a controlled experiment. In matching tasks where the task instructions conflicted with the project’s top-level rules, the model followed the task instructions 95% to 100% of the time. Across 110 runs that did not ask the model to report conflicts, it proactively mentioned the discrepancy to the user zero times.

    The scope of this data matters. It comes from two coding agents and a self-built coding sandbox task set. The sandbox contained real repository files, and the model could read documents, write code, and run tests. The conclusion cannot be generalized to every model and every business scenario, but the mechanism it reveals is universal: when multiple authoritative sources exist in the context, the model ranks them by priority but does not disclose how it ranked them by default.

    2. Two Kinds of “Context Problems” Must Be Separated

    Anyone who has used AI to write code for a while knows when to start a new session: old proposals pile up in the context, and the model keeps picking up approaches that stopped working long ago. Compressing history, clearing noise, and reopening the conversation all address the same class of problem, which can be called context rot: as a single session grows longer, accumulated historical noise drags down answer quality.

    The experiment targets a different phenomenon, which can be called context drift: multiple documents inside a project lose sync over time. The root-level AGENTS.md has already changed the refund window to 14 days, a skill file for a customer support task still says 30 days, and the docs directory still holds a legacy note from three months ago. A skill file is the operating instruction the model reads when it receives a specific task. Even in a freshly opened blank session, these files still produce different answers.

    Dimension Context Rot Context Drift
    When it triggers The longer the chat, the more old ideas interfere Can appear in the first turn of a new session
    Source of noise Accumulated session history Multiple project documents contradicting each other
    What the user sees Answers go off track, tried approaches repeat Answers flow smoothly but give only one version
    Where to start debugging Clear history, restate the task Locate the conflicting text, check which document is still valid
    How to govern it Session segmentation, context compression Single source of truth for docs, conflict detection

    Treating the two as the same problem makes “reopen the session” look like a cure-all, while the drift problem remains.

    3. How Drift Happens: No One Needs to Make a Big Mistake

    Drift is usually not the result of a careless error. It is a by-product of normal collaboration:

    • Multiple write entry points: architecture conventions live in the repository root spec, task-level instructions live in skill files, and business details are scattered across the docs directory, maintained by different people at different times.
    • Changes cover only part of the surface: a single window adjustment updates the main spec but misses the task templates and sample Q&A.
    • Examples look more “concrete” than specs: samples in old documents carry full context and read as more actionable, so they are easily treated as the source of truth.
    • No validity markers: documents have no version, effective date, or scope, so the model cannot tell which one is newer.
    • Retrieval is probabilistic: chunk recall does not guarantee “spec first,” and which segment gets hit carries randomness.

    4. A Practical Governance Plan

    1. Establish Document Layers and a Single Source of Truth

    Organize content into four layers — “global rules / domain rules / task instructions / reference material” — and make the priority explicit: global rules define constraints that cannot be violated, task instructions may only add detail where the rules leave gaps, and reference material must not carry hard-coded values.

    1. project/
    2. AGENTS.md # Global constraints: windows, compliance, naming
    3. skills/refund.md # Task instructions: reference global constants only
    4. docs/legacy/ # Archived material: excluded from recall by default

    The key practice is removing duplicate facts: instead of hard-coding 14 or 30 in the task instructions, reference a single constant.

    1. Refund window: see global spec REFUND_WINDOW_DAYS. This file does not redefine the value.

    2. Give Context a Budget and a Priority Order

    Manage context as a finite resource: set separate quotas for system specs, task instructions, retrieved snippets, and conversation history. Keep spec-type content at the top and never compress it. Exclude archived material by default. Illustrative configuration logic:

    1. CONTEXT_BUDGET = {
    2. "global_rules": 0.25, # Pinned at top, excluded from compression
    3. "task_spec": 0.25,
    4. "retrieved": 0.35, # Must carry source and effective time
    5. "history": 0.15, # Summarized once exceeded
    6. }
    7. def assemble(ctx):
    8. return [ctx.global_rules, ctx.task_spec,
    9. *ranked(ctx.retrieved), summarize(ctx.history)]

    3. Make Conflicts Explicit

    Since the model does not proactively report disagreements by default, write it into the output contract: require the conclusion first, then the conflicts.

    1. Answer format:
    2. 1) Conclusion and the file path it relies on
    3. 2) Conflicts found (file, value, last updated time)
    4. 3) The priority rule applied
    5. If an unresolved conflict exists, it must be explicitly flagged. Do not silently pick one.

    You can also add a deterministic check on the pipeline side: extract the key values from the answer, compare them against the trusted-source constants, and block the output on any mismatch. This is engineering validation, not model self-discipline.

    4. Guard Against Drift with Regression Cases

    Turn “old and new documents contradict each other” into a fixed test case in continuous integration: after every spec change, run it automatically to check whether the agent still cites the old value and whether it discloses conflicts in the required format. Pair this with logging services and monitoring alerts, and track trends for “conflict disclosure rate” and “constant mismatch blocks” to judge whether governance is actually working.

    5. Acceptance Checklist

    • Is each key value defined in exactly one place, with all other locations referencing it?
    • Are archived and expired documents excluded from recall by default?
    • Does the output include the supporting path and conflict disclosure?
    • Is there a deterministic check as a fallback, rather than relying on model self-discipline?
    • Are conflict scenarios included in regression tests and production monitoring?

    Context drift is fundamentally a knowledge management problem, not a prompt engineering trick. When a project has multiple sources that all look authoritative, the agent will not adjudicate for you, and it will not tell you that it adjudicated. Converging the trusted source into one place and turning conflicts into observable output is the prerequisite for keeping coding agents stable in long-running projects.