Usage-based token scale can be deflated by providers that exclude cached tokens from prompt_tokens

未关闭
#1,830 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

Start with _usage_total in mellea/stdlib/context/compactor.py, which derives the measured token count from total_tokens or prompt_tokens + completion_tokens, then read ChatContext._usage_scale in mellea/stdlib/context/chat.py where the ratio is clamped to _MIN_USAGE_SCALE. The work is investigate-and-guard: choose a plausibility check (budget cross-check, prefer prompt_tokens, or skip the correction when the total is implausibly small). Done means a guard is in place with tests covering an Anthropic-style cache-excluded usage payload.

由索引模型根据 Issue 内容生成。

描述

enhancement needs-design
What happened?

_usage_total (mellea/stdlib/context/compactor.py) derives the measured
token count from total_tokens, falling back to prompt_tokens + completion_tokens. Through providers whose usage accounting excludes
cache-read tokens from prompt_tokens
(Anthropic-style accounting, common
via LiteLLM routing), the measured total covers only the uncached suffix plus
the completion.

In ChatContext._usage_scale (mellea/stdlib/context/chat.py, from #1760),
every receipt/identity check still passes and estimated >= _MIN_MEASURED_ESTIMATE still passes, so the ratio is computed from the
deflated measurement and clamps to _MIN_USAGE_SCALE (0.5). Every history
item is then costed at half its estimate, so up to ~2x the model's real
context can be admitted within the budget — producing hard context-length API
errors. The pre-#1760 unscaled estimate was inaccurate in the same direction,
but was never multiplied down by a mismeasurement.

Ollama is not affected: its prompt_eval_count includes KV-cached prefix
tokens (verified during the #1760 review).

What did you expect to happen?

The correction should not silently over-admit history based on a measurement
that under-counts the request.

Notes
  • Scope as investigate-and-guard rather than a presumed fix. Options to
    evaluate: cross-check the measured total against the current budget before
    applying the ratio; prefer prompt_tokens as the basis when total_tokens
    is inconsistent with it; or skip the correction when the measured total is
    implausibly small relative to the rendered request.
  • Related to the "overhead is counted twice" discussion in the #1760 review
    (documented, not changed, in the merged code).
主要语言
Python
星标
1.8k
派生
161
平均合并
4 天 3 小时
30 天内合并 PR
45

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

generative-computing/mellea 的其他 Issue

查看 generative-computing/mellea 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。