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).
- Click the context indicator above or below the assistant's reply.
- Expand the list of attached context snippets.
- 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.
- The symptom: Cody retrieves only your currently open tab and ignores related symbols in adjacent directories within the same workspace.
- The check: Open your IDE Output panel (
View > Outputin VS Code) and select Cody by Sourcegraph from the channel dropdown. Ifsymfencountered execution permission issues, crashed on an unsupported architecture, failed on deeply nested file paths (ENAMETOOLONG), or corrupted its local index, you will find explicit crash logs here. - The fix: Delete the corrupted local cache. On macOS/Linux, locate Cody's extension storage (under
~/.config/Code/User/globalStorage/sourcegraph.cody-aior your OS equivalent) and restart your IDE. Cody will re-download or re-initialize thesymfbinary and rebuild the local index.
B. Remote Indexing and Context Filters (cody.contextFilters)
If you connect Cody to a remote Sourcegraph instance or an enterprise code graph:
- Missing SCIP / LSIF precise indexes: When precise code intelligence (SCIP) is missing, Cody falls back to fuzzy text search. Complex polymorphism, overloaded signatures, and abstract factory implementations frequently fail to resolve under keyword matching. Ensure your CI pipeline actively generates and uploads SCIP data for the target repository.
- Repository exclusion via
contextFilters: Enterprise administrators usecody.contextFiltersto protect proprietary or regulated repositories from third-party LLMs. If an administrator configured regex exclusion rules that match internal shared libraries or monorepo packages, Cody is forbidden from reading them. Check instance-level settings or ask your administrator to verify whether the dependency repository matches anexcludepattern. - Forked Repositories: By default, Cody may skip forks during remote searches. If your workspace operates against a fork, add
"search.includeForks": trueto your settings.
C. Collateral Damage from .gitignore and .codyignore
Cody respects .gitignore by default and enforces .codyignore / .cody/ignore rules.
- If your build pipeline generates type definitions, TypeScript declarations, or gRPC/Protobuf stubs into a directory that is gitignored (such as
dist/,build/,gen/, orgenerated/), Cody cannot read those files. - Because the types exist only in ignored build artifacts and not in source code, Cody cannot retrieve the method signatures, forcing the model to hallucinate expected API signatures.
- The fix: Create an explicit
.codyignorefile that un-ignores generated contract files, or check in your schema stubs so Cody's indexer can access them.
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:
- It silently truncates or discards retrieved code chunks to fit the conversation history.
- Older type definitions that were present in turn 1 are pushed out by turn 8.
- 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:
| Syntax | Action | When to Use |
|---|---|---|
@filename.ts | Pins the exact source file into prompt context | When Cody needs full implementation details of an adjacent service |
@symbol:FunctionName | Targets a specific class, interface, or method | When you only need the type signature without wasting token budget |
@folder/ | Restricts context search to a bounded module | When Cody keeps pulling irrelevant matches from legacy directories |
@repo:name | Directs multi-repo queries to a specific remote repo | When 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:
- 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, outputAPI_MISSINGand describe the missing signature." - 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.