mcpField
L
Llmcache

Llmcache

本地服务
github.com/sp98记忆与知识019 周前更新

关于 Llmcache

Llmcache 是供 Claude Code 使用的 MCP 服务,将源代码文件的 AI 理解结果缓存为摘要,减少后续会话重复处理文件的开销。它通过 Git 哈希判断缓存是否过期,并支持文件及目录级摘要。

主要功能

  1. 01缓存源代码文件摘要
  2. 02按 Git 哈希使缓存失效
  3. 03生成文件与目录级理解
  4. 04提取函数或类型详情
  5. 05查看缓存命中与节省统计

适用场景

  1. 01在后续编码会话复用文件摘要
  2. 02预先索引代码库以预热缓存
  3. 03检查缓存命中和令牌节省情况
  4. 04清理过期摘要

MCP 相关

该 MCP 服务面向 Claude Code,提供文件理解、目录摘要、代码详情提取和缓存摘要列表等工具。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

暂未收录安装方式

我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。

代码仓库

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

llmcache

别再为 Claude 已经在昨天理解过的同一份代码付费。

llmcache 是一个可缓存 AI 生成的源代码理解结果的 MCP 服务。Claude 只需读取每个文件一次,此后的每次会话都会从缓存的理解结果开始,从而为你节省 Token 和费用。

为什么重要

Claude Code 使用无状态 API。每一轮都会将完整的对话历史(包括 Claude 已读取的每个文件)重新作为输入 Token 发送。一个在第 1 轮读取的 1,000 Token 文件,在本次会话之后的每一轮都会让你付出 1,000 个输入 Token。

llmcache 会用一份 50 Token 的摘要替代这个 1,000 Token 的文件。每一轮都能为你节省 950 个 Token,而不只是一次。

此外,Claude 还会生成输出 Token 来处理每个文件。有了缓存摘要,Claude 就可以完全跳过这部分工作。输出 Token 的费用是输入 Token 的 5 倍。

Without llmcache:
  Turn 1: prompt + file.go (1,000 tokens)        > you pay for 1,000
  Turn 5: prompt + file.go still in history       > you pay for 1,000 again
  Turn 10: still carrying it                      > and again

With llmcache:
  Turn 1: prompt + summary (50 tokens)            > you pay for 50
  Turn 5: summary still in history                > you pay for 50
  Turn 10: still just 50 tokens                   > 95% smaller

前提条件

  • Claude Code(唯一支持的客户端)
  • Git(用于使缓存失效)
  • 以下任一项:
    • Anthropic API 密钥(ANTHROPIC_API_KEY)
    • 已启用 Vertex AI API 的 GCP 项目,以及 gcloud auth application-default login

安装

下载适用于你平台的二进制文件:

# macOS (Apple Silicon)
curl -L -o llmcache https://github.com/sp98/llmcache-site/releases/latest/download/llmcache-darwin-arm64

# macOS (Intel)
curl -L -o llmcache https://github.com/sp98/llmcache-site/releases/latest/download/llmcache-darwin-amd64

# Linux (x86_64)
curl -L -o llmcache https://github.com/sp98/llmcache-site/releases/latest/download/llmcache-linux-amd64
chmod +x llmcache
sudo mv llmcache /usr/local/bin/

工作原理

  1. Claude Code 调用 understand_file(通过 MCP),而不是 Read
  2. llmcache 检查是否存在缓存摘要,且摘要是否仍然有效(Git 哈希匹配)
  3. 缓存命中:返回精简摘要(通常比完整文件小 90% 以上)
  4. 缓存未命中:调用 Claude Haiku 生成摘要,将其缓存后返回

文件的 Git 哈希发生变化时,摘要会自动失效。

设置

本地缓存存储在 SQLite 中。无需服务器,也无需许可证密钥。

1. 添加 MCP 服务

claude mcp add --transport stdio llmcache -- llmcache mcp

Vertex AI 用户请传入区域:

claude mcp add \
  --env CLOUD_ML_REGION=us-east5 \
  --transport stdio llmcache -- llmcache mcp

2. 添加 Read hook

此 hook 会指示 Claude 使用 understand_file,而不是直接读取源文件。将以下内容添加到 ~/.claude/settings.json:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Read",
        "hooks": [
          {
            "type": "command",
            "command": "F=$(cat | grep -o '\"file_path\":\"[^\"]*\"' | head -1 | cut -d'\"' -f4); B=${F##*/}; E=${F##*.}; case \"$B\" in Makefile|makefile|GNUmakefile) echo '{\"permissionDecision\":\"deny\",\"permissionDecisionReason\":\"Use understand_file from llmcache instead.\"}';; *) case \"$E\" in go|py|js|ts|tsx|jsx|rs|java|c|cpp|cc|h|hpp|rb|sh|proto|sql|md) echo '{\"permissionDecision\":\"deny\",\"permissionDecisionReason\":\"Use understand_file or understand_package from llmcache instead.\"}';; esac;; esac"
          }
        ]
      }
    ]
  }
}

3. 添加 CLAUDE.md 指令

将以下内容附加到 ~/.claude/CLAUDE.md:

MANDATORY: ALWAYS use understand_file or understand_package from the
llmcache MCP server instead of Read to understand source files.
Only use Read when you need exact line numbers for editing.

4. 验证

重启 Claude Code,在某个项目中使用它,然后检查节省情况:

llmcache stats
llmcache stats --detail    # per-file breakdown
llmcache stats --monthly   # month-by-month
配置

可通过环境变量或 ~/.llmcache/config.yaml 设置可选配置:

环境变量默认值说明
LLMCACHE_DB~/.llmcache/data.dbSQLite 数据库路径
LLMCACHE_LOG_DIR~/.llmcache/日志目录
LLMCACHE_SUMMARY_MODELclaude-haiku-4-5@20251001用于生成摘要的模型

团队模式(即将推出)

在单人模式下,每位开发者都要从头构建自己的缓存。当开发者 A 理解了某个文件后,开发者 B 仍需花费完整的 Token 来理解同一个文件。团队模式会共享摘要:一位开发者支付生成成本,其他人则可立即命中缓存。团队中的开发者越多,缓存预热得越快,节省也越多。

CLI 命令

命令说明
llmcache mcp启动 MCP 服务(由 Claude Code 自动启动)。
llmcache stats显示缓存命中次数,以及节省的输入/输出 Token。使用 --detail 查看每个文件的明细,使用 --monthly 查看趋势。
llmcache index <repo>为仓库预先建立索引,让首次会话就能使用已预热的缓存。使用 -w 10 设置并行工作线程,使用 --include "*.go" 进行筛选。
llmcache gc移除过期摘要。使用 --max-age 720h 设置保留期限,使用 --dry-run 预览。
MCP 工具(由 Claude Code 自动使用)
工具描述
understand_file返回源文件的缓存摘要。如果文件已更改(git 哈希不匹配),则生成新的摘要。如果文件尚未提交,则提示 Claude 直接读取。
understand_package汇总目录中的所有源文件,让 Claude 一次调用即可了解包级概况。
get_file_detail从文件中提取特定函数或类型。Go 文件使用 Go AST,其他文件使用模式搜索。
list_summaries列出仓库的所有缓存摘要,并显示命中次数和新鲜度。

问题与反馈

发现问题或有功能请求?在 GitHub 上提交 issue。

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。