context-diamond
Enables LLM agents to compress handoffs into structured, auditable context capsules, preserving goals, constraints, decisions, and risks without external API calls.
README
Context Diamond
Stop pasting the same messy context into every LLM. Turn chats, logs, issues, agent state, and docs into small, auditable context capsules.
Context Diamond is a deterministic context compression toolkit for LLM handoffs. It extracts the things models keep losing in long conversations:
- goals and success criteria
- hard constraints
- decisions already made
- current working state
- open questions and risks
- files, symbols, entities, and anchors
It is built for developers who switch between coding agents, OpenCode, chat UIs, RAG pipelines, issue threads, and local notes. The default engine is offline, zero-dependency, inspectable, and safe to run before any text is sent to an LLM.
Why People Click This
Most LLM context tools promise "memory". Context Diamond gives you a portable handoff artifact you can read, diff, benchmark, paste, store, or feed to another agent.
Use it when you want to:
- recover signal from noisy agent sessions
- reduce repeated prompt/context cost
- preserve constraints before handing work to another model
- keep decisions visible instead of buried in a paragraph summary
- audit what got dropped with a loss report
- expose compression as an OpenCode MCP tool
60-Second Demo
Install from GitHub:
pip install git+https://github.com/RainCherb/context-diamond.git
Compress a long handoff:
context-diamond examples/long_handoff.md --budget 320 --title "Sprint Handoff"
Get JSON with an audit trail:
context-diamond examples/long_handoff.md --format json --loss-report
Benchmark it against dumb head/tail clipping:
context-diamond-bench examples/long_handoff.md --budget 320
Example benchmark output:
535 source tokens -> 387 rendered capsule tokens
1.38x ratio
constraints:1.00 decisions:1.00 risks:1.00 code:1.00
The Pitch
Generic summaries are cheap, but they often flatten the one thing you needed to keep. Context Diamond keeps the handoff structured:
| Problem | Context Diamond answer |
|---|---|
| "The model forgot the rules." | Rules live in their own section. |
| "We reopened an old decision." | Decisions are extracted separately. |
| "The transcript is mostly noise." | Noise is scored down and shown in loss reports. |
| "I need this in OpenCode." | Run it as a local MCP server. |
| "I do not want another API bill." | No runtime API calls by default. |
OpenCode MCP
Add Context Diamond to OpenCode as a local MCP server:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"context_diamond": {
"type": "local",
"command": ["context-diamond-mcp"],
"enabled": true,
"timeout": 10000
}
}
}
OpenCode tools:
context_diamond_compress_textcontext_diamond_compress_filecontext_diamond_benchmark_file
See docs/opencode.md.
CLI
# Markdown capsule
context-diamond notes.md --budget 500 --output capsule.md
# JSON capsule for automation
context-diamond notes.md --format json --loss-report --output capsule.json
# Stdin
type notes.md | context-diamond - --budget 350
Use a JSON message list:
context-diamond conversation.json --messages-json --format json
[
{"role": "user", "content": "Build a local context compressor."},
{"role": "assistant", "content": "Decision: use deterministic extraction first."}
]
Python API
from context_diamond import CompressionConfig, ContextDiamondCompressor
text = """
Goal: reduce token waste in LLM handoffs.
The tool must run locally and avoid API keys by default.
Decision: emit markdown and JSON capsules.
"""
compressor = ContextDiamondCompressor(CompressionConfig(token_budget=220))
capsule = compressor.compress(text)
print(capsule.to_markdown())
Integration helpers:
from context_diamond import compress_documents, compress_messages, compress_tool_payload
See docs/integrations.md.
What The Capsule Looks Like
# Context Diamond Capsule
- Strategy: `diamond-v1`
- Source tokens: `535`
- Capsule tokens: `315`
- Compression ratio: `1.7x`
## Diamond Pulse
- The strongest signals from the source.
## Rules And Constraints
- Requirements that should not be violated.
## Decisions Already Made
- Choices that should not be reopened accidentally.
## Open Questions And Risks
- Unresolved items that need attention.
Why This Over X
Context Diamond is not trying to replace every prompt compressor, RAG compressor, or memory store. It is best at one job:
create auditable context capsules for LLM and coding-agent handoffs.
Read the honest comparison in docs/why-context-diamond.md.
Features
- Offline by default: no hidden network calls.
- Zero runtime dependencies: install it into boring environments.
- OpenCode-ready: ships a local stdio MCP server.
- Benchmarkable: compare against deterministic clipping baselines.
- Auditable: optional loss report shows omitted shards.
- Structured: goals, rules, decisions, facts, state, risks, anchors.
- Composable: CLI, Python API, JSON output, adapters, MCP.
Docs
Local Development
git clone https://github.com/RainCherb/context-diamond.git
cd context-diamond
python -m venv .venv
.\.venv\Scripts\activate
pip install -e ".[dev]"
python -m pytest
python -m ruff check .
On macOS or Linux, activate with source .venv/bin/activate.
Roadmap
- Larger public benchmark corpus with task-level answer quality checks.
- Optional embedding reranker for very large sources.
- Exact tokenizer extras for OpenAI, Anthropic, Gemini, and local models.
- More first-class agent adapters: GitHub issues, Linear, Slack, Markdown logs.
- Streaming capsule updates for long-running coding agents.
- PyPI release after the public API stabilizes.
Star This If
- you lose context when switching between LLM tools
- you want OpenCode agents to compress handoffs before continuing
- you prefer inspectable local tools over another black-box summarizer
- you like boring, deterministic software that saves expensive tokens
MIT licensed. Built to be small, honest, and useful.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。