countTokens()

Counts the number of tokens in a prompt before sending it. Useful for staying within context limits and estimating costs.

Syntax

gemini-api
const { totalTokens } = await model.countTokens(prompt)

Example

gemini-api
const model = genAI.getGenerativeModel({ model: "gemini-1.5-pro" });

const largeText = fs.readFileSync("./large_document.txt", "utf-8");
const { totalTokens } = await model.countTokens(largeText);

console.log(`Document has ${totalTokens} tokens`);

if (totalTokens > 900_000) {
  throw new Error(`Too large: ${totalTokens} tokens. Maximum is 1,000,000.`);
}

const result = await model.generateContent(largeText + "\n\nSummarize the key points.");
console.log(result.response.text());