Skip to content

estimateTokens counts every non-ASCII char as one token — ~2× overestimate for Cyrillic #4203

Open

Description

@belenov-maker

Environment

  • kimi-code (Node) v2.1.1, macOS arm64
  • Workload: sessions with Cyrillic-heavy content (Russian)
  • Serving: vLLM-style endpoint

Component

agent-core-v2/src/llm-adapter/contract/tokens.ts (estimateTokens; bundle ~191702 and ~323353): ceil(ascii/4) + nonAsciiCount.

Mechanism

The heuristic is calibrated for CJK (one ideograph ≈ one token), but Cyrillic actually tokenizes at ~2–2.5 characters per token. preShrinkHistoryToWindowBudget uses this estimate, while the compaction trigger counts measured tokens — the mixed units cause extra / overly deep head trimming of the history for any non-CJK, non-English language.

Facts

  • 480K real tokens vs an 806K estimate (×1.68).
  • A Russian-language session was trimmed on the very first trigger firing even though the history actually fit the call budget.

Expected

Calibrate non-ASCII (e.g. /2.5 for Cyrillic ranges) or reuse the measured token counter; at minimum, use consistent units between the trigger and the pre-shrink.

Reproduction

  1. A session with Cyrillic-heavy content on a large-window model.
  2. Compare estimateTokens output with the measured token count of the same messages (we saw 480K measured vs 806K estimated, ×1.68).
  3. Observe the history being head-trimmed by preShrinkHistoryToWindowBudget although the measured size fits the call budget.

Related issues:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions