Back to library
AI
tutorial

Reduce API Costs by Caching Large Documents with Gemini Context Caching

Cache massive documents once to save on every subsequent query.

Use Gemini's Explicit Caching to store large datasets (PDFs, codebases, or transcripts) in the model's memory, reducing future request costs and latency.

Google Gemini

The Scenario

You are building a chatbot that lets users ask questions about a 200-page employee handbook or a complex technical manual. Repeating that context for every single user question is slow and expensive.

Before & after

The old way

Developers previously had to resend full PDF text (e.g., 50 pages) with every user query, costing full input token rates and taking 20–30 seconds per response.

With AI

With Gemini's explicit caching, you send the data once and pay a lower storage rate, with responses arriving in seconds. Total setup takes 5–10 minutes.

The Prompt

// Using the Gemini SDK (Node.js/Python)
const cache = await client.createCachedContent({
  model: 'gemini-1.5-pro-002',
  contents: [ { role: 'user', parts: [{ text: [PASTE_LONG_DOCUMENT_HERE] }] } ],
  ttl: { seconds: 3600 } // Cache for 1 hour
});

Explicit caching allows you to manually store massive documents or system instructions on Google's servers for a specific duration (TTL). This is ideal for chatbots that focus on a static codebase, a specific legal document, or a literature set.

Source

Release notes  |  Gemini API  |  Google AI for Developers
"The Gemini API now supports context caching to reduce costs and latency for large prompts."