Slash Multi-Query Costs Using Gemini Context Caching
Drastically reduce API costs and latency for repetitive, long-form queries.
Use Gemini's Context Caching to store massive reference documents on Google's servers once, rather than re-sending them with every API call.
The Scenario
You are building a tool for users to ask questions about a specific 50-page government proposal or legal contract. You want to avoid the high cost and latency of sending the same document text with every user message.
Before & after
Previously, you had to re-upload and re-process the entire 50-page PDF with every single follow-up question. (Rough time cost: 30-60 seconds of latency + high repeated token costs).
By using Context Caching, you only pay to store the documents once; Subsequent queries process instantly and cost significantly less. (Rough time cost: 30 seconds to set up the cache).
The Prompt
// Using the Gemini Node.js SDK
import { GoogleAICacheManager } from "@google/generative-ai/server";
const cacheManager = new GoogleAICacheManager(process.env.API_KEY);
const cache = await cacheManager.create({
model: "models/gemini-1.5-pro-001",
contents: [
{ role: "user", parts: [{ text: "[INSERT_LONG_DOCUMENT_TEXT_HERE]" }] }
],
ttlSeconds: 3600, // Valid for 1 hour
});
console.log(`Cache created: ${cache.name}`);Context Caching is particularly effective for repeated queries against massive datasets like legal documents, technical manuals, or political manifestos where the core source doesn't change.
Source
Release notes | Gemini API | Google AI for Developers"Introduced contextual caching for Gemini 1.5 Pro and Flash to optimize performance and cost for long prompts."
