AD SPACE AVAILABLE · BRAND PARTNERS WELCOME

How to Troubleshoot Context Retrieval Failures and Hallucinated Function Calls in Sourcegraph Cody

Claude Rochaix · 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

When Sourcegraph Cody invents a non-existent method, invents phantom parameters, or references imported modules that do not exist, the failure almost never originates in the underlying Large Language Model. It originates in Cody's context retrieval pipeline.

At 365Finds, we track developer AI tooling closely, and Sourcegraph Cody stands out for its architecture: rather than relying strictly on dumb vector embeddings, it combines local structural search, repository search APIs, and deep code intelligence graphs. But when that retrieval chain drops a critical type definition or interface, the LLM does not raise an error. It does what language models are trained to do: it guesses. And in code, guessing looks like plausible, syntactically elegant hallucination that breaks at compile time.

Here is the exact diagnostic workflow to pinpoint why Sourcegraph Cody missed your dependency definitions and how to eliminate hallucinated function calls.


1. Inspect What Sourcegraph Cody Actually Fed the Model

Before tweaking configuration files or restarting language servers, look at the prompt payload.

In the Cody chat window, every response displays a context pill or dropdown indicating how many files and lines of code were sent to the LLM (for example, 5 files, 142 lines of context).

  1. Click the context indicator above or below the assistant's reply.
  2. Expand the list of attached context snippets.
  3. Check whether the file containing the function definition, interface, or type signature appears in that list.

If the file is completely absent or truncated before reaching the exported symbol, Cody operated blind. The LLM synthesized the method name purely from semantic hints in your prompt and adjacent files.


2. Diagnose the Three Failure Points in the Retrieval Pipeline

Sourcegraph Cody uses a multi-tier context engine. Retrieval failures typically occur at one of three distinct layers:

A. Local Symbol Search Failure (symf)

For local repository navigation without a connected Sourcegraph Enterprise instance, Cody relies on symf—a local structural search binary running inside your IDE extension.

B. Remote Indexing and Context Filters (cody.contextFilters)

If you connect Cody to a remote Sourcegraph instance or an enterprise code graph:

C. Collateral Damage from .gitignore and .codyignore

Cody respects .gitignore by default and enforces .codyignore / .cody/ignore rules.


3. Clear Token Budget Exhaustion in Long Chat Threads

Cody operates within strict token ceilings. User-defined context and automated context retrieval are budgeted alongside conversation history.

In long, multi-turn chat conversations, accumulated chat messages consume the available prompt window. When Cody approaches the context token limit:

  1. It silently truncates or discards retrieved code chunks to fit the conversation history.
  2. Older type definitions that were present in turn 1 are pushed out by turn 8.
  3. Cody reverts to guessing interfaces it previously knew.

365Finds strongly advises against maintaining persistent, sprawling chat threads in Sourcegraph Cody. Once you finish a specific refactoring task or debugging sequence, open a fresh chat panel (Ctrl+L or Cmd+L). A fresh session resets the token budget, allowing Cody to allocate the maximum token allotment to code context retrieval.


4. Eliminate Hallucinations with Explicit Context Mentions

Relying entirely on Cody's automatic search heuristics across a monorepo or large codebase is a recipe for intermittent hallucination. When precision is required, override heuristic retrieval using explicit @-mentions:

SyntaxActionWhen to Use
@filename.tsPins the exact source file into prompt contextWhen Cody needs full implementation details of an adjacent service
@symbol:FunctionNameTargets a specific class, interface, or methodWhen you only need the type signature without wasting token budget
@folder/Restricts context search to a bounded moduleWhen Cody keeps pulling irrelevant matches from legacy directories
@repo:nameDirects multi-repo queries to a specific remote repoWhen calling APIs defined across separate enterprise services

By explicitly pinning @types/api.ts or @symbol:PaymentGateway, you inject the authoritative contract directly into the context payload, making it structurally impossible for the model to hallucinate parameters.


5. Add Prompt-Level Guardrails and Model Selection

Even with retrieved context, aggressive generative models sometimes prefer plausible extrapolation over strict conformance. If Cody still invents convenience methods:

  1. Enforce negative constraints in the prompt:
    Append a explicit constraint:
    "Implement this strictly using the exported functions in @client.ts. Do not assume helper methods exist if they are not in the provided context. If a required method is missing, output API_MISSING and describe the missing signature."
  2. Switch the underlying LLM:
    In Sourcegraph Cody's model dropdown, evaluate model performance. In testing at 365Finds, models like Claude 3.5 Sonnet demonstrate markedly lower hallucination rates when adhering to strict API signatures compared to smaller default models.

The 365Finds Verdict

Sourcegraph Cody's architectural strength is its code graph integration, but its silent failure mode is its biggest usability flaw. When symf crashes, SCIP indexes are missing, or a .gitignore hides generated stubs, Cody does not halt with an error—it defaults to ungrounded semantic guessing.

Our operational rule at 365Finds: inspect the context pill on the first sign of an unfamiliar function call, verify your symf output log, and force explicit symbol resolution with @-mentions whenever working across module boundaries.

References