Skip to content

Shared token-estimation module: CHARS_PER_TOKEN is independently defined in 7 files #644

Closed

Description

@avs-io

Problem

The content-length token heuristic is copy-pasted across the codebase: CHARS_PER_TOKEN is defined independently in src/context-tree.ts, src/context-budget.ts, src/providers/kiro.ts, src/providers/cursor.ts, src/providers/warp.ts, src/providers/codex.ts, and src/providers/cursor-agent.ts, with Math.ceil(len / CHARS_PER_TOKEN) re-derived at each site. If the heuristic is ever refined (different ratio for code vs prose, per-tokenizer), it will silently apply to some providers and not others, skewing exactly the cross-provider comparisons the product exists for.

Evidence

  • grep -rln 'CHARS_PER_TOKEN = ' src --include='*.ts' lists the 7 files above.
  • Verified on main at 1a75484.

Proposed fix

One exported estimateTokensFromChars(chars: number): number in a shared leaf module; delete the local constants. Mechanical change, no behavior difference today, and it gives estimated-token provenance a single home (pairs well with #639).

Activity

  1. avs-io commented on Jul 16, 2026

    @avs-io
    CollaboratorAuthor

    Working on this. Approach as proposed: add a shared estimateTokensFromChars() in a new leaf module, migrate all 7 call sites, delete the local CHARS_PER_TOKEN constants, and add a test pinning the heuristic so the ratio can only change in one place. No behavior change intended.

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