by 255308153
面向 AI Agent 的高并发、低延迟上下文治理与 Prompt Cache 守护网关 (Tree-sitter AST / Tool Delta / 50%~80% Token 削减)
# Add to your Claude Code skills
git clone https://github.com/255308153/CtxGuardSee how CtxGuard compares with popular alternatives.
CtxGuard is an open-source ai agents skill for AI coding assistants such as Claude Code, Codex CLI, and ChatGPT, built by 255308153. 面向 AI Agent 的高并发、低延迟上下文治理与 Prompt Cache 守护网关 (Tree-sitter AST / Tool Delta / 50%~80% Token 削减). It has 50 GitHub stars.
CtxGuard's catalog security scan is still queued. You can run an instant dependency and prompt-injection check now with the "Scan for vulnerabilities" button above.
Clone the repository with "git clone https://github.com/255308153/CtxGuard" and add it to your Claude Code skills directory (see the Installation section above).
CtxGuard is primarily written in Python. It is open-source under 255308153 on GitHub, so you can review or fork the full source.
Yes. SkillsLLM lists many other AI Agents skills you can browse and compare side by side. Open the AI Agents category from the badge at the top of this page, or use the Related Skills and comparison links further down to weigh CtxGuard against similar tools.
No comments yet. Be the first to share your thoughts!
⚠️ Third-Party Software Notice
This skill is third-party open-source software developed and hosted independently on GitHub. SkillsLLM is an informational directory and does not control or maintain the underlying repository.
Any security checks, ratings, or warnings displayed by SkillsLLM are automated and limited in scope. They do not constitute a security certification or guarantee that the software is safe, error-free, or free from malicious code, vulnerabilities, compromised dependencies, or prompt-injection risks.
Review the source code, permissions, dependencies, and configuration before installing or running any third-party skill. Use is at your own risk. To the maximum extent permitted by applicable law, SkillsLLM is not liable for losses arising from third-party software.
The deep catalog scan for this skill is still queued. Run an instant dependency check now instead.
高并发、低延迟的 AI Agent 上下文治理与智能缓存加速网关
毫秒级算子流水线 • 保证云端 KV Cache 稳定命中 • 长效时序记忆 • 零业务侵入
在现代 AI Agent(如 Claude Code、Cursor、Pi Agent、Codex)的复杂编码与长链路自动化任务中,随着工具调用的频繁执行,请求上下文呈指数级膨胀:大段未修改的代码差异、冗长的构建日志、重复的状态探测输出以及历史思考链迅速消耗宝贵的上下文窗口,产生高昂的 Token 费用;与此同时,客户端工具定义的无序序列化与动态提示词拼接极易引发服务商前缀缓存(Prompt Cache)频繁失效。
CtxGuard 是一套部署在客户端与大模型服务商(OpenAI、Anthropic、Google Gemini、xAI Grok、DeepSeek 等)之间的透明反向代理网关。它在协议中转层对出向请求实施轻量级结构化优化、语法树骨架提取与字节级防抖,在完全不损耗模型推理能力与代码逻辑的前提下,显著降低通信负载与计算开销,并为多轮会话提供自动进化的项目级经验沉淀与知识图谱记忆能力。
CtxGuard 提供了现代化的一体化监控控制台(默认访问 http://127.0.0.1:8787/dashboard),用于直观监控全局 Token 流向、会话压缩曲线、知识图谱与自动演进的规则库:
| 全局数据大盘与实时压缩曲线 | 个人记忆知识图谱与 2-hop 子图检索 |
|---|---|
![]() |
![]() |
| 请求记录明细与逐轮压缩审计 | 记忆中枢与自进化经验规则库 |
|---|---|
![]() |
![]() |

tools 列表及内部 JSON Schema 定义,统一按字母序进行确定性重排序,彻底解决各类客户端实现中字典无序导致的前缀缓存击穿。< 1ms,比纯 Python AST 快 5~10 倍;完美免疫注释括号、模板字符串及 JSX/TSX 嵌套,绝不破坏代码语法。ctx_expand(short_sha) 标记,支持模型按需随时索要完整实现。list_dir、find_by_name、git status、ls 等探针指令,在内存中维护会话维度的文件与目录快照影子状态机。+ Added(新增项)与 - Removed(删除项),将海量未变更条目折叠为 ... (K items unchanged),将 2500+ Tokens 的状态刷新压缩至 30 Tokens(Token 消耗削减 90%+ 并 100% 可逆)。npm test、cargo build、pip install 产生的数千行编译或测试长日志。thinking + signature、Gemini <thought>、DeepSeek <think>)默认实施 100% 绝对透传保真。was_cold=True)且显式配置时才执行全量基线重塑。-----BEGIN PRIVATE KEY-----)与包含密码的数据库连接字符串(postgres://user:password@host:5432/db)自动打码,杜绝凭据外泄风险。tool_calls: ctx_expand。网关本地 0.1ms 提取原文并自动在后台发起第 2 轮续写(Continuation Request),向下游客户端(Cline, RooCode, Claude Code, Cursor)彻底屏蔽虚拟工具调用过程,100% 杜绝 Tool not found 崩溃,真正实现无感可逆。Storage Invariant 2),新会话首读文件 100% 全量放行,彻底消除新会话“两眼一抹黑”的致盲隐患;SQLite 全局指纹库专注扮演 CAS(内容寻址存储)永久还原底座。Use ctx_expand 诱导信息,杜绝欺骗大模型。OrderedDict 维护热点指纹索引,读路径 100% 内存 O(1) 命中,杜绝频繁磁盘 I/O 与 SSD 写入磨损;结合批量惰性持久化削减 99% 以上磁盘事务提交。asyncio.to_thread 将 AST 解析与正则分词卸载至工作线程池,主事件循环零阻塞,多 Agent 并发无排队延迟。ContextTracker 7 重防御体系,在用户提问前置自适应识别并回填关键上下文,防范 Prompt 膨胀;严格限制仅在活区(Live Zone)末尾追加,绝不篡改历史前缀,完美守护云端 KV Cache 90%+ 稳定命中率。ClaudePlugin、CtxGuardGatewayPlugin、GeminiPlugin、CodexPlugin)。不仅能离线扫描 Claude Code、Gemini CLI、Codex 等外部工具轨迹,更能将 CtxGuard 自身网关实时中转的多 Agent 请求直接接入分析。claude -p / gemini -p / codex exec 命令行,直接复用终端现有订阅权限,无需额外配置昂贵 API Key;AGENTS.md、.cursorrules 等);新提炼规则增量更新,历史学到但本轮未触发的冷门规则自动结转保留,彻底杜绝“重新学习冲掉历史宝贵经验”的问题。# 克隆工程
git clone https://github.com/255308153/CtxGuard.git
cd CtxGuard
# 本地安装
pip install -e .
# 标准启动(默认监听 127.0.0.1:8787)
ctxguard start --port 8787
# 多核并发推荐(突破 GIL,启动 4 个 Worker 进程并行处理高并发 Agent 请求)
ctxguard start --port 8787 -w 4
服务启动后,可在浏览器中打开 Web 监控看板:http://127.0.0.1:8787/dashboard 查看实时请求流量、Token 压缩曲线与图谱状态。
# 自动在后台启动网关、自动感知原有中转地址并直接进入 Agent 终端:
ctxguard wrap claude # 一键启动 Claude Code
ctxguard wrap pi # 一键启动 Pi Agent
ctxguard wrap codex # 一键启动 Codex
Wrap 运行原理与底层工作流:
- 网关自愈探测:自动检测
8787端口是否存活,若未启动则毫秒级在后台自动静默拉起 Proxy 网关。- 上游配置自动感知 (Auto-Discovery):自动读取
~/.claude/settings.json或 Pi 等配置文件中原本保存的真实中转站 API 地址与 Key,并在网关中建立专用映射。- 进程级隔离注入 (Isolated Spawning):在内存中为即将拉起的 Agent 子进程独立注入
ANTHROPIC_BASE_URL/OPENAI_BASE_URL,完全不污染操作系统的全局环境。- 原生 TTY 终端接管:无缝透传 stdin/stdout 交互,退出 Agent 时自动完成状态清理与复盘。
# 自动设置当前终端会话的环境变量
eval $(ctxguard env --eval)
# 启动你的 Agent 即可自动享受代理加速:
claude
pi
# 自动读取并记忆各客户端原有中转与 Key,自动将客户端 Base URL 改写指向代理网关
ctxguard env --patch
在任意兼容 OpenAI 或 Anthropic 协议的工具中配置代理地址:
http://127.0.0.1:8787/v1http://127.0.0.1:8787在标准受控测试集下的细分场景评测数据:
| 评测场景与优化维度 | 原始 Token 量 | 优化后 Token 量 | 节省比例 | 处理延迟 (p50) |
|---|---|---|---|---|
| Git Diff 补丁上下文折叠 | 383 | 197 | 48.56% | 0.15 ms |
| 历史大文件重复引用 | 4,200 | 45 | 98.92% | 0.08 ms |
| 构建日志与终端控制码 | 1,850 | 320 | 82.70% | 0.12 ms |
| 多轮端到端 Coding 轨迹 | 6,735 | 5,584 | 17.09% | 0.21 ms |
| 模型输出端精简与塑造 | 1,240 (Output) | 860 (Output) | 30.70% | 0.00 ms |
基于本地 .ctxguard.db 记录的日常多 Agent 真实写代码会话累计数据:
| 核心统计指标 | 真实生产环境统计值 | 说明 |
|---|---|---|
| 拦截处理请求总量 | 10,868 次 | 涵盖日常编码、代码审查、单元测试等真实 Agent 轨迹 |
| 累计会话数 | 153 个 | 跨 Claude Code / Cursor / Codex / Pi Agent 多客户端会话 |
| 原始输入 Token 总量 | 1,742,154,963 Tokens (~17.4 亿) | 未经网关优化的原始请求上下文体积 |
| 优化后输入 Token 量 | 596,293,798 Tokens (~5.96 亿) | 经指纹去重、Diff折叠与日志清洗后的实际入参 |
| 累计净节省 Token | 1,149,299,422 Tokens (~11.5 亿) | 实际削减的纯有效上下文体积 |
| 综合上下文压缩率 | 65.97% | 平均每 1 亿 Token 上下文可压降 6,600 万+ |
| 累计规避成本 | $214.85 | 按各厂商公开输入单价折算的等效费用节省 |
| 网关中位处理延迟 | 0.14 ms | 纯算子流水线开销,对端到端时延无感知 |
| 云端前缀缓存稳定性 | 100%(Immutable) | System Prompt 与前 N 轮历史字节级绝对锁死 |
| 主流前沿模型实测 | GPT / Claude / Gemini / Grok / DeepSeek | 实测各主流旗舰模型均保持 58% ~ 88% 稳定压缩率 |
CtxGuard 各核心引擎支持在 ctxguard.yaml 中进行细粒度的独立开关控制:
# 1. 记忆图谱与偏好召回开关 (Memory & Piggyback Extraction)
piggyback_extraction:
enabled: true # 设为 false 则关闭偏好记忆提取与上下文动态注入
# 2. 自进化与规则同步开关 (Autonomous Learning & Rule Sync)
learn:
enabled: true # 设为 false 则停止死循环扫描与项目规则文件自动修改
detect_loop_threshold: 3
# 3. 输出端 Token 塑造与降噪 (Output Shaper)
output_shaper:
enabled: false # 设为 true 开启输出端客套话修剪与思考预算动态降级
level: 2 # 1: 轻度精简 | 2: 推荐标准 | 3: 高度精炼 | 4: 极限代码
# 4. 自适应调度与三级动态档位 (Adaptive Pipeline)
adaptive_pipeline:
enabled: true # 设为 false 则始终使用纯无损基础压缩
levels:
- name: "level_1" # 档位 1 (< 128k Tokens): 纯无损结构压缩 (ANSI/GitDiff/Progress)
max_tokens: 131072
compression_mode: "lossless"
- name: "level_2" # 档位 2 (128k ~ 512k Tokens): 轻量 AST 骨架提取与堆栈剪枝
max_tokens: 524288
compression_mode: "lightweight"
- name: "level_3" # 档位 3 (512k ~ 2M+ Tokens): 深度修剪与激进语义去重 (超长防爆档)
max_tokens: 2097152
compression_mode: "deep"
protected_keywords: ["CRITICAL", "FATAL", "TODO", "FIXME", "EXCEPTION"]
ctxguard wrap <agent> # 自动感知中转配置并直接一键拉起目标 Agent (claude/pi/codex)
ctxguard env --patch # 自动扫描并改写本地客户端配置指向代理网关
ctxguard stats # 查看网关当前的吞吐量、压缩效率与近期待处理请求
ctxguard savings # 查看长周期的 Token 与成本节约明细
ctxguard learn --dry-run # 预览复盘提炼的避坑经验与浪费权重(不写磁盘)
ctxguard learn --apply # 触发自进化学习,智能结转并写入项目规则 (AGENTS.md / .cursorrules)
ctxguard learn -a claude # 指定仅扫描特定 Agent 生态 (auto / claude / ctxguard / gemini / codex)
ctxguard env # 查看或导出各客户端的环境变量配置
本项目基于 MIT License 许可证开源发布。