AI • Science • Technology • Future

Coding AI

A Smarter Way to Run Code Migrations with Less LLM Context

Diagram of a codebase audit workflow with sandboxed script processing and structured output generation

As enterprises adopt AI for codebase modernization, a critical challenge emerges: the cost of token consumption. Tasks like code migration audits often require scanning thousands of files, leading to massive context loads in large language models (LLMs). This inefficiency not only inflates costs but also degrades model performance due to information overload.

Sourcegraph’s Deep Search introduces a novel architectural solution: running scripts in a sandboxed environment around its search APIs. Instead of passing raw file contents into an LLM, Deep Search performs the heavy lifting of data processing—filtering, aggregating, and structuring results—before delivering only high-level, actionable outputs such as CSV checklists or visual reports.

What Happened: A Shift from Raw Data to Structured Outputs

Traditional AI agent workflows treat the LLM as the primary processor for every step of a task. For example, when auditing a codebase for outdated dependencies or migration opportunities, an agent might scan every file, extract every line of code, and feed the entire dataset into the model’s context window. This approach, while intuitive, is fundamentally flawed.

As the original source explains, “more context does not always mean better context.” The LLM is not designed to parse thousands of lines of irrelevant or redundant code. Instead, it must synthesize conclusions from clean, structured data. Deep Search changes this paradigm by offloading the data processing to a script-based sandbox that runs directly on Sourcegraph’s search APIs.

Key Facts and How It Works

Deep Search operates in a secure, isolated environment where scripts execute against Sourcegraph’s search capabilities. These scripts can perform multiple queries, cross-reference findings, and generate structured artifacts—such as CSVs, JSONs, or SVG charts—without ever exposing raw file content to the LLM.

  • It scans the Kubernetes repository for files under pkg/ with the most TODO comments, then returns a CSV and an SVG chart.
  • The LLM receives only the aggregated findings and final artifacts, not the intermediate file contents.
  • Results are immediately actionable, enabling engineering teams to begin migration work without delays or data overload.

This architecture is not just about cost reduction—it improves the quality of AI-generated insights. By eliminating noisy, redundant data, the LLM remains focused on synthesizing meaningful conclusions rather than acting as a data processor.

Why This Matters: Cost, Efficiency, and Quality

Token costs are a growing concern for enterprise AI adoption. A single large-scale audit could consume tens of thousands of tokens, making such tasks prohibitively expensive at scale. Deep Search directly addresses this by reducing the volume of data fed into LLMs.

For example, in a migration scenario where services must be moved from an old package to a new one, Deep Search can perform a full audit of all repositories still importing the old package. It returns a clean CSV checklist of affected files—no raw code, no file contents, just actionable data. Teams can open this file and begin migration work with confidence in its completeness and accuracy.

Beyond cost, this approach enhances the reliability and usability of AI-generated outputs. Structured reports are easier to interpret, share, and act upon than unstructured, text-heavy responses. This shift aligns with best practices in AI engineering: using AI not for data processing, but for insight generation.

Broader Applications and Use Cases

Deep Search’s capabilities extend beyond migration audits. It enables a range of search-driven artifact generation tasks, including:

  • Inventory tracking: Identify how shared services are consumed across the organization.
  • Code health analysis: Detect repositories using outdated or vulnerable dependencies (e.g., Log4j versions < 2.17).
  • Pattern consistency checks: Compare implementations across code patterns to identify inconsistencies or deviations.

These applications demonstrate that Deep Search is not a niche tool—it is a foundational component for scalable, efficient AI-assisted software maintenance.

Limitations and Open Questions

While Deep Search significantly reduces token usage, it is not a panacea. Limitations include:

  1. It requires access to a robust code search infrastructure like Sourcegraph, limiting adoption to organizations with existing codebase visibility tools.
  2. Scripting capabilities are constrained by the search API’s query language and performance limits—complex logic may require optimization or external tools.
  3. There is no public documentation on error handling or failure modes in sandboxed script execution.

Additionally, the effectiveness of this approach depends on the quality of the underlying search results. If the search APIs are inaccurate or incomplete, the generated reports may contain false positives or miss critical issues.

What to Watch Next

As AI adoption matures, token efficiency will become a core metric for engineering leaders. The evolution of tools like Deep Search signals a broader trend: moving from AI-as-a-data-processor to AI-as-a-conclusion-generator.

Looking ahead, similar architectures may be applied to other domains—such as automated dependency triage or security vulnerability scanning—where large-scale codebase analysis is required. The integration of such tools with existing CI/CD pipelines and DevOps workflows will likely become a key focus area.

For organizations planning large-scale migrations or audits, adopting solutions like Deep Search represents a practical step toward sustainable AI use. As one of the original source authors notes, “It’s time to move beyond standard agent workflows and let Deep Search handle the scale.”

Original source: A smarter way to run code migrations with less LLM context | Sourcegraph Blog

For more on how AI agents can scale code updates, see Agentic Batch Changes: Scaling Code Updates with AI Agents. For insights on how AI pricing is evolving, explore Sourcegraph Now Prices AI Code Changes on Outcomes.

Sources & further reading