Count tokens
Count the number of tokens in your content:- total_tokens - Total number of tokens in the content
- cached_tokens - Number of tokens from cached content (if applicable)
Compute tokens
Compute tokens is only supported in Vertex AI.
compute_tokens method provides more detailed token information:
Async token counting
Use async methods for non-blocking token counting:Local tokenizer
For offline token counting without making API calls, use the local tokenizer:Local compute tokens
Compute detailed token information locally:- Works offline without API calls
- Provides faster token counting
- Useful for preprocessing and validation
- Returns the same counts as the API