
Rubi – A knowledge graph that cuts how much Claude Code re-reads
AI coding assistants are billed for every token they read, and in a long conversation they re-read the whole history on every message. Analyzing 284 real Claude Code sessions, we found that 71% of the cost was spent re-…
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
Check your usage · Live demo · Walkthrough · Results · Install & test · The theory
AI coding assistants are billed for every token they read, and in a long conversation they re-read the whole history on every message. Analyzing 284 real Claude Code sessions, we found that 71% of the cost was spent re-reading context and 97% came from sessions that grew past 150k tokens. Claude was not doing more work. It was carrying more weight.
Everything is plain markdown inside the project (.rubi/), opens as an Obsidian graph, and runs locally with zero dependencies. It was built for Claude Code and, because its core is a standard MCP server, it also runs in Codex, Gemini CLI, Cursor, GitHub Copilot, Windsurf, Cline, OpenCode, Continue, Goose and Zed, with hosted or local models (see the table).
Results. Two studies, reported as measured, including what did not work:
The problem: why long sessions get expensive
Background: the ideas, in plain words
Design: how Rubi works
Results
Walkthrough: a week with Rubi
Limitations and threats to validity
Install
Reference
References
- The problem: why long sessions get expensive
1.1 Claude has no memory between messages
A language model does not "remember" a conversation. Every time you send a message, the entire conversation so far is sent again: your requests, Claude's answers, and every file it read and every command it ran. That bundle is the context.
In plain words. Imagine a consultant who, before answering each new question, re-reads the whole meeting transcript from the start. Message 10 is cheap. Message 500 means re-reading 499 pages first.
So the cost of a session is not proportional to the number of messages but to the sum of the context sizes. If the context grows by a roughly constant amount per message, that sum grows with the square of the session length.
Anthropic's prompt caching stores the beginning of the context so that re-reading it costs about 10% of the normal input price. It is a real discount, and in our data it worked: 99% cache hit rate. But 10% of a million tokens, on every message, for hundreds of messages, is still most of the bill. When the cache expires (for example after a pause), the whole context is written to it again at a higher price.
Rubi ships a token X-ray that reads Claude Code's local transcripts. On 30 days of real usage:
Rubi combines well-known techniques from information retrieval, graph theory and recent research on memory for language models. Each one below gets a plain-words explanation and a pointer to the original work.
A graph is just things (nodes) connected by relationships (edges). In Rubi the nodes are files, modules, decisions, bugs, gotchas and concepts, and the edges are "file A imports file B", "this decision is about login.ts", "this bug is related to Checkout", "this decision replaces that one".
In plain words. A folder of notes is like a pile of index cards: you only find a card if you guess the word written on it. A graph is like a city map: from where you are, you can see what's next door. Ask about "finishing a purchase" and a plain search finds nothing, because no note says "purchase". With Rubi, Claude adds the related word checkout to the search (section 2.9), the graph finds the concept Checkout, sees that it is linked to a gotcha called "Coupon applied twice" and to the file cupom.ts, and brings those along. You get the trap you were about to fall into, even though you never typed its name.
That is the difference: search finds what matches your words; a graph also finds what is connected to them. In code, connections are everything: who imports whom, which decision affects which file, which bug came back where.
When you search, something has to decide which results come first. BM25 (Robertson & Zaragoza, 2009) is the classic answer used by search engines: a word counts more if it is rare in the collection and frequent in the document, with diminishing returns so repetition doesn't win. Rubi uses it for the textual part of the search, with accent folding, camelCase splitting (PaymentError → payment, error) and light Portuguese/English stemming.
PageRank (Brin & Page, 1998), the algorithm behind early Google, says a page is important if important pages link to it. Applied to code: a file is central if many files import it, especially central ones. Rubi computes PageRank over the import graph to tell Claude, in one line, which files are the backbone of the project. This is the same idea as Aider's repo map.
Personalized PageRank (Haveliwala, 2002) is PageRank that "starts" from a few chosen nodes and spreads outward, so you get what is important relative to your question. HippoRAG (Gutiérrez et al., 2024) showed this works remarkably well as long-term memory for language models, inspired by how the hippocampus links memories. Rubi seeds it with the nodes that matched your words and lets relevance flow through the graph, discounting "hub" files that everything touches.
Text, exact file and function names, and graph proximity each produce their own ranked list. Reciprocal Rank Fusion (Cormack et al., 2009) merges them with a simple rule: each list gives every item 1 / (60 + position), and the scores are added. It is robust, needs no tuning, and is what ai-memory also uses.
Microsoft's GraphRAG (Edge et al., 2024) showed the value of summarizing a knowledge graph at several levels. Rubi keeps three:
In plain words. Instead of handing someone the whole encyclopedia, you hand them the table of contents and let them open the chapter they need.
Decisions change. Zep / Graphiti (Rasmussen et al., 2025) proposed a temporal knowledge graph for agent memory, where facts are invalidated instead of deleted. Rubi does the same: when a new decision replaces an old one, the old node is marked superseded and points to its successor. Claude never follows a stale rule, yet it can still see why things changed. Each node also records the files and the git commit it came from. If a file changed after the note was written, search flags it with ⚠ so Claude double-checks.
MemGPT (Packer et al., 2023) framed LLM memory like an operating system: a small core always in context, a working set, and archival storage. Rubi maps that onto core (the level-0 map), working (the current handoff) and archival (the graph, queried on demand).
Most memory systems call another model to extract facts and compute embeddings. Rubi doesn't need one, because Claude is already running:
3.1 What lives in your project
your-project/ ├── .rubi/ ← commit it: your team shares the memory │ ├── mapa.md level 0 · injected at the start of every session │ ├── modulos/src-checkout.md level 1 · one per module │ ├── conhecimento/ level 2 · one markdown file per graph node │ │ ├── decisao-usar-jwt-em-vez-de-sessao.md │ │ ├── armadilha-cupom-aplicado-duas-vezes.md │ │ └── conceito-checkout.md │ ├── handoffs/2026-10-17-1530.md │ ├── sessoes/… automatic pre-compaction records (git-ignored) │ └── .indice.json derived index, rebuildable (git-ignored) └── CLAUDE.md can now be short: it points to the map
A node is ordinary markdown with its type and edges in the frontmatter:
id: "decisao-usar-jwt-em-vez-de-sessao" tipo: "decisao" status: "ativo" arquivos: ["src/auth/login.ts"] liga: [{"rel":"sobre","alvo":"conceito-autenticacao"}] substitui: "decisao-sessao-em-cookie" commit: "a1b2c3d"
Usar JWT em vez de sessão
Stateless, so it runs on edge runtimes without a session store.
Ligações
- sobre [[conceito-autenticacao]]
- substitui [[decisao-sessao-em-cookie]]
Personal preferences that apply to every project live in a separate personal vault (~/rubi-vault/_pessoal). Search covers both.
sequenceDiagram participant You participant Claude as Claude Code participant Rubi as Rubi hooks + MCP participant Mem as .rubi/ (markdown graph) Note over Rubi,Mem: session start: index the code (incremental), inject the ~500-token map You->>Claude: "fix the coupon bug in checkout" Rubi-->>Claude: recall: gotcha "Coupon applied twice" (from the graph, before any exploration) Claude->>Rubi: rubi_contexto("coupon bug", termos: [discount, cart, Checkout]) Rubi->>Mem: BM25 + entities + Personalized PageRank → RRF Rubi-->>Claude: gotcha "Coupon applied twice", cupom.ts, Checkout.tsx (≤1.5k tokens) Claude->>Claude: goes straight to the right file Note over Claude,Rubi: end of an answer that explored the code Rubi-->>Claude: Stop hook: "register what you learned" Claude->>Mem: rubi_registrar(facts, links, files) Note over Claude,Rubi: context reaches 80% of 150k Rubi-->>Claude: "save a handoff now" Claude->>Mem: rubi_salvar_handoff Claude->>Rubi: PreCompact → session record saved Claude->>Rubi: SessionStart(compact) → handoff injected back Claude->>You: continues, same thread, ~60k context
Loading
Hook / tool What it does
SessionStart Indexes the code (incremental, in the background after the first run) and injects the level-0 map. After a compaction or /clear, injects the handoff.
UserPromptSubmit Recall. Searches the graph with your request and injects the relevant knowledge (≤700 tokens, only when something actually matches). Also runs the meter.
PostToolUse Meter. Reads the real context size and, at 80% of the window, asks Claude to register facts, write missing summaries and save a handoff.
Stop Harvest. If the answer involved heavy exploration (≥10 reads/searches) and nothing was saved, asks Claude to register 1–5 durable facts before finishing.
PreCompact Saves an objective record of what is about to be compacted (requests, files changed, commands) and links it to the files.
PreToolUse Read guard. Blocks reading a huge file in full once and points to Grep or offset/limit.
rubi_contexto Hybrid search within a token budget.
rubi_registrar Writes graph nodes: merges duplicates, resolves links, supersedes old decisions.
rubi_mapa, rubi_escrever_resumo Zoom levels and curated summaries.
3.3 What a search returns
[armadilha] Cupom aplicado duas vezes — conhecimento/armadilha-cupom-aplicado-duas-vezes.md
src/desconto/cupom.ts · ⚠ src/desconto/cupom.ts mudou depois desta nota
O desconto rodava antes do total ser recalculado.
[conceito] Checkout — conhecimento/conceito-checkout.md Fluxo de finalizar compra.
[arquivo] src/desconto/cupom.ts — módulo src/desconto, usado por 2 · ligado a "Cupom aplicado duas vezes" aplicarCupom
(3 itens, ~180 tokens. Abra um item com rubi_ler; zoom no módulo com rubi_mapa.)
Every item says why it is there ("ligado a…" = linked to, "citado" = named in the question), so Claude can trust or discard it quickly.
We ran two studies: a retrospective analysis of real usage, and a controlled A/B benchmark.
Method. The X-ray reads every Claude Code transcript from the last 30 days (284 sessions) and replays each session's actual context curve under a policy that compacts whenever the context passes a threshold and resumes from the fixed context plus an 8k-token handoff. The cost of each compaction (rewriting the new cache prefix) is charged. Costs are in input-token equivalents using Anthropic's price ratios (cache read 0.1×, cache write 1.25–2×, output 5×).
150k was the sweet spot: lower thresholds pay for too many compactions, higher ones carry too much context. This is why Rubi's default is 150k. The interactive demo lets you replay a single session with your own numbers.
Method. Two public repositories were cloned fresh for every run: Flask (Python) and Hono (TypeScript). For each one we wrote three chained questions about the same area of the code, like three days of work on one feature (see bench/tarefas.json). Each question ran in a new session, so nothing carried over except what was stored on disk.
We separated the two things a memory system does, reading and writing, into four arms:
Reproduce with node bench/rodar.mjs --reps 3 --bracos A,C,D. Raw CSVs, summaries and every answer are in bench/resultados/ and on Hugging
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。