The Deska blog

How Aider's Repo Map Works: Context Management for LLMs

A deep dive into how Aider's repo map works to compress codebase context for LLMs using tree-sitter tags and graph rankings.

· 11 min read

Understanding how Aider's repo map works is essential for any developer looking to optimize their interaction with Large Language Models (LLMs) when working on large codebases. As repositories grow, providing the entire source code to an AI becomes impossible due to token limits and increased latency. The repo map solves this by creating a compressed, semantic representation of the project structure, allowing the LLM to understand how different components interact without needing to read every line of implementation.

The Problem of Context Exhaustion

Large Language Models have a finite context window. Even with modern models supporting hundreds of thousands of tokens, a medium sized repository can easily exceed these limits. More importantly, filling the context window with irrelevant code degrades performance, as the model may struggle to focus on the most important logic.

Typical approaches to this problem include RAG (Retrieval-Augmented Generation), where the system searches for relevant snippets based on a query. While RAG is effective for finding specific functions, it often loses the broader architectural context. The LLM might see a function call but have no information about where that function is defined or what its signature looks like if it resides in a different directory.

Technical Architecture of the Repo Map

The repo map functions by extracting a high-level summary of the repository. Instead of just listing file names, it identifies the most important symbols: classes, functions, and variables. This is achieved through a multi-step process involving syntax parsing and ranking algorithms.

Syntax Parsing with Tree-sitter

Aider uses tree-sitter, a concrete syntax tree parser, to analyze the source code. Unlike regex based searching, tree-sitter understands the actual structure of the programming language. It identifies precisely where a class starts and ends, which names are exported, and where functions are invoked.

For every file in the repository, the tool generates a set of tags. These tags include:

  • Function definitions and their signatures.
  • Class definitions and member variables.
  • Significant method declarations.

By extracting only these tags, the repo map reduces the size of the information by orders of magnitude compared to the raw source code.

The Ranking Algorithm

Not all code is equally important for understanding a task. Aider employs a ranking algorithm similar to PageRank to determine which files and symbols are most central to the project. It builds a graph where nodes are files and edges represent references between them.

If main.py imports auth.py and database.py, those files receive a higher rank. The repo map prioritize these high-rank symbols when deciding what to include in the limited token budget allocated for the map. This ensures the LLM sees the "heart" of the application architecture first.

Integrating Repo Maps into the Developer Workflow

When a developer uses a tool that implements repo mapping, the interaction changes from manual file selection to automated context awareness. The developer can ask about a feature, and the LLM uses the map to identify which files need to be loaded in full for editing.

In many modern environments, this map is updated automatically as files change. This keeps the AI's mental model of the code current without requiring manual re-indexing.

Comparison of Context Strategies

StrategyGranularityToken EfficiencyStructural Awareness
Full File UploadHighVery LowHigh
RAG SnippetsLowHighLow
Repo MappingMediumHighHigh
Manual Copy-PasteVariesMediumLow

Managing Multiple Tools in Deska

When working with advanced tools like Aider or Claude Code, developers often find themselves jumping between different interfaces. This is where Deska provides a structured environment. Instead of a single terminal window, Deska allows you to run multiple coding agents side-by-side.

Since Deska is local-first, your code and the indexes generated by these tools remain on your machine. You can open a terminal for Aider to manage the repo map while having a code editor open next to it to verify the changes in real-time. This layout helps you visualize how the AI is interpreting your project structure.

Optimization and Token Budgeting

The repo map is not a static document. It is dynamically sized based on the available space in the LLM's context window. If the developer adds more files to the active chat, the repo map shrinks to accommodate them, removing the lowest-ranked symbols first.

This behavior ensures that the LLM always has a "map" of the surrounding territory, even if it is currently focused on a specific "building" (a file). This prevents the AI from hallucinating APIs or assuming a function exists when it does not.

Extending the Concept with Voice and Mobile

Advanced workspace management allows these technical concepts to be controlled more naturally. For example, using voice commands through Ask Deska, a developer could ask to "open the main controller and the related service definitions." The underlying system can use the repo map logic to identify those specific panels.

Furthermore, when moving away from the workstation, the mobile app allows you to monitor the progress of these agents. Even if the repo map is complex, the secure relay ensures you can see the agent's output and the code it is modifying without needing to expose your local network.

FAQ

How to optimize aider repo map for large projects?

To optimize the map, ensure your project has a clean directory structure and standard naming conventions. Tree-sitter performs best when code follows predictable patterns. You can also use a .aider.ignore file to exclude large, irrelevant directories like node_modules or build artifacts, which prevents the ranking algorithm from wasting cycles on dependencies.

Does the repo map send all my code to the LLM?

No, the repo map only sends a condensed summary of signatures and hierarchy. It does not send the implementation details of every function unless you explicitly add that file to the chat. This is a key privacy and efficiency feature that keeps token usage low while maintaining structural awareness.

Why is tree-sitter used for repo mapping?

Tree-sitter is used because it provides fast, incremental parsing that is more reliable than regular expressions. It allows the tool to distinguish between a function definition and a function call, ensuring that the repo map contains accurate symbols rather than just matching text strings.

Get Started with Enhanced AI Workspaces

Effective context management is the difference between a helpful AI and one that constantly makes mistakes. By combining the technical precision of repo mapping with a flexible, local-first workspace, you can build software faster and with fewer errors.

Experience a better way to manage your AI agents and code side-by-side by downloading the desktop app at /download.

💡 Ideas+🐛 BugsSuggest a feature or report a bug