The 'borg learn' Trick: How to Stop Your AI Agent from Re-Reading Your Entire Repo
TL;DR
- Run
borg learnonce and it writes a BORG.md that captures your repo's structure so the agent never re-scans the whole thing.- The compact Markdown file stays in your repo, giving every future session instant project memory without token waste.
- On a typical 1,000-file repo, the agent's context shrinks from megabytes of full source to a few kilobytes of summary.
Use borg learn in your project root. It generates a BORG.md file that distills your codebase into a compact, searchable map the agent reads instantly, avoiding repeated full-tree scans. We build and operate xShellz: the borg terminal agent, the Agent Shell boxes and the hosted model gateway behind them.
How do I stop my AI coding agent from wasting tokens re-reading my whole project every time I start a new session?
The problem is real: every time you fire up an AI coding agent, it parses the entire source tree to understand what you are working on. A 10,000-file project can consume millions of tokens before it even gets to your prompt. Borg, the terminal agent that ships as one static Go binary, fixes this with a dedicated subcommand. Run borg learn once inside any project and it writes a BORG.md file, a permanent, structured summary of the codebase. From then on, the agent reads that compact file instead of re-indexing everything. You skip the token burn, and the agent gets its bearings instantly. For a deeper look at the agent itself, see the borg page.
what is borg learn and how does it work?
borg learn is a subcommand of the borg CLI (installed via curl -fsSL https://turborg.com/install.sh | sh). It recursively walks your project, identifies the most important files (entry points, configuration files, directory layout, package manifests), and synthesizes a BORG.md in the current working directory. The file is plain Markdown: headings for top-level structure, bullet lists of key modules, and short descriptions of design patterns. When you later start a session with borg, it loads BORG.md as pre-context, effectively giving the agent a floor plan before it writes a single line of code. Context-efficient agents are a known design principle; the Claude Code docs highlight how a focused context window avoids redundant re-processing. The beauty is that the summary persists on disk, so every future session (days, weeks, or months later) starts with the same mental model, no re-scan required.
how to create a BORG.md file for my project
Install borg if you haven't already:
curl -fsSL https://turborg.com/install.sh | sh
Then cd into your project root and type:
borg learn
That's it. Borg scans the directory, talks to your configured model (Ollama, LM Studio, OpenAI, OpenRouter, or the hosted xShellz models: Chuppa Flash, Chuppa Pro, Floko, Axiom), and writes BORG.md in the current directory. The file is human-readable, so you can review it immediately. Commit it to version control so the whole team benefits, or add it to .gitignore if you prefer to regenerate it locally. The next time you run borg in that project, the agent will read the summary before anything else.
can I save my AI agent's context between sessions?
Yes, in two complementary ways. The BORG.md file acts as long-term, project-wide memory: key modules, architectural choices, and file layout survive across weeks and across different machines. For short-term session continuity, borg offers borg --resume, which continues your most recent interactive session exactly where you left off, including the chat history and open tasks. Combine the two: borg learn writes the permanent map, and borg --resume picks up the ongoing conversation. If you run borg on an Agent Shell box, the always-on environment keeps your tmux session alive 24/7, so you never lose conversational context even if you close your laptop.
how much money/tokens does borg learn actually save?
The savings depend on the size of your repo, but the mechanism is straightforward. A BORG.md for a 2,000-file TypeScript monorepo might be 3 KB to 5 KB of text, while the full source tree (including node_modules or vendored dependencies) can easily run into tens of megabytes that an LLM would otherwise tokenize. The agent reads the summary file once per session, not the whole tree. In a large repository, that means you avoid sending hundreds of thousands of excess tokens every time you start work. There is no per-token charge for generating BORG.md itself when you use a bring-your-own model; with a hosted model you pay only for the one-time analysis, and each subsequent session skips the repeated indexing cost. The real number depends on your tree depth and how many files the agent would otherwise inspect, but a typical project sees an order-of-magnitude drop in initial context size.
Frequently asked questions
Can I edit BORG.md after it's generated?
Yes. BORG.md is plain Markdown. You can add notes about architectural decisions, point to additional documentation, or remove irrelevant sections. The agent treats your edits as the canonical project description.
How often should I re-run borg learn?
Re-run after a major refactor, when you add a new core module, or whenever the project's high-level structure changes. For most teams, running it once per sprint keeps the summary fresh without churn.
Does borg learn require a specific AI model?
No. borg works with any model you configure: Ollama, LM Studio, OpenAI, OpenRouter, or the xShellz hosted models (Chuppa Flash, Chuppa Pro, Floko, Axiom). BORG.md is a simple text file, completely model-agnostic.