← All posts
AI Guides· 5 min read

Context Engineering: A Hands-On Guide to Memory, Knowledge Graphs, and Token Compression for Claude Code

Claude Code fails in two quiet, expensive ways: it forgets what it learned last session, and it re-reads your whole codebase to answer a small question. This is the install-and-use walkthrough for four tools that fix both — with exact commands and the order I'd add them.

In the last post I walked through installing skills that teach Claude how you work. This one is about the other half: what Claude knows, and what you pay to know it.

Claude Code fails in two quiet ways, and both cost money. It forgets — close the session and its understanding of your repo evaporates, so you re-explain it tomorrow. And it over-reads — to answer “where do we validate webhooks?” it scans far more of your codebase than it needed, and every token is on the bill.

I spend most of my time on data pipelines, so the framing came naturally: Claude’s context window is a query engine, and we already know how to make those fast — index once, query cheap, compress before you pay. Four tools do exactly that. Here’s how to install and use each.

Index the codebase: codebase-memory-mcp

codebase-memory-mcp builds a knowledge graph of your codebase — functions, files, relationships — across 158 languages via tree-sitter, exposed to Claude over MCP. Claude looks things up instead of re-scanning files (the maintainers claim ~120× fewer tokens vs file-by-file exploration).

Install (auto-detects and configures Claude Code):

curl -fsSL https://raw.githubusercontent.com/DeusData/codebase-memory-mcp/main/install.sh | bash

Prereqs: a C/C++ compiler (gcc/clang) and zlib — it builds a single static binary, no runtime deps. If you prefer manual MCP wiring, add to ~/.claude/.mcp.json:

{
  "mcpServers": {
    "codebase-memory-mcp": { "command": "/path/to/codebase-memory-mcp", "args": [] }
  }
}

Use: restart Claude Code, run /mcp to confirm it’s connected. Then just work — Claude reaches the 14 MCP tools (semantic search, call-graph tracing, dead-code detection) on its own. A background watcher auto-syncs the index as code changes.

The data-engineer analogy: an index. Instead of full-scanning to learn that processWebhook calls verifySignature, Claude queries it.

Caveat: an index is only as good as its freshness. Confirm the auto-sync keeps up on a fast-moving branch, or you’ll get confidently outdated answers.

Index everything around the code too: graphify

graphify widens the idea past code: it turns source, SQL schemas, docs, PDFs, even images and videos into one queryable graph — app code, database schema, and infrastructure together.

Install (needs Python 3.10+ and uv):

uv tool install graphifyy
graphify install            # registers with Claude Code

Use — build, then query in natural language:

/graphify .                                   # build the graph
/graphify query "what connects auth to the database?"
/graphify explain "RateLimiter"
graphify hook install                         # auto-update the graph on each commit

It writes an interactive graphify-out/graph.html and a GRAPH_REPORT.md. graphify claude install makes Claude prioritize graph queries over reading files.

The data-engineer analogy: a unified semantic layer over a messy lake. The value isn’t one table — it’s the joins across them.

Reach for it when the questions that slow you down cross boundaries — “this API field maps to which column, populated by which job?” That answer lives in three systems; one graph stops the scavenger hunt.

Caveat: the broader the graph, the more it costs to build and keep current. Point it at the corner where cross-system questions genuinely hurt — not at everything on day one.

Remember across sessions: claude-mem

claude-mem captures what happens in a session, compresses it, and injects the relevant parts into future sessions so the next one starts warm.

Install:

npx claude-mem install

or via the plugin marketplace:

/plugin marketplace add thedotmack/claude-mem
/plugin install claude-mem

Needs Node 20+. Restart Claude Code — past-session memory then appears automatically (it wires 5 hooks: SessionStart, UserPromptSubmit, PostToolUse, Stop, SessionEnd).

Use: the mem-search skill queries your project history in natural language; a web viewer lives at http://localhost:37777. Wrap sensitive content in <private> tags to keep it out of storage.

The data-engineer analogy: a materialized summary table. You don’t replay the whole event log every morning; you read a rolled-up state instantly.

Caveat: memory is only an asset while it’s true. Stale memories get recalled with the same confidence as good ones — know how to inspect and prune (the viewer makes this easy).

Compress what’s left: headroom

headroom (from a Netflix engineer) sits between your tools and the model and compresses everything Claude reads before it hits the window — they report 60–95% fewer tokens, with originals cached so compression is reversible.

Install (Python 3.10+):

pip install "headroom-ai[all]"

Use — easiest is to wrap Claude directly:

headroom wrap claude

Or run it as a proxy any OpenAI-compatible client routes through (zero code changes):

headroom proxy --port 8787

Optionally trim what the model writes back too:

export HEADROOM_OUTPUT_SHAPER=1

Check savings with headroom dashboard; sanity-check health with headroom doctor.

The data-engineer analogy: column pruning and compression. Ship the smallest representation that still answers the question.

Caveat: “same responses” is the claim to verify on your workload. Compression is lossy by design — watch a few real tasks for quality drift before trusting the savings. (Their published GSM8K/TruthfulQA numbers held, but your code isn’t a benchmark.)

How I’d actually layer them

These are four layers of one stack, not four answers to one question:

That last clause matters. None of this is free: every layer is more setup, more moving parts, more freshness to babysit. Don’t bolt on a knowledge graph because it’s clever — add it the day you watch Claude re-read the same directory for the fifth time. Let the pain pick the tool. That’s true of data infrastructure, and just as true here.


Part two of a hands-on series. Next: loop engineering — installing and wiring the tools that let agents keep working, checking, and fixing without you babysitting every step.