$ ls ./menu

© 2025 ESSA MAMDANI

cd ../blog
10 min read
AI & Technology

The Paradox of Context: Why More Files Can Sabotage Your AI Coding Agent's Performance

Audio version coming soon
The Paradox of Context: Why More Files Can Sabotage Your AI Coding Agent's Performance
Verified by Essa Mamdani

When we interact with a new codebase or tackle a complex bug, our human instinct is to gather as much information as possible. We open multiple files, browse documentation, search commit histories, and mentally construct a comprehensive understanding of the system. It’s natural, then, to assume that providing a coding AI agent with a vast array of "context files" – entire repositories, directories, or even just numerous related files – would similarly empower it to perform better. The more information, the better the decision-making, right?

Surprisingly, and often counter-intuitively, this assumption frequently proves false. For AI coding agents, especially those powered by large language models (LLMs), providing excessive or poorly curated context can not only fail to improve performance but can actively hinder it, leading to irrelevant suggestions, increased hallucination, higher costs, and slower response times. This phenomenon reveals a fundamental difference in how humans and AI process information and underscores the need for a more strategic approach to context provision.

The Intuitive Appeal: Why We Think More Context Helps

Our inclination to flood AI agents with context stems from several understandable reasons:

  • Human Analogy: As developers, we thrive on comprehensive understanding. We build mental models of systems, cross-referencing files, understanding architectural patterns, and grasping the bigger picture. We project this need for breadth onto AI.
  • Fear of Missing Out (FOMO): There's a concern that if we don't provide a specific file or piece of information, the AI might miss a crucial detail, leading to an incorrect or incomplete solution.
  • Simplicity of Implementation: It's often easier to just dump a directory of files into the context window than to meticulously select and curate relevant snippets.
  • Belief in AI's Omniscience: We sometimes overestimate an LLM's ability to discern relevance from a sea of data, assuming it will naturally filter out the noise and identify the signal.

While these intuitions are valid for human cognition, they often clash with the operational realities and limitations of current AI models.

The Core Problem: When Context Becomes a Burden

The reasons why excessive context can hurt AI coding agent performance are multifaceted and rooted in the architecture and training of LLMs:

1. Information Overload & Cognitive Burden (for the AI)

Unlike a human who can dynamically shift focus and prioritize information based on an evolving understanding of the task, LLMs process their entire context window in a largely sequential and uniform manner. Every token in the prompt, regardless of its relevance, contributes to the computational load.

  • Distraction: Irrelevant code, verbose comments, outdated documentation, or even files from completely unrelated modules within the same repository can act as distractors. The AI might spend computational cycles trying to find patterns or connections where none exist, diluting its focus on the actual problem.
  • "Lost in the Middle" Phenomenon: Research has shown that LLMs often struggle to retrieve information accurately when it's buried deep within a very long context window. They tend to perform best when relevant information is at the beginning or end of the prompt, making the middle a "blind spot." This means that even if crucial information is present, it might be effectively invisible to the model if surrounded by too much noise.

2. Increased Noise-to-Signal Ratio

When you provide dozens of files, the sheer volume of irrelevant or low-value information can overwhelm the truly pertinent details. Imagine asking a junior developer to fix a bug in a specific module, but instead of giving them the module's code, you hand them printouts of the entire 500,000-line monorepo. They'd spend more time sifting through noise than understanding the problem. LLMs, despite their advanced capabilities, face a similar challenge. The signal (the specific code related to the task) gets drowned out by the noise (everything else).

3. Context Window Limitations & Cost Implications

Every LLM has a finite "context window" – the maximum number of tokens it can process in a single interaction. Exceeding this limit means information gets truncated, often arbitrarily. Even if you stay within the limit, larger context windows directly translate to:

  • Higher API Costs: LLM providers charge based on token usage. A prompt with 10,000 tokens of context will cost significantly more than one with 1,000 tokens, even if 90% of the longer prompt is irrelevant.
  • Slower Response Times: Processing more tokens takes more computational power and time, leading to noticeable delays in receiving responses from the AI agent. This can significantly degrade the developer experience and workflow efficiency.

4. Stale or Irrelevant Information

Codebases are living entities, constantly evolving. A file that was relevant yesterday might be outdated today. Providing broad context increases the likelihood of including:

  • Outdated Comments/Docs: Comments that no longer reflect the code's behavior.
  • Deprecated Code: Functions or modules that are no longer used or have been replaced.
  • Conflicting Definitions: Different versions of a constant or function in various parts of the codebase.

The AI doesn't inherently know which information is current or authoritative without explicit guidance. It might synthesize an answer based on stale data, leading to incorrect or non-functional code suggestions.

5. Hallucination Amplification

LLMs are prone to "hallucinating" – generating plausible but factually incorrect information. When given a vast, noisy context, the model might try to infer connections or patterns that don't exist, stitching together disparate pieces of information into a coherent but fabricated narrative. The more ambiguous or overwhelming the input, the higher the chance of the model veering off into confident but incorrect assertions, especially if it struggles to find a direct answer within the provided context.

Real-World Scenarios Where Excessive Context Hurts

Let's look at practical examples where the "more context" approach backfires:

Example 1: Debugging a Specific Bug in a Large Monorepo

The Problem: A user reports a specific UI rendering issue related to a UserProfileCard component in a large React monorepo.

The "Bad" Approach: You feed the AI agent the entire frontend/src directory, containing hundreds of components, utility files, hooks, and pages.

The Outcome:

  • Slow Response: The agent takes a long time to process all the files.
  • Irrelevant Suggestions: It might suggest changes to unrelated components, CSS files that aren't impacting UserProfileCard, or even backend API calls that are tangential to the UI bug.
  • Missed Specifics: Because of the noise, it might overlook the subtle styling conflict or incorrect prop being passed directly to UserProfileCard – the actual cause of the bug.
  • High Cost: Each interaction costs significantly more.

Example 2: Refactoring a Function in an Unfamiliar Codebase

The Problem: Refactor calculateOrderTotal() to improve readability and handle edge cases, within a legacy e-commerce application.

The "Bad" Approach: You provide the AI agent with the entire src/main/java/com/ecommerce directory, including controllers, services, repositories, and DTOs that are not directly involved in calculateOrderTotal().

The Outcome:

  • Scope Creep: The AI might suggest refactoring unrelated services or database interactions, trying to "improve" the entire system rather than focusing on the specific function.
  • Incorrect Assumptions: It might infer dependencies or design patterns from other parts of the codebase that don't apply to the specific function's context, leading to an incompatible refactor.
  • Security Risks: If the broader context contains sensitive configuration or business logic, it could be inadvertently exposed or misused in the AI's suggestions.

Example 3: Generating a New Feature in an Existing Project

The Problem: Add a new exportToCSV function to a ReportService class, following existing project conventions.

The "Bad" Approach: You give the AI agent all files related to ReportService, plus every other service, controller, and data model in the project.

The Outcome:

  • Generic Code: The AI might generate a very generic exportToCSV function that doesn't fully align with the specific project's utility libraries, error handling patterns, or data structures because it's trying to satisfy too many potential contexts.
  • Overly Complex Implementation: It might pull in unnecessary dependencies or patterns from other services, making the new function more complex than it needs to be.
  • Missed Nuances: It might fail to correctly use an existing CsvWriter utility or a specific logging framework, despite those being present in the broader context, simply because they were buried.

When Does Context Help? (The Nuance)

This isn't to say context is useless. Far from it! The key lies in selective, relevant, and concise context. When provided intelligently, context is incredibly powerful.

Context helps immensely when it is:

  • Directly Relevant to the Task: Only include files or snippets that are absolutely necessary for the current operation.
    • Example: If debugging UserProfileCard, provide UserProfileCard.js, its associated CSS/SCSS, and the parent component that renders it.
  • Focused and Concise: Instead of entire files, sometimes a function signature, an interface definition, or a specific block of code is enough.
    • Example: When implementing an API, provide only the API endpoint definition (e.g., OpenAPI spec) and the relevant data models, not the entire backend service implementation.
  • High-Value Metadata: Information that helps the AI understand the intent or purpose.
    • Example: Docstrings, comments explaining complex logic, README.md files for specific modules, or even relevant excerpts from architectural decision records.
  • Error Logs for Debugging: When debugging, providing the specific stack trace and relevant log lines is far more useful than the entire application log file.
  • Test Cases: Existing unit or integration tests for a function or module can provide excellent examples of expected behavior and input/output formats.
  • Specific Utility Functions/Helper Modules: If the task requires using a specific project utility (e.g., a custom date formatter), providing just that utility's code is beneficial.

Strategies for Effective Context Provision

The goal is to provide the AI with the minimum viable context – just enough information to complete the task accurately and efficiently, without overwhelming it.

1. Manual Curation & Selective Inclusion

The most straightforward approach is for the developer to manually select and include only the files or code snippets they believe are directly relevant.

  • Actionable Advice: Before sending a prompt, pause and ask yourself: "If I were a new developer tackling this, which 2-3 files would I absolutely need to see first?" Start there. Be ruthless in cutting out anything that isn't immediately pertinent.
  • Example: For a bug in src/components/Button.tsx, provide Button.tsx, its associated Button.module.css, and perhaps the theme.ts file if styling is involved. Do not provide the entire src/components directory.

2. Dynamic Context Retrieval (RAG Principles)

Inspired by Retrieval Augmented Generation (RAG), this involves dynamically fetching relevant information based on the user's query and the current task. This is where more sophisticated AI agents truly shine.

  • Actionable Advice: Implement or use agents that can perform semantic search over your codebase. When you ask the agent to "fix the bug in UserProfileCard," it should intelligently search for files semantically related to "UserProfileCard," "bug," and potentially "rendering," rather than just dumping a directory.
  • How it works:
    • Embeddings: Your codebase (files, functions, documentation) is chunked and converted into numerical vector embeddings.
    • Vector Database: These embeddings are stored in a vector database.
    • Query Embedding: When a user prompts the AI, the prompt is also converted into an embedding.
    • Similarity Search: The vector database finds the code chunks whose embeddings are most similar to the query embedding.
    • Context Injection: Only these top-N most relevant chunks are injected into the LLM's context window.

3. Focus on High-Value Metadata

Leverage the structured information already present in your code.

  • Actionable Advice: Ensure your code has clear function names, descriptive variable names, and well-written docstrings/comments. These act as internal "tags" that help both humans and AI understand the purpose of code segments. When generating context, prioritize these metadata-rich snippets.
  • Example: Instead of an entire file, send only the function signature and its docstring if the task is about understanding what a function does.

4. Iterative Prompting & Conversational Context

Instead of a single, massive prompt, engage the AI in a conversation. Let the AI ask for more context if it needs it, or provide it incrementally.

  • Actionable Advice: Start with a concise prompt and minimal context. If the AI's response is insufficient or it requests more information, then provide additional, targeted context in the next turn. This mimics a human pair-programming session.
  • Example:
    • You: "Refactor calculateOrderTotal to handle discount codes." (Provide only calculateOrderTotal function).
    • AI: "To do this, I need to know how discount codes are structured and where they are applied in the order object."
    • You: "Okay, here's the Order interface and the applyDiscount utility function." (Provide only those snippets).

5. Leveraging Tools and IDE Integrations

Modern IDEs and AI tools are increasingly offering features that facilitate smart context management.

  • Actionable Advice: Explore plugins and extensions for your IDE that integrate with LLMs. Many are designed to automatically identify and provide relevant context based on your cursor position, selected code blocks