计算一个上下文预算
算出 Pi 有文档记载的保留 token 阈值,并把它与一次真实的压缩运行区分开。
锁定在 Pi 1.0.4 上的压缩参考资料把触发条件记述为
contextTokens > contextWindow - reserveTokens;默认保留区是 16,384。
第 4 章的测试使用一个带 100,000 token 窗口的脚本化模型。下面就是这些
输入:窗口是一个测试模型的容量,不是每个 provider 的容量。
抄下这个计算
把这个教育用的计算存为 budget.py,然后运行 python budget.py。
它求值那个有文档记载的不等式;它不调用也不模仿 Pi 的压缩实现。
context_window = 100_000
reserve_tokens = 16_384
threshold = context_window - reserve_tokens
print(f"threshold={threshold}")
for context_tokens in (80_000, threshold, 84_000):
headroom = context_window - context_tokens
over_threshold = context_tokens > threshold
print(f"input={context_tokens} headroom={headroom} over_threshold={over_threshold}")预期结果
threshold=83616
input=80000 headroom=20000 over_threshold=False
input=83616 headroom=16384 over_threshold=False
input=84000 headroom=16000 over_threshold=True边界是严格的:相等并不越过有文档记载的阈值。 这个设置片段使用那个保留区;把它合并进你的项目设置, 而不是替换掉你的其他设置:
{
"compaction": {
"enabled": true,
"reserveTokens": 16384
}
}什么进入一次请求
| 包含的上下文 | 对预算的影响 |
|---|---|
| 系统提示词与活动工具声明 | 初始的请求开销 |
| 活动分支投影出的消息 | 对话输入 |
| 新的工具结果 | 可能增大下一条请求的大小 |
| 压缩摘要与被保留的消息 | 替换一段被总结的跨度 |
规范的 ch04-budget/budget.test.ts 通过一个真实的 AgentSession 检查
增长的上下文用量、保留阈值、活动分支历史与活动工具。它使用一个脚本化的
provider 和一个自定义摘要。这个 Python 计算是本书提出的教育材料;
它的输出是算术,不是某次真实会话在某一轮发生了压缩的证据。在一次真实
会话中,token 估计、时序和 provider 的会计都是要紧的。
规范验证
。
锁定的权威来源:docs/compaction.md 与导出的压缩声明。
Understand this example
Copy this prompt into your AI tool. No code runs here.
Apply this example
Copy this prompt into your AI tool. No code runs here.