-
Notifications
You must be signed in to change notification settings - Fork 1
Token Meter
Edge adaptation of the upstream per-session token measurement system.
Upstream reference: Token Meter
TokenMeter (ctx.tokenMeter) provides isolated per-session token measurement and request pressure tracking. It produces detached immutable snapshots (TokenMeasurement) at specific consumption points during the agent loop.
- Two baseline modes: usage-based (reuses provider-reported token counts from successful LLM calls) and estimated (heuristic pricing when no provider data is available).
-
Surface delta tracking: signed
surfaceDeltaTokensmeasure growth or reduction relative to the matched anchor, letting compaction and UI know how context pressure is changing. - Per-node pricing: each session surface node gets a heuristic token estimate, enabling the "context used" indicator and compaction triggers.
The token meter does not call LLM providers — it reads provider usage from successful request headers and applies heuristics between calls. It is a passive measurement service consumed by compaction, the system prompt budget, and client UI.
TokenMeter is installed with no configuration and no Edge-specific code. It reads token usage from request/header session events emitted by the DeepSeek LLM adapter — the same event format upstream produces. The measurement API (measure(), estimateMessage()) works identically.
The client-side token meter UI (context usage percentage, input/output token counts, cache hit rate) is embedded in dsh-client-ui-conversation, one of the 33 client plugins included in the Edge assembly. No standalone token meter client plugin exists — the display is part of the conversation footer.
- Measurement strategy (usage-based vs estimated baselines)
- Surface delta tracking and per-node pricing
- Heuristic token estimation rules
- Integration with compaction engine and system prompt budget
- Client-side context usage display
measure() replays the durable event tail since the last anchor point. Cost is O(surface nodes) — typically 10–50 nodes for a normal conversation. The replay is synchronous and cached; repeated calls within the same turn reuse the result. No I/O, no LLM calls.
Between provider calls, token counts are heuristic estimates. After each successful LLM call, the provider-reported usage replaces the heuristic baseline. The signed delta preserves accuracy across the transition. For DeepSeek models, the provider reports exact token counts in the response header.
| Component | Category | Edge Code|
| TokenMeter service | One ctx.plugin() call, no config |
|
|---|---|---|
| Token usage display | Part of conversation UI plugin |
Key observation: TokenMeter is one of the simplest upstream integrations — zero Edge code, zero configuration. It passively reads provider usage data from session events and applies heuristics. The entire measurement and display pipeline is upstream.
上游逐 session token 计量系统在 Edge 中的适配。
上游参考:Token Meter
TokenMeter(ctx.tokenMeter)提供隔离的逐 session token 计量和请求压力追踪。在 agent 循环的特定消费点生成不可变快照(TokenMeasurement)。
- 两种基线模式:usage-based(复用成功 LLM 调用的 provider 报告 token 数)和 estimated(无 provider 数据时的启发式定价)。
-
Surface delta 追踪:有符号的
surfaceDeltaTokens测量相对锚点的增减,让压缩和 UI 知道上下文压力如何变化。 - 逐节点定价:每个 session surface 节点获得启发式 token 估计,支撑"上下文使用量"指示器和压缩触发。
Token meter 不调用 LLM provider——它从成功请求的 header 中读取 provider usage,在调用间隙应用启发式估算。它是被压缩引擎、系统提示词预算和客户端 UI 消费的被动计量服务。
TokenMeter 无配置安装,无 Edge 专属代码。它从 DeepSeek LLM 适配器发出的 request/header session 事件中读取 token usage——与上游产生的事件格式相同。计量 API(measure()、estimateMessage())行为一致。
客户端 token meter UI(上下文使用百分比、输入/输出 token 数、cache 命中率)嵌入在 dsh-client-ui-conversation 中,是 Edge 组装的 33 个客户端插件之一。没有独立的 token meter 客户端插件——显示是会话页脚的一部分。
- 计量策略(usage-based vs estimated 基线)
- Surface delta 追踪和逐节点定价
- 启发式 token 估算规则
- 与压缩引擎和系统提示词预算的集成
- 客户端上下文使用量显示
measure() 从上一个锚点开始重放持久事件尾部。开销 O(surface 节点数)——普通对话通常 10–50 个节点。重放是同步的且有缓存;同一 turn 内的重复调用复用结果。无 I/O,无 LLM 调用。
Provider 调用间隙的 token 数是启发式估算。每次成功的 LLM 调用后,provider 报告的 usage 替换启发式基线。有符号 delta 保证过渡期的精度。DeepSeek 模型在响应 header 中报告精确的 token 数。
| 组件 | 分类 | Edge 代码|
| TokenMeter 服务 | 一行 ctx.plugin(),无配置 |
|
|---|---|---|
| Token 用量显示 | 会话 UI 插件的一部分 |
关键观察:TokenMeter 是最简单的上游集成之一——零 Edge 代码,零配置。它被动地从 session 事件中读取 provider usage 数据并应用启发式估算。整个计量和显示管线都是上游。
- Home
- Architecture
- Core & Scope
- Session & Persistence
- Model & Context
-
Execution & Tools
- Tools
- Bash
- Subprocess 🚫
- PTY Session 🚫
- Background Jobs 🚫
- Filesystem
- LSP Navigation 🚫
- Code Runtime 🚫
-
Web Access
⚠️ -
Skills
⚠️ - Workflow 🚫
- Subagent 🚫
-
Policy & Interaction
- Goal
- Approval 🚫
- Permission Presets 🚫
-
Sandbox
⚠️ - Plan Mode 🚫
- User Interaction 🚫
- Commands 🚫
- Schedule 🚫
- Message Feedback 🚫
- Platform & Access
- Development
- 首页
- 架构
- 核心与作用域
- 会话与持久化
- 模型与上下文
- 执行与工具
- 策略与交互
- 平台与接入
- 开发