# 智能令牌压缩

LLM 令牌很昂贵，而冗长的工具输出正是它们大多被浪费掉的地方。一个 `git status` 在一个繁忙的仓库里，一个 `cargo build` 日志、一条 600 条消息的邮件线程，一个 `docker ps -a` 针对真实集群时，这些中的每一个都可能把上下文窗口膨胀得很大，而几乎没有任何信息增益。

OpenHuman 自带 **TokenJuice**，这是对 [vincentkoc/tokenjuice](https://github.com/vincentkoc/tokenjuice) 的移植，并直接集成在工具执行路径中。在任何工具结果到达模型之前，TokenJuice 会先通过一个规则覆盖层对输出进行处理，去除噪音，保留关键信息。

## 三层规则覆盖层

规则采用 JSON 格式，并按以下顺序合并，后面的层会覆盖前面的层：

<table><thead><tr><th width="134.41796875">层</th><th>路径</th><th>用途</th></tr></thead><tbody><tr><td><strong>内置</strong></td><td>随二进制一起提供</td><td>针对 git、npm、cargo、docker、kubectl、ls 等的合理默认值。</td></tr><tr><td><strong>用户</strong></td><td><code>~/.config/tokenjuice/rules/</code></td><td>你的个人覆盖规则，适用于每个项目</td></tr><tr><td><strong>项目</strong></td><td><code>.tokenjuice/rules/</code></td><td>仓库特定的覆盖规则，已检入，与团队共享</td></tr></tbody></table>

每条规则都会命名一种工具/命令模式和一种压缩策略（截断、去重行、折叠空白、删除匹配的正则、总结各部分，……）。新规则只是 JSON 文件；无需重新编译。

## 这对内存的重要性

TokenJuice 正是让 [自动获取](/openhuman/zh/gong-neng/obsidian-wiki/auto-fetch.md) 在经济上可行。当 Gmail 提供方同步一页 200 条消息时，TokenJuice 会压缩每封规范化后的邮件 *之前* 它进入构建摘要的模型。同样的道理也适用于 GitHub diff、Slack 频道转储，以及任何其他洪流式数据源。

具体来说：通过前沿模型摄取你过去六个月的邮件，成本只需个位数美元，而不是数百美元。

## 它在流水线中的位置

```
工具调用结果
      │
      ▼
TokenJuice（分类 → 匹配规则 → 压缩）
      │
      ▼
LLM 上下文
```

实现： `src/openhuman/tokenjuice/` (`classify.rs`, `reduce.rs`, `rules/compiler.rs`, `tool_integration.rs`).

## 检查与覆盖

* 将一个 JSON 文件放入 `~/.config/tokenjuice/rules/` 即可全局添加或覆盖一条规则。
* 将一个文件放入 `.tokenjuice/rules/` 即可在仓库内针对每个项目执行相同操作。
* 使用以下命令启动核心： `RUST_LOG=openhuman_core::openhuman::tokenjuice=debug` 以查看哪些内容被匹配，以及有多少输出被裁剪掉。

## 另见

* [原生工具](/openhuman/zh/gong-neng/native-tools.md). 大多数重型工具输出都会通过 TokenJuice 流转。
* [记忆树](/openhuman/zh/gong-neng/obsidian-wiki/memory-tree.md). 压缩输出的下游消费者。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tinyhumans.gitbook.io/openhuman/zh/gong-neng/token-compression.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
