Async request-level token measurement for @tanstack/ai-compaction? #1295
Visualizeit
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
@tanstack/ai-compactioncurrently uses a synchronous, per-messageestimateTokenscallback, withcharacterCount / 4as the default.This is inexpensive, but:
tool schemas, multimodal content, and provider framing.
onUsagearrives after the request, so it cannot protect the current call.Would it make sense to support an async, request-level token measurement hook?
I think request-level measurement would be preferable to making the existing
per-message estimator async. Calling a remote API once per message could create
many requests, while provider count APIs generally measure the complete input.
Prior art:
with local estimation and overflow recovery.
usage for overflow decisions, with estimation for compaction selection and
provider errors as a fallback.
Both approaches still need heuristics because exact preflight measurement is
not available through the compaction abstraction.
Would this fit better as:
measureTokensoption in@tanstack/ai-compaction; orcountTokenscapability consumed by compaction?All reactions