Skip to content

Add injectable token counter; conservative default estimate - #11

Merged
ayman3000 merged 1 commit into
mainfrom
feat/token-counter-hook
Jul 27, 2026
Merged

Add injectable token counter; conservative default estimate#11
ayman3000 merged 1 commit into
mainfrom
feat/token-counter-hook

Conversation

@ayman3000

Copy link
Copy Markdown
Owner

Roadmap P1.4 — real tokenizer (#5). A multi-provider kit shouldn't hardcode one tokenizer (each provider tokenizes differently), so this adds a hook plus a safer default.

  • Conversation.tokenCounter — an optional synchronous counter that overrides the built-in heuristic used for context-window trimming. Apps can plug in an exact per-provider tokenizer (or cached count-tokens results).
  • Conservative defaultcharsPerToken 4.0 → 3.5 and a per-message framing overhead (tokensPerMessageOverhead, default 4), so trimming errs toward staying under the real window for code/JSON/non-ASCII histories (which the old heuristic under-counted).

Regression tests for the override and the overhead. 90 tests pass. Behavior change is limited to trimming being slightly more aggressive by default; both knobs are public and tunable.

🤖 Generated with Claude Code

Context-window trimming relied on a fixed ~4 chars/token heuristic that
under-counts code/JSON/non-ASCII, risking over-window prompts.

- Conversation.tokenCounter: optional synchronous hook that overrides the
  heuristic — plug in an exact per-provider tokenizer for precise trimming.
- Default heuristic is now conservative: charsPerToken 4.0 -> 3.5 plus a
  per-message overhead (tokensPerMessageOverhead, default 4).

Adds regression tests for the override and the overhead. 90 tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@ayman3000
ayman3000 merged commit ff49e41 into main Jul 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant