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.
Parameters
Section titled “Parameters”options
Section titled “options”UseTokenCountOptions = {}
Returns
Section titled “Returns”Example
Section titled “Example”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> );}