Skip to content

useTokenCount

useTokenCount(options): UseTokenCountReturn

Defined in: react-hooks/src/use-token-count.ts:90

useTokenCount - React hook for estimating token counts.

Provides real-time token estimation for text input, useful for staying within model context limits.

UseTokenCountOptions = {}

UseTokenCountReturn

import { useTokenCount } from 'ai.matey.react.hooks';
function TokenCounter() {
const [text, setText] = useState('');
const { tokenCount, characterCount, wordCount } = useTokenCount({ text });
return (
<div>
<textarea value={text} onChange={(e) => setText(e.target.value)} />
<p>Tokens: ~{tokenCount} | Words: {wordCount} | Characters: {characterCount}</p>
</div>
);
}