ContextMesh — Intelligent Context Layer for Claude Code
One coding session. Infinite memory. Minimal active context.
ContextMesh sits between Claude Code and the model. It captures every event in your session, builds a structured knowledge graph of your work, and dynamically provides Claude with only the most relevant context for the current task — saving tokens, reducing cost, and preventing context overload.
How it works
Claude Code Hooks → ContextMesh Daemon → Session Graph + Repo Graph
↓
Context Router (multi-signal scoring)
↓
Claude Code ← MCP Server (get_context)
↓
Token Savings Tracker → Report
The key insight: semantic similarity alone fails for long coding sessions because everything related to the same feature looks similar. ContextMesh uses graph proximity + code dependency relationships + causal edges + recency + file overlap — not just vectors.
Installation
# Clone and install
git clone https://github.com/you/ContextMesh
cd ContextMesh
pip install -e .
# Run the install script (sets up hooks + config)
bash scripts/install.sh
# Index your project's codebase
contextmesh index /path/to/your/project
# Start the daemon
contextmesh start
# Start the MCP server (in a separate terminal)
contextmesh mcp
For exact token measurement (Claude Enterprise)
# Start the token proxy
contextmesh proxy
# Then set in your environment:
export ANTHROPIC_BASE_URL=http://localhost:8099
# This records EXACT token counts from actual API responses
# including cache_creation_input_tokens and cache_read_input_tokens
Adding ContextMesh to a project
Copy CLAUDE.md from this repo to your project root. Claude Code will automatically instruct Claude to use the get_context tool at the start of each response.
cp /path/to/ContextMesh/CLAUDE.md /path/to/your/project/CLAUDE.md
Register MCP server with Claude Code
Add to ~/.claude/settings.json:
{
"mcpServers": {
"contextmesh": {
"command": "contextmesh",
"args": ["mcp"]
}
}
}
View token savings
# Session summary
contextmesh stats --session YOUR_SESSION_ID
# Recent turns with savings breakdown
contextmesh turns --session YOUR_SESSION_ID --limit 20
# Global summary across all sessions
contextmesh stats
# If using proxy mode — actual API token counts
contextmesh stats --proxy
Token savings tracker
Every time Claude calls get_context(), ContextMesh records:
| Metric | What it is |
|---|---|
| Accumulated tokens | What the full session history would have been |
| Routed tokens | What ContextMesh actually provided |
| Tokens saved | The difference |
| Compression ratio | routed / accumulated |
| Cost saved | Based on your configured per-MTok price |
Architecture
Dual Graph
Session Graph — captures every meaningful event:
- User prompts, tool results, file reads/writes
- Decisions, bugs, solutions, errors, test results
- Typed edges:
caused_by,solved_by,depends_on,same_task
Repo Graph — deterministic code relationships:
- Functions, classes, methods, files (Tree-sitter parsed)
calls,imports,same_file,tested_by,inheritsedges- Updated incrementally on every file write
Hot / Warm / Cold Memory
HOT → Current task context (always injected)
WARM → Related tasks, decisions, nearby graph nodes (retrieved on demand)
COLD → Full historical transcripts (never auto-injected)
Context Router
Before every get_context() call:
context_score =
semantic_relevance (embedding cosine similarity)
+ graph_proximity (BFS distance in session graph)
+ file_overlap (Jaccard similarity with current task files)
+ recency (exponential decay from now)
+ causal_relevance (DECISION/BUG/SOLUTION type bonus)
+ unresolved_bonus (UNRESOLVED_ISSUE always surfaces)
Cache-aware assembly order
STATIC (cacheable)
─────────────────────────────
=== CURRENT TASK ===
[hot nodes — current thread]
=== RELEVANT DECISIONS ===
[top-scored decisions]
DYNAMIC
─────────────────────────────
=== RELATED CODE CONTEXT ===
[repo graph: functions/classes in touched files]
=== RECENT HISTORY ===
[recent warm nodes]
=== UNRESOLVED ISSUES ===
[always surfaced]
Configuration
.contextmesh/config.toml in your project (or ~/.contextmesh/config.toml globally):
[router]
default_budget_tokens = 15000
[tracker]
input_price_per_mtok = 3.0 # Claude Enterprise cached input price
uncached_price_per_mtok = 15.0
[embeddings]
model = "all-MiniLM-L6-v2" # Local, no API key needed
[tasks]
topic_shift_threshold = 0.35 # Cosine distance to detect task switch
Development
pip install -e ".[dev]"
pytest tests/
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file claude_contextmesh-0.1.0.tar.gz.
File metadata
- Download URL: claude_contextmesh-0.1.0.tar.gz
- Upload date:
- Size: 42.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
323c59823c4ad0b042c1d29359d9601654e344c4981a43deffd14b72b9f537d6
|
|
| MD5 |
d4b9c30c197b56980dfec71d6ec35b6b
|
|
| BLAKE2b-256 |
7a58d6c4566bcd357fb49f9b6be3cdf857cd6d6bad0fe5a19cd8be1947720a10
|
File details
Details for the file claude_contextmesh-0.1.0-py3-none-any.whl.
File metadata
- Download URL: claude_contextmesh-0.1.0-py3-none-any.whl
- Upload date:
- Size: 50.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87ecc7b834d950ec29bf6700b0ba1c31a4d94b7261bd093b4e5d6851ad901825
|
|
| MD5 |
e0681a9f613b38bf2a650f1b823b3e44
|
|
| BLAKE2b-256 |
03ac6845dc41e1fa97d4298d6f5f2ee5c5c11df9753b237bc1a1175301028a8c
|