Why Your Code Gets Overwritten by AI Agents

Developers frequently encounter a frustrating scenario where an AI coding agent, after being asked to implement a feature, scans the entire codebase and silently rewrites files they just spent minutes manually editing. This behavior is not a bug; it is a feature of how agentic workflows prioritize consistency over incremental change. When an agent determines that the current state of a file diverges from its internal plan, it assumes the manual edit was a temporary deviation and corrects it to match the expected structure.
The root cause usually lies in how the agent interprets context and scope. Unlike a human developer who understands the intent behind a quick fix, an agent often treats the codebase as a static snapshot that needs to be brought into alignment with its generated plan. If the agent’s instructions do not explicitly account for existing local modifications, it will overwrite them, leading to lost work and confusion. This is particularly common in workflows using tools like Cursor or Claude Code where the agent has broad read/write access to the repository.
To prevent this, you must configure the agent’s context window and provide explicit instructions that preserve manual edits. By understanding the difference between model behaviors and how agents manage state, you can tune your prompts to respect local changes. This article explains the mechanics of this behavior and provides concrete strategies to keep your manual edits intact while still leveraging the power of automated code generation. Note that while this site uses the brand name Anthropic in its domain, it is not affiliated with Anthropic.
Frequently asked questions
What is an agentic coding tool?
An agentic coding tool is a software application that uses an AI model to autonomously plan, write, and execute code changes across multiple files. Unlike simple autocomplete, these agents can read the entire codebase, understand dependencies, and make structural changes. They operate in a loop: plan, execute, verify, and repeat until the task is complete, often overwriting files to maintain consistency.
How does an AI agent differ from a human editor?
A human editor understands context, intent, and temporary states. An AI agent typically treats the codebase as a set of files to be optimized according to a plan. If a file’s current content does not match the agent’s expected state, it will rewrite the file to align with its plan, assuming the previous state was incorrect or incomplete. This lack of nuanced understanding leads to overwriting manual edits that the agent did not explicitly account for.
Why do agents overwrite files I just edited?
Agents overwrite files because they prioritize global consistency over local changes. When an agent generates a plan, it often assumes a specific structure for each file. If your manual edit deviates from that structure, the agent views it as an error or an outdated state and rewrites the file to match its generated plan. This is especially common when the agent has broad read/write access and no explicit instruction to preserve existing changes.
What is the context window in coding agents?
The context window is the limit of text an AI model can process in a single request. In coding agents, this determines how much of the codebase the agent can see at once. If the context window is too small, the agent may miss your manual edits. If it is too large, the agent may become overwhelmed by irrelevant information. Properly managing the context window ensures the agent sees your manual edits and respects them in its plan.
Which is better for preserving manual edits: Cursor or Claude Code?
Neither is inherently better; both depend on configuration. Cursor offers granular control over context and rules, while Claude Code provides a more streamlined, opinionated workflow. The key is not the tool, but how you configure the agent’s instructions to explicitly state that manual edits should be preserved unless explicitly overridden. Both tools can achieve this with the right prompts.
How do I stop the agent from rewriting my files?
You can stop the agent from rewriting files by providing explicit instructions in your prompt. Use phrases like 'do not overwrite existing changes' or 'preserve manual edits in file X.' Additionally, configure the agent’s context to include only relevant files, reducing the likelihood of the agent overwriting unrelated changes. Understanding model differences helps in choosing the right approach.
What is the role of system prompts in preventing overwrites?
System prompts define the agent’s behavior and priorities. By including instructions like 'preserve manual edits' or 'do not rewrite files unless necessary,' you guide the agent to respect your changes. System prompts are powerful because they apply to all subsequent interactions, ensuring consistency. Hidden safety instructions can also influence behavior, so clear directives are essential.
How do I configure context to prevent unwanted overwrites?
Configure context by limiting the files the agent reads and writes. Use ignore patterns to exclude generated files or temporary directories. Explicitly list the files you have edited in your prompt, telling the agent to focus only on new changes. This reduces the agent’s tendency to rewrite existing files and ensures it only modifies the parts of the codebase that require attention.
Understanding the Agent’s Workflow
Agentic coding tools operate in a loop that begins with planning and ends with execution. The agent first analyzes the task, breaks it down into steps, and identifies the files that need to be modified. It then reads the current state of those files, generates the necessary code changes, and writes them back to the filesystem. This process is efficient but assumes that the current state of the files is either correct or needs to be aligned with the plan.
The problem arises when the agent’s plan does not account for manual edits. If you have manually modified a file, the agent may view that modification as a deviation from its plan. Since the agent prioritizes consistency, it will rewrite the file to match its expected state, effectively undoing your changes. This is not a mistake but a logical consequence of how the agent interprets the codebase as a static entity that needs to be optimized.
The Planning Phase
During the planning phase, the agent analyzes the task and generates a step-by-step plan. This plan includes the files that need to be modified and the expected structure of those files. If the agent assumes a specific structure, it will expect the files to match that structure. Any deviation, such as a manual edit, is seen as an error that needs to be corrected.
The Execution Phase
In the execution phase, the agent reads the current state of the files, generates the necessary code changes, and writes them back. If the agent detects a deviation from its plan, it will rewrite the file to align with the expected structure. This ensures consistency but can lead to the loss of manual edits that the agent did not explicitly account for.
- Plan: Analyze task and identify target files.
- Read: Load current file contents into context.
- Write: Generate and apply new code changes.
- Verify: Check if the output matches the plan.
# Example of a simple agent loop
while not task_complete:
plan = agent.plan(task)
files = agent.identify_files(plan)
for file in files:
current_content = read_file(file)
new_content = agent.generate(current_content, plan)
if new_content != current_content:
write_file(file, new_content)Why Agents Prioritize Consistency
Agents are designed to maintain consistency across the codebase. When an agent generates a plan, it assumes that the codebase will be in a specific state. If a file’s current content does not match this state, the agent assumes that the file is outdated or incorrect. This assumption is logical from the agent’s perspective, as it prioritizes the overall structure of the codebase over individual changes.
This behavior is particularly common in agentic workflows where the agent has broad read/write access to the repository. The agent does not distinguish between manual edits and generated changes; it simply sees a file that does not match its plan. As a result, it overwrites the file to ensure that the codebase remains consistent with the plan. This can be frustrating for developers who have invested time in manual edits that the agent then discards.
The Consistency Paradox
The consistency paradox arises because agents prioritize global consistency over local changes. While this ensures that the codebase remains coherent, it can lead to the loss of manual edits that the agent did not explicitly account for. Developers must find a balance between consistency and flexibility to prevent unwanted overwrites.
The Role of Context
Context plays a crucial role in how agents interpret changes. If the agent’s context window is too small, it may miss manual edits. If it is too large, it may become overwhelmed by irrelevant information. Properly managing the context ensures that the agent sees your manual edits and respects them in its plan, reducing the likelihood of overwrites.
- Global consistency ensures all files align with the plan.
- Local changes are seen as deviations if not explicitly noted.
- Rewriting ensures the final output matches the expected structure.

Configuring Context to Preserve Edits
One of the most effective ways to prevent agents from overwriting manual edits is to configure the context window. By limiting the files the agent reads and writes, you reduce the likelihood of the agent encountering and rewriting your changes. This can be done by using ignore patterns to exclude generated files or temporary directories, ensuring that the agent focuses only on the relevant parts of the codebase.
Additionally, you can explicitly list the files you have edited in your prompt. By telling the agent which files have been manually modified, you provide it with the information it needs to preserve those changes. This approach ensures that the agent does not view your manual edits as deviations but as intentional changes that should be respected.
Using Ignore Patterns
Ignore patterns allow you to exclude specific files or directories from the agent’s context. By excluding generated files or temporary directories, you reduce the noise in the context window, allowing the agent to focus on the relevant parts of the codebase. This minimizes the risk of the agent overwriting files that it does not need to touch.
Explicitly Listing Edited Files
Explicitly listing the files you have edited in your prompt provides the agent with clear instructions on which files to preserve. This approach ensures that the agent does not view your manual edits as deviations but as intentional changes that should be respected. It also helps the agent prioritize its attention on the parts of the codebase that require modification.
{
"ignore_patterns": [
"node_modules/**",
"dist/**",
"*.min.js"
],
"explicit_files": [
"src/main.py",
"src/utils.py"
]
}System Prompts and Instructions
System prompts define the agent’s behavior and priorities, making them a powerful tool for preventing unwanted overwrites. By including instructions like 'preserve manual edits' or 'do not rewrite files unless necessary,' you guide the agent to respect your changes. These instructions apply to all subsequent interactions, ensuring consistency in how the agent handles your codebase.
System prompts are particularly effective because they set the baseline behavior for the agent. Unlike ad-hoc instructions that may be ignored, system prompts are processed at the beginning of each interaction, ensuring that the agent starts with the correct context. This reduces the likelihood of the agent overwriting manual edits and ensures that your changes are respected throughout the workflow.
Crafting Effective Prompts
Effective prompts are clear, concise, and specific. Use phrases like 'do not overwrite existing changes' or 'preserve manual edits in file X' to guide the agent’s behavior. The more specific you are, the more likely the agent is to respect your changes. Avoid vague instructions that leave room for interpretation.
The Impact of System Prompts
System prompts have a significant impact on how agents interpret and execute tasks. By setting clear expectations, you can prevent the agent from overwriting manual edits and ensure that it focuses on the parts of the codebase that require modification. This leads to a more efficient and predictable workflow, reducing the frustration of lost changes.
- Be explicit about which files to preserve.
- Use negative constraints to prevent unwanted actions.
- Repeat key instructions in system prompts for consistency.

Managing Context Windows
Context windows determine how much of the codebase an agent can process in a single request. If the context window is too small, the agent may miss manual edits, leading to overwrites. If it is too large, the agent may become overwhelmed by irrelevant information, reducing its ability to focus on the task at hand. Properly managing the context window is essential for preventing unwanted overwrites.
To manage context windows effectively, limit the files the agent reads and writes. Use ignore patterns to exclude generated files or temporary directories, ensuring that the agent focuses only on the relevant parts of the codebase. This reduces the likelihood of the agent encountering and rewriting your changes, leading to a more stable and predictable workflow.
Optimizing Context Size
Optimizing context size involves balancing the need for comprehensive information with the risk of overwhelming the agent. By limiting the files the agent reads, you ensure that it focuses on the relevant parts of the codebase. This reduces the likelihood of the agent overwriting files that it does not need to touch.
Reducing Noise
Reducing noise in the context window involves excluding irrelevant files and directories. By using ignore patterns, you can ensure that the agent focuses only on the parts of the codebase that require attention. This leads to a more efficient workflow and reduces the risk of unwanted overwrites.
# Example of filtering context
relevant_files = [f for f in all_files if f not in ignore_patterns]
context = read_files(relevant_files)
agent.process(context, task)Comparing Agent Tools
Different agentic coding tools offer varying levels of control over context and behavior. Cursor offers granular control over context and rules, allowing developers to fine-tune how the agent interacts with the codebase. Claude Code provides a more streamlined, opinionated workflow, which may be less flexible but easier to use for simpler tasks. The choice between tools depends on your specific needs and preferences.
Both tools can achieve the goal of preserving manual edits with the right configuration. The key is not the tool itself, but how you configure the agent’s instructions and context. By understanding the strengths and limitations of each tool, you can choose the one that best fits your workflow and ensures that your manual edits are respected.
Cursor’s Granular Control
Cursor offers granular control over context and rules, allowing developers to fine-tune how the agent interacts with the codebase. This flexibility is ideal for complex workflows where precise control over the agent’s behavior is required. However, it may require more effort to configure effectively.
Claude Code’s Streamlined Approach
Claude Code provides a more streamlined, opinionated workflow, which may be less flexible but easier to use for simpler tasks. This approach is ideal for developers who prefer a hands-off experience and want to focus on writing code rather than configuring agents. It reduces the cognitive load of managing context and rules.
- Cursor: High control, steeper learning curve.
- Claude Code: Low control, easy to use.
- Both: Require explicit instructions for best results.

Best Practices for Preservation
To ensure that manual edits are preserved, follow best practices such as explicitly listing edited files, using ignore patterns, and crafting effective system prompts. These strategies reduce the likelihood of the agent overwriting your changes and ensure that it focuses on the parts of the codebase that require modification. By combining these approaches, you can create a stable and predictable workflow that respects your manual edits.
Regularly review the agent’s behavior and adjust your configuration as needed. If you notice that the agent is still overwriting files, refine your prompts or context settings to better align with your needs. This iterative process ensures that your workflow remains efficient and that your manual edits are preserved throughout the development process.
Iterative Refinement
Iterative refinement involves regularly reviewing the agent’s behavior and adjusting your configuration as needed. If you notice that the agent is still overwriting files, refine your prompts or context settings to better align with your needs. This ensures that your workflow remains efficient and that your manual edits are preserved.
Balancing Flexibility and Control
Balancing flexibility and control involves finding the right level of granularity in your context and rules. Too much control can lead to complexity, while too little can result in unwanted overwrites. By finding the right balance, you can create a workflow that is both flexible and predictable.
{
"system_prompt": "Preserve manual edits in src/main.py. Do not overwrite unless necessary.",
"context": {
"include": ["src/main.py", "src/utils.py"],
"exclude": ["node_modules/**"]
}
}Common Mistakes to Avoid
One common mistake is relying solely on the agent’s default behavior without providing explicit instructions. Agents assume a specific structure for files, and if your manual edits deviate from that structure, they will be overwritten. Providing explicit instructions ensures that the agent respects your changes and does not view them as deviations.
Another mistake is ignoring the context window. If the context window is too small, the agent may miss manual edits. If it is too large, the agent may become overwhelmed by irrelevant information. Properly managing the context window ensures that the agent sees your manual edits and respects them in its plan, reducing the likelihood of overwrites.
Ignoring Explicit Instructions
Ignoring explicit instructions leads to the agent overwriting manual edits. By providing clear instructions like 'preserve manual edits,' you guide the agent to respect your changes. This reduces the likelihood of the agent viewing your edits as deviations and overwriting them.
Mismanaging Context Windows
Mismanaging context windows leads to the agent missing or overwhelming itself with information. By properly configuring the context, you ensure that the agent sees your manual edits and respects them in its plan. This leads to a more stable and predictable workflow, reducing the risk of lost changes.
- Always provide explicit instructions for critical files.
- Use ignore patterns to reduce noise.
- Review agent behavior regularly and adjust as needed.

Test the endpoint now with no account required.