AD SPACE AVAILABLE · BRAND PARTNERS WELCOME

How to Debug Inaccurate or Hallucinated Responses in Sourcegraph Cody on Unfamiliar Codebases

Daniel Kräher · 365Finds Editorial
Indexed 88,650 Wiki articles

Put 365 Wiki to work with your AI

Copy the prompt below, paste it into the AI you already use, then ask your question.

Download full Skill

Try it with the prompt first. If you use 365 Wiki often, install the full 365Wiki Skill so you do not have to paste it again. No regular AI? Gemini or Microsoft Copilot on the web will work.

365Wiki Skill

You clone a 400,000-line repository you have never seen before. You open Sourcegraph Cody in VS Code and type: "How does our session middleware handle token expiration?"

Seconds later, Cody serves up a clean, authoritative explanation. It quotes a class called SessionTokenValidator and points you toward a method named verifyAndRevoke(). You run a global search for SessionTokenValidator.

Zero results. The file does not exist. The class does not exist. The entire answer was an elaborate, confident fiction.

This is the classic failure mode when exploring unfamiliar codebases with AI assistants. Sourcegraph Cody is designed specifically to solve this problem by anchoring LLM generations in actual repository context. But when Cody cannot find what it needs, it does not stop. It falls back silently to the underlying LLM's parametric memory, hallucinating patterns that look like typical industry standards instead of your actual code.

At 365Finds, we consider Sourcegraph Cody one of the most capable code-intelligence tools on the market. But treating it like an omniscient oracle in an unknown repository will burn hours of debugging time. When Cody hallucinates, the issue is almost never that Claude or GPT got confused—it is that Cody’s context retrieval pipeline failed before the model even generated its first token.

Here is how we debug and eliminate hallucinated answers when onboarding onto unfamiliar codebases.


Step 1: Open the Context Drawer Before Reading the Answer

Whenever Cody returns an answer that feels slightly generic, stop reading the prose. Look directly above or below your prompt in the chat window for the Context chip or dropdown row.

Click it open. This is your primary diagnostic pane.

Cody displays every file chunk, symbol, and search result it fed into the model prompt. In an unfamiliar codebase, you will usually find one of three issues:

  1. Zero context items retrieved: Cody found no relevant matches in your workspace and simply asked the LLM to guess based on standard framework conventions.
  2. Irrelevant context poisoning: The retrieval heuristic grabbed unit test mocks, legacy migration files, or documentation from an unrelated microservice in your monorepo, crowding out the real implementation.
  3. Truncated context: Cody grabbed the right file, but the relevant function was cut off due to prompt token limits.

If the context drawer does not contain the actual source files where the business logic resides, whatever Cody writes next is pure statistical guesswork.


Step 2: Enable Verbose Logging to Expose Retrieval Failures

When Cody fails to pull context from a repository you do not yet understand, you need to see what its retrieval engine is doing behind the scenes.

  1. Open your IDE settings (VS Code or JetBrains).
  2. Search for Cody > Debug: Verbose (or set "cody.debug.verbose": true).
  3. Open your IDE Output panel and select Cody by Sourcegraph from the dropdown.

Now repeat your query and watch the log stream. Look specifically for these critical failure indicators:


Step 3: Check Enterprise Context Filters and Exclusion Boundaries

In unfamiliar enterprise repositories, you might assume Cody has full visibility across the codebase when it actually does not.

Enterprise instances of Sourcegraph Cody rely on Context Filters configured by site administrators (cody.contextFilters). If your security team configured exclusion rules matching certain directories (e.g., **/auth/**, **/billing/**, or proprietary algorithms), Cody is hard-blocked from indexing or retrieving snippets from those paths.

Check the diagnostic output log for:

file is ignored (due to cody.contextFilters Enterprise configuration setting)

When Cody hits an excluded directory, it will not explain that it lacks permission unless prompted. It will simply proceed with whatever partial context it has, inventing the missing architecture.


Step 4: Stop Asking Open Questions; Pin Context with @-mentions

The fastest way to trigger hallucinations in an unfamiliar repository is asking broad, architecture-level questions without anchoring. A prompt like "How does error reporting work in this service?" forces Cody's search heuristic to make wild guesses across thousands of files.

Take manual control of the context pipeline using Cody’s @-mention primitives:

By explicitly providing 2 or 3 anchor files, you prevent Cody's automated retrieval from grasping at unrelated files across the workspace.


Step 5: Force the LLM into Grounded Extraction Mode

Even with clean context, generative models have an instinct to fill in conversational blanks. If a codebase uses an unorthodox architectural style, the model may attempt to "correct" it by suggesting standard patterns that do not exist in your repo.

At 365Finds, we enforce a strict, defensive prompting pattern when investigating unfamiliar code. When you suspect Cody is fabricating details, wrap your request in explicit grounding guardrails:

You are analyzing an unfamiliar codebase. Answer the question using ONLY the provided context snippets.

Rules:
1. Do not assume standard library patterns or standard framework conventions if they are not explicitly present in the context.
2. If the implementation details or methods are not found in the retrieved context, state clearly: "NOT FOUND IN RETRIEVED CONTEXT". Do not invent function names, classes, or configuration keys.
3. Every claim must include an exact file path and line number reference from the retrieved context.

Question: Where are database connection retries configured in @file:src/database/pool.ts?

This prompt structure cuts through the model's desire to be helpful. It forces Cody to behave as an index reader rather than a generative writer.


Step 6: Verify Inline Citations and Test the Model Switcher

Cody renders clickable file citations inside its responses. Treat unlinked symbol names as immediate red flags. If Cody claims that TokenManager.cleanup() handles background purges, hover over or click the citation. If Sourcegraph cannot navigate to that symbol in your repository, do not trust it.

Finally, do not hesitate to swap the underlying model. Sourcegraph Cody allows switching between multiple foundation models in the chat header (such as Claude 3.5 Sonnet, Claude 3 Opus, or GPT-4o).

In our evaluations at 365Finds, reasoning quality over sprawling context chunks varies dramatically between model families. When unraveling tangled, unfamiliar legacy systems, Claude 3.5 Sonnet consistently demonstrates tighter adherence to retrieved context boundaries and significantly lower hallucination rates than lighter, faster models.


References