Podcast

Building Cody, an Open Source AI Coding Assistant

Beyang Liu, SourcegraphEpisode 173 · 1:02:13 · Aug 2023 · 812 viewsHosted by Demetrios Brinkmann
Thumbnail for Building Cody, an Open Source AI Coding Assistant Watch on YouTube
TL;DR
  1. 1

    Cody's answers depend mainly on whether its context-fetching system finds the relevant code, often through actions such as finding references or jumping to a definition.

  2. 2

    Sourcegraph combines traditional keyword search, embedding-based search, and AI-based reranking instead of replacing its existing search system wholesale.

  3. 3

    AI coding agents need reliable single-step actions before they can support longer chains of autonomous work in production.

Summary

Beyang Liu explains why Sourcegraph started with code search and how that work led to Cody, its AI coding assistant. Developers spend much of their time understanding existing code before they can change it, especially in large and old codebases. Sourcegraph built a code graph from symbol tables so users could jump to definitions and find references without first making the code build locally. Cody adds code completion, natural-language questions, code generation, and commands such as explaining code or generating unit tests. Beyang argues that the quality of these features depends mostly on retrieval. If the system finds the right surrounding code, current language models can often synthesize a useful answer. If it misses important context, the model is likely to invent an explanation. He also describes a gradual path toward agents, starting with reliable one-step operations and adding more steps only as accuracy improves.

Key ideas
02:01

Sourcegraph began by reducing the time developers spend understanding existing code

Beyang says Sourcegraph came from his own frustration with reading unfamiliar code before making changes. At Palantir, he and co-founder Quinn worked with large enterprise codebases that were old, complex, and difficult to understand. Some banking teams were still emailing code blobs to lawyers for compliance review in 2011. His earlier experience with Google's internal code search shaped the product idea. Google engineers could search the monorepo, jump to definitions, and find references without setting up a local development environment. Sourcegraph aimed to make that style of code exploration available to developers outside Google.

09:08

Code search had to include the relationships between symbols

Sourcegraph treated code search as more than matching text. After opening a result, developers usually need to follow definitions, references, and other connections through the code. Beyang describes constructing a code graph by compiling code and extracting symbol tables. That graph lets Sourcegraph answer requests such as jump to definition and find references without requiring the user to get the whole codebase building first. The graph also became useful for code exploration and provided signals that could improve search. This work gave the product a structural view of code alongside ordinary textual search.

24:39

Embedding search finds related code even when the words do not match

Beyang explains that Sourcegraph maps functions and user queries into the same embedding space. A dot product estimates how close the query and code are in semantic space, allowing nearest-neighbor retrieval. A developer searching for an OIDC handler might use terms such as 'SSO Handler' or 'SSO Handler based on OAuth' and find relevant code without typing 'OIDC' or 'OpenID Connect'. Sourcegraph began testing these signals in late 2021 or early 2022. Early results were poor most of the time, but a small share produced semantically close matches with no keyword overlap. The team then iterated and fine-tuned the embedding model.

18:03

Sourcegraph is adding AI beside its existing search system

Cody was built as a new product, while Sourcegraph added AI signals to its existing search engine. Beyang describes a future hybrid backend where keyword and embedding search each retrieve a broad set of potentially relevant results. An AI-enabled intermediate layer can then rerank those results and move the most relevant ones to the top. This approach lets the team improve an established product without rewriting it from scratch. He says the first experiments used an off-the-shelf embedding model to augment trigram and regular-expression search, which provided a practical way to test whether AI signals improved the baseline.

26:09

Cody combines autocomplete with explicit code instructions

Cody has two main interfaces. Its inline autocomplete uses the current code and retrieved snippets from elsewhere in the codebase to predict what the developer is trying to write. Its explicit instruction interface supports tasks such as explaining a selected piece of code, generating a unit test, and creating documentation. Beyang prefers presenting this as a natural-language REPL rather than open-ended chat. A request can be attached to a selected code range or entered in a sidebar. Cody searches the repository for context, then answers the question or generates code based on the instruction.

28:45

Retrieval quality determines whether Cody explains code correctly

Beyang says the quality and accuracy of Cody's responses are dictated about 95 percent of the time by whether the system retrieves the relevant code into the model's context window. To explain why a function has a 'do not change' comment, Cody may need to find references and inspect how the function is called elsewhere. A model such as Claude or ChatGPT can often combine those snippets into a reasonable explanation. If the retrieval system misses an important example, the model is more likely to make up an answer. Cody does not yet trace every dependency through very complex codebases, but Sourcegraph is working toward multi-step exploration.

31:25

AI should reproduce the useful parts of human code exploration

Beyang rejects the idea that an AI will simply absorb an entire codebase and produce a magical answer. A human investigating a risky change starts with the target code, searches for related functionality, finds references, follows definitions, and gradually builds enough context to edit safely. Sourcegraph is trying to make the AI perform that process. The current system handles some questions answerable with one hop, such as a find-references or go-to-definition action. Beyang describes a progression toward two hops, three hops, and eventually more autonomous exploration. The aim is to automate the tedious investigation while leaving the high-level change to the developer.

48:14

Long agent workflows remain unreliable because errors compound

Beyang sees agents as systems that take natural-language instructions and perform a sequence of actions, with or without a human checking each step. He says the field first needs reliable individual actions. A one-step process that is 70 percent reliable may be tolerable, but a chain of several such steps quickly becomes unlikely to finish correctly because each step can send the workflow off course. He expects progress from better training and fine-tuning, models that use APIs more effectively, and systems that perform static checks or fetch better context. In the conversation, he estimates that longer chains may still be at least a year away from dependable production use.

"The quality and accuracy of responses from Cody, whether you're doing inline autocomplete or answering a question, probably 95% of the time it's dictated by, hey, did you fetch the relevant pieces of code into the context window of the large language model?"Beyang Liu29:09
Who should watch
  • You are building an AI feature into an existing developer product and need a practical way to add model capabilities without replacing the whole system.
  • Your coding assistant produces plausible answers, but you need to understand why retrieval and repository context determine its accuracy.
  • You are exploring coding agents and want a candid view of why multi-step reliability is harder than single-step autocomplete or question answering.