in javascript/count_tokens.js [259:301]
export async function tokensCachedContent() {
// [START tokens_cached_content]
// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const textFile = await ai.files.upload({
file: path.join(media, "a11.txt"),
config: { mimeType: "text/plain" },
});
const cache = await ai.caches.create({
model: "gemini-1.5-flash-001",
config: {
contents: createUserContent([
"Here the Apollo 11 transcript:",
createPartFromUri(textFile.uri, textFile.mimeType),
]),
system_instruction: null,
tools: null,
},
});
const prompt = "Please give a short summary of this file.";
const countTokensResponse = await ai.models.countTokens({
model: "gemini-2.0-flash",
contents: prompt,
});
console.log(countTokensResponse.totalTokens);
const generateResponse = await ai.models.generateContent({
model: "gemini-1.5-flash-001",
contents: prompt,
config: { cachedContent: cache.name },
});
console.log(generateResponse.usageMetadata);
await ai.caches.delete({ name: cache.name });
// [START tokens_cached_content]
return {
totalTokens: countTokensResponse.totalTokens,
usage: generateResponse.usageMetadata,
};
}