feat: add token counters - #12195
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sjrl
marked this pull request as ready for review
July 30, 2026 10:50
sjrl
requested review from
anakin87 and
julian-risch
and removed request for
a team and
julian-risch
July 30, 2026 10:50
anakin87
reviewed
Jul 30, 2026
Member
|
I'd like to see a short example of how you'd use this in the compaction Hook |
Contributor
Author
It's still a work in progress but you can see how these token counters would update the compaction hook here #12196 It's a new PR that builds the compaction hook off this one. |
anakin87
approved these changes
Jul 30, 2026
anakin87
left a comment
Member
There was a problem hiding this comment.
Looks good.
- I think we can further iterate on them while working on Hooks
- Let's create an issue to track documentation for these classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issues
Proposed Changes:
Added
haystack.token_counters: aTokenCounterprotocol for estimating how many tokens a list ofChatMessageobjects occupies, with two implementations.Providers report token usage only after a call, and only for the call as a whole, so anything that needs a size beforehand - deciding whether a conversation still fits a model's context window, or how much of it to drop - has to
estimate one.
ApproximateTokenCounterneeds no deps and estimates from text length at a configurablechars_per_token.TiktokenCountercounts with OpenAI's byte-pair encoder usingtiktoken.Neither can measure an image or a file, since these are text only tokenizers. Both charge a flat
tokens_per_imageandtokens_per_fileinstead, counting images a tool returned inside its result as well as those a message carries directly. Raise those values if you send large images or long documents. Similar approach as LC.Follow-up: provider-specific token counters
Every major provider exposes an endpoint that counts exactly, including images and tool schemas, at the cost of a network round trip. The
TokenCounterprotocol exists so these can be added as separate implementations:POST /v1/responses/input_tokens(client.responses.input_tokens.count)client.messages.count_tokens()models.countTokensModalityTokenCountCountTokensHow did you test it?
New tests
Notes for the reviewer
If we think this route makes sense I can open up issues for adding token counters for each of the different providers.
Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.