> ## AI Agent Context
> - Full documentation index: https://aistudio.google.com/llms.txt
> - Gemini Docs MCP server (consistent live info): https://gemini-api-docs-mcp.dev
> - Coding agent skills & setup guide: https://aistudio.google.com/docs/coding-agents.md

# Context caching

AI workflows often send the same system instructions, tool schemas, files, or conversation turns across requests. The Gemini API offers two caching options on `generateContent`:

* Implicit caching: Enabled by default on Gemini 2.5 and newer models with no setup calls or storage fees. Cached input tokens cost 95% less on Gemini 4 models and up to 90% less on Gemini 3 and 2.5 models, with no cost-saving guarantee.
* Explicit caching: Available on Gemini 3 and Gemini 2.5 models when you want guaranteed cache retention with a custom TTL and prorated storage billing. Not supported on Gemini 4 models.

## Implicit caching  {% id="implicit-caching" %}

Implicit caching is enabled by default on all Gemini 2.5 and newer models. When a request matches a cached prefix above the model's token floor, the API bills those tokens at the cached input rate. You pay no storage fees and no cache-write surcharges.

| Model | Minimum input tokens | Cache discount | Storage fee |
| :--- | :--- | :--- | :--- |
| Gemini 4 Neon (`gemini-4-neon`) | 4,096 | 95% (all tiers) | None |
| Gemini 4 Carbon (`gemini-4-carbon`) | 4,096 | 95% (all tiers) | None |
| Gemini 4 Hydrogen (`gemini-4-hydrogen`) | 4,096 | 95% (all tiers) | None |
| Gemini 4 Argon (`gemini-4-argon`) | 4,096 | 95% (all tiers) | None |
| Gemini 3.8 Flash (`gemini-3.8-flash`) | 4,096 | 90% (all tiers) | None |
| Gemini 3.6 Flash (`gemini-3.6-flash`) | 4,096 | 90% (all tiers) | None |
| Gemini 3.1 Pro Preview (`gemini-3.1-pro-preview`) | 4,096 | 90% (Standard, Priority), 80% (Flex, Batch) | None |

Gemini 4, Gemini 3.8 Flash, and Gemini 3.6 Flash apply their full discount across Standard, Flex, Batch, and Priority tiers. On Gemini 3.1 Pro Preview, the 90% discount applies to Standard and Priority tiers. On Flex and Batch tiers, Gemini 3.1 Pro Preview bills cache hits at the Standard cached rate (80% off Flex and Batch input). For per-million-token rates across tiers, see [Pricing](/docs/pricing).

Implicit caching is opportunistic and offers no guaranteed retention window or cost savings. Implicit cache entries stay in memory (RAM), not on disk, and remain isolated to your project. Each entry has a best-effort TTL of up to 24 hours, and the server evicts entries dynamically based on capacity and request routing. Because eviction is not predictable, sending priming or periodic keep-alive requests does not guarantee that a prefix stays cached. Send requests with shared prefixes close together in time. In-memory caching works with [zero data retention (ZDR)](/docs/zdr) because cached prefixes are never stored at rest.

{% callout type="note" %}
  **Shared prefixes and the 2,050-token window on Gemini 4:** Independent stateless requests that share an identical prefix (such as system instructions, tool definitions, and reference documents followed by different user messages) can share an implicit cache entry without extending a full earlier request. Gemini 4 always processes at least the last 2,050 tokens of every request at the standard input rate. In multi-turn and agent loops, Gemini caches the full previous request only when the new turn adds at least 2,050 tokens (including model output, thinking tokens, tool calls, tool results, and new user input). For smaller additions, Gemini reprocesses the last 2,050 tokens of the previous request at the standard input rate.

{% /callout %}

### Prompt-prefix ordering  {% id="prompt-prefix-ordering" %}

Implicit caching matches your tokenized request from the first token onward. Gemini 4 serializes `generateContent` requests in this fixed order from top to bottom:

1. Model configuration and built-in server tools. `thinking_config` comes first, followed by generic structured-output format instructions (plus inline enum values for `text/x.enum`), then server tools like `code_execution`, `google_search`, and `url_context`.
2. Tools and function declarations, placed before `system_instruction`.
3. Developer `system_instruction`.
4. Conversation history and user `contents`, oldest turns first. Server-injected grounding context and any schema from `response_schema` or `response_json_schema` land right before the latest user turn.

Changing `thinking_level` invalidates 100% of your cache. Adding, removing, or reordering `tools` invalidates both `system_instruction` and the entire conversation history. Keep static configuration, tools, instructions, and shared files fixed at the top, and put timestamps, request IDs, and user queries at the end of `contents`. For the full ordering guide and cache-invalidator table, see [Prompt-prefix engineering for Gemini 4](/docs/caching#prompt-prefix-engineering).

Run two consecutive `generateContent` calls over a shared prefix of at least 4,096 tokens, then read `usage_metadata.cached_content_token_count` (Python) or `usageMetadata.cachedContentTokenCount` (JavaScript and REST) to inspect the cache hit:

{% tabs %}
  {% tab label="Python" %}
  ```python
  from google import genai
  from google.genai import types
  
  client = genai.Client()
  
  # Build a shared prefix above the 4,096-token minimum for Gemini 4.
  shared_manual = "\n".join(
      f"Policy {i}: Standard SLA response time is 4 hours for tier {i % 3 + 1} "
      "accounts. Escalate billing disputes above $5,000 to regional finance."
      for i in range(1, 351)
  )
  
  config = types.GenerateContentConfig(
      system_instruction="Answer policy questions concisely using only the manual.",
      thinking_config=types.ThinkingConfig(thinking_level="high"),
  )
  
  # First call populates the implicit cache.
  client.models.generate_content(
      model="gemini-4-neon",
      contents=f"{shared_manual}\n\nQuestion: What is the SLA response time?",
      config=config,
  )
  
  # Second call reuses the identical prefix.
  response = client.models.generate_content(
      model="gemini-4-neon",
      contents=f"{shared_manual}\n\nQuestion: Where do billing disputes above $5,000 go?",
      config=config,
  )
  
  cached = response.usage_metadata.cached_content_token_count or 0
  prompt_tokens = response.usage_metadata.prompt_token_count or 1
  print(f"Answer: {response.text}")
  print(f"Cached tokens: {cached} / {prompt_tokens} ({cached / prompt_tokens:.1%})")
  ```

  {% /tab %}

  {% tab label="JavaScript" %}
  ```javascript
  import { GoogleGenAI, ThinkingLevel } from "@google/genai";
  
  const ai = new GoogleGenAI({});
  
  // Build a shared prefix above the 4,096-token minimum for Gemini 4.
  const sharedManual = Array.from(
    { length: 350 },
    (_, idx) =>
      `Policy ${idx + 1}: Standard SLA response time is 4 hours for tier ${(idx % 3) + 1} accounts. Escalate billing disputes above $5,000 to regional finance.`
  ).join("\n");
  
  const config = {
    systemInstruction: "Answer policy questions concisely using only the manual.",
    thinkingConfig: { thinkingLevel: ThinkingLevel.HIGH },
  };
  
  async function main() {
    // First call populates the implicit cache.
    await ai.models.generateContent({
      model: "gemini-4-neon",
      contents: `${sharedManual}\n\nQuestion: What is the SLA response time?`,
      config,
    });
  
    // Second call reuses the identical prefix.
    const response = await ai.models.generateContent({
      model: "gemini-4-neon",
      contents: `${sharedManual}\n\nQuestion: Where do billing disputes above $5,000 go?`,
      config,
    });
  
    const cached = response.usageMetadata?.cachedContentTokenCount ?? 0;
    const promptTokens = response.usageMetadata?.promptTokenCount ?? 0;
    const hitRate = promptTokens > 0 ? ((cached / promptTokens) * 100).toFixed(1) : "0.0";
    console.log("Answer:", response.text);
    console.log(`Cached tokens: ${cached} / ${promptTokens} (${hitRate}%)`);
  }
  
  await main();
  ```

  {% /tab %}

  {% tab label="REST" %}
  ```bash
  # Build a shared prefix above the 4,096-token minimum for Gemini 4.
  SHARED_MANUAL=$(awk 'BEGIN {
    for (i = 1; i <= 350; i++) {
      printf "Policy %d: Standard SLA response time is 4 hours for tier %d accounts. Escalate billing disputes above $5,000 to regional finance.\n", i, (i % 3) + 1
    }
  }')
  SYSTEM_INSTRUCTION="Answer policy questions concisely using only the manual."
  PROMPT_1="${SHARED_MANUAL}"$'\n\n''Question: What is the SLA response time?'
  PROMPT_2="${SHARED_MANUAL}"$'\n\n''Question: Where do billing disputes above $5,000 go?'
  
  # First call populates the implicit cache.
  jq -n \
    --arg sys "$SYSTEM_INSTRUCTION" \
    --arg text "$PROMPT_1" \
    '{
      systemInstruction: { parts: [{ text: $sys }] },
      generationConfig: { thinkingConfig: { thinkingLevel: "high" } },
      contents: [{ role: "user", parts: [{ text: $text }] }]
    }' \
  | curl -s -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-4-neon:generateContent" \
      -H "x-goog-api-key: $GEMINI_API_KEY" \
      -H "Content-Type: application/json" \
      -d @- > /dev/null
  
  # Second call reuses the identical prefix.
  RESPONSE=$(jq -n \
    --arg sys "$SYSTEM_INSTRUCTION" \
    --arg text "$PROMPT_2" \
    '{
      systemInstruction: { parts: [{ text: $sys }] },
      generationConfig: { thinkingConfig: { thinkingLevel: "high" } },
      contents: [{ role: "user", parts: [{ text: $text }] }]
    }' \
  | curl -s -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-4-neon:generateContent" \
      -H "x-goog-api-key: $GEMINI_API_KEY" \
      -H "Content-Type: application/json" \
      -d @-)
  
  echo "$RESPONSE" | jq '{
    cached_tokens: (.usageMetadata.cachedContentTokenCount // 0),
    prompt_tokens: (.usageMetadata.promptTokenCount // 0),
    hit_rate_pct: (if (.usageMetadata.promptTokenCount // 0) > 0 then (((.usageMetadata.cachedContentTokenCount // 0) / .usageMetadata.promptTokenCount) * 100 | round) else 0 end)
  }'
  ```

  {% /tab %}

{% /tabs %}

Moving from explicit `cachedContents` to Gemini 4 implicit caching? See [Migrating from explicit caching](/docs/caching#migrating-from-explicit-caching) for a step-by-step checklist and before-and-after code samples.

## Explicit caching  {% id="explicit-caching" %}

{% callout type="note" %}
  Explicit context caching is currently in Beta. Endpoints and SDK methods are
  available under `v1beta`. Explicit caching is not supported for Gemini 4 models; Gemini 4 models use
  [implicit caching](/docs/caching#implicit-caching) by default.

{% /callout %}

Using the Gemini API explicit caching feature, you can pass some content
to the model once, cache the input tokens, and then refer to the cached tokens
for subsequent requests. At certain volumes, using cached tokens is lower cost
than passing in the same corpus of tokens repeatedly.

When you cache a set of tokens, you can choose how long you want the cache to
exist before the tokens are automatically deleted. This caching duration is
called the *time to live* (TTL). If not set, the TTL defaults to 1 hour. The
cost for caching depends on the input token size and how long you want the
tokens to persist.

This section assumes that you've installed a Gemini SDK (or have curl installed)
and that you've configured an API key, as shown in the
[Get started guide](/docs/get-started).

### Generate content using a cache  {% id="generate-content" %}

{% tabs %}
  {% tab label="Python" %}
  The following example shows how to generate content using a cached system
    instruction and video file.

  {% tabs %}
  {% tab label="Videos" %}
  ```python
  import os
  import pathlib
  import requests
  import time
  
  from google import genai
  from google.genai import types
  
  client = genai.Client(http_options={"api_version": "v1beta"})
  
  # Download a test video file and save it locally
  url = 'https://storage.googleapis.com/generativeai-downloads/data/SherlockJr._10min.mp4'
  path_to_video_file = pathlib.Path('SherlockJr._10min.mp4')
  if not path_to_video_file.exists():
      path_to_video_file.write_bytes(requests.get(url).content)
  
  # Upload the video using the Files API
  video_file = client.files.upload(file=path_to_video_file)
  
  # Wait for the file to finish processing
  while video_file.state.name == 'PROCESSING':
      time.sleep(2.5)
      video_file = client.files.get(name=video_file.name)
  
  print(f'Video processing complete: {video_file.uri}')
  
  model='models/gemini-3.8-flash'
  
  # Create a cache with a 5 minute TTL (300 seconds)
  cache = client.caches.create(
      model=model,
      config=types.CreateCachedContentConfig(
          display_name='sherlock jr movie', # used to identify the cache
          system_instruction=(
              'You are an expert video analyzer, and your job is to answer '
              'the user\'s query based on the video file you have access to.'
          ),
          contents=[video_file],
          ttl="300s",
      )
  )
  
  response = client.models.generate_content(
      model = model,
      contents= (
      'Introduce different characters in the movie by describing '
      'their personality, looks, and names. Also list the timestamps '
      'they were introduced for the first time.'),
      config=types.GenerateContentConfig(cached_content=cache.name)
  )
  
  print(response.usage_metadata)
  
  print(response.text)
  ```

  {% /tab %}

  {% tab label="PDFs" %}
  ```python
  from google import genai
  from google.genai import types
  import io
  import httpx
  
  client = genai.Client(http_options={"api_version": "v1beta"})
  
  long_context_pdf_path = "https://sma.nasa.gov/SignificantIncidents/assets/a11_missionreport.pdf"
  
  # Retrieve and upload the PDF using the File API
  doc_io = io.BytesIO(httpx.get(long_context_pdf_path).content)
  
  document = client.files.upload(
    file=doc_io,
    config=dict(mime_type='application/pdf')
  )
  
  model_name = "gemini-3.8-flash"
  system_instruction = "You are an expert analyzing transcripts."
  
  # Create a cached content object
  cache = client.caches.create(
      model=model_name,
      config=types.CreateCachedContentConfig(
        system_instruction=system_instruction,
        contents=[document],
      )
  )
  
  print(f'{cache=}')
  
  response = client.models.generate_content(
    model=model_name,
    contents="Please summarize this transcript",
    config=types.GenerateContentConfig(
      cached_content=cache.name
    ))
  
  print(f'{response.usage_metadata=}')
  
  print('\n\n', response.text)
  ```

  {% /tab %}

  {% /tabs %}

  {% /tab %}

  {% tab label="JavaScript" %}
  The following example shows how to generate content using a cached system
    instruction and a text file.

  ```javascript
  import {
    GoogleGenAI,
    createUserContent,
    createPartFromUri,
  } from "@google/genai";
  
  const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY", httpOptions: { apiVersion: "v1beta" } });
  
  async function main() {
    const doc = await ai.files.upload({
      file: "path/to/file.txt",
      config: { mimeType: "text/plain" },
    });
    console.log("Uploaded file name:", doc.name);
  
    const modelName = "gemini-3.8-flash";
    const cache = await ai.caches.create({
      model: modelName,
      config: {
        contents: createUserContent(createPartFromUri(doc.uri, doc.mimeType)),
        systemInstruction: "You are an expert analyzing transcripts.",
      },
    });
    console.log("Cache created:", cache);
  
    const response = await ai.models.generateContent({
      model: modelName,
      contents: "Please summarize this transcript",
      config: { cachedContent: cache.name },
    });
    console.log("Response text:", response.text);
  }
  
  await main();
  ```

  {% /tab %}

  {% tab label="Go" %}
  The following example shows how to generate content using a cache.

  ```go
  package main
  
  import (
      "context"
      "fmt"
      "log"
  
      "google.golang.org/genai"
  )
  
  func main() {
      ctx := context.Background()
      client, err := genai.NewClient(ctx, &genai.ClientConfig{
          HTTPOptions: genai.HTTPOptions{APIVersion: "v1beta"},
          APIKey: "GOOGLE_API_KEY",
          Backend: genai.BackendGeminiAPI,
      })
      if err != nil {
          log.Fatal(err)
      }
  
      modelName := "gemini-3.8-flash"
      document, err := client.Files.UploadFromPath(
          ctx,
          "media/a11.txt",
          &genai.UploadFileConfig{
            MIMEType: "text/plain",
          },
      )
      if err != nil {
          log.Fatal(err)
      }
      parts := []*genai.Part{
          genai.NewPartFromURI(document.URI, document.MIMEType),
      }
      contents := []*genai.Content{
          genai.NewContentFromParts(parts, genai.RoleUser),
      }
      cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
          Contents: contents,
          SystemInstruction: genai.NewContentFromText(
            "You are an expert analyzing transcripts.", genai.RoleUser,
          ),
      })
      if err != nil {
          log.Fatal(err)
      }
      fmt.Println("Cache created:")
      fmt.Println(cache)
  
      // Use the cache for generating content.
      response, err := client.Models.GenerateContent(
          ctx,
          modelName,
          genai.Text("Please summarize this transcript"),
          &genai.GenerateContentConfig{
            CachedContent: cache.Name,
          },
      )
      if err != nil {
          log.Fatal(err)
      }
      printResponse(response) // helper for printing response parts
  }
  ```

  {% /tab %}

  {% tab label="REST" %}
  The following example shows how to create a cache and then use it to
    generate content.

  {% tabs %}
  {% tab label="Videos" %}
  ```shell
  wget https://storage.googleapis.com/generativeai-downloads/data/a11.txt
  echo '{
    "model": "models/gemini-3.8-flash",
    "contents":[
      {
        "parts":[
          {
            "inline_data": {
              "mime_type":"text/plain",
              "data": "'"$(base64 a11.txt | tr -d '\n')"'"
            }
          }
        ],
      "role": "user"
      }
    ],
    "systemInstruction": {
      "parts": [
        {
          "text": "You are an expert at analyzing transcripts."
        }
      ]
    },
    "ttl": "300s"
  }' > request.json
  
  curl -X POST "https://generativelanguage.googleapis.com/v1beta/cachedContents" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d @request.json \
  > cache.json
  
  CACHE_NAME=$(cat cache.json | grep '"name":' | cut -d '"' -f 4 | head -n 1)
  
  curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.8-flash:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
        "contents": [
          {
            "parts":[{
              "text": "Please summarize this transcript"
            }],
            "role": "user"
          }
        ],
        "cachedContent": "'"$CACHE_NAME"'"
      }'
  ```

  {% /tab %}

  {% tab label="PDFs" %}
  ```shell
  DOC_URL="https://sma.nasa.gov/SignificantIncidents/assets/a11_missionreport.pdf"
  DISPLAY_NAME="A11_Mission_Report"
  SYSTEM_INSTRUCTION="You are an expert at analyzing transcripts."
  PROMPT="Please summarize this transcript"
  MODEL="models/gemini-3.8-flash"
  TTL="300s"
  
  # Download the PDF
  wget -O "${DISPLAY_NAME}.pdf" "${DOC_URL}"
  
  MIME_TYPE=$(file -b --mime-type "${DISPLAY_NAME}.pdf")
  NUM_BYTES=$(wc -c < "${DISPLAY_NAME}.pdf")
  
  echo "MIME_TYPE: ${MIME_TYPE}"
  echo "NUM_BYTES: ${NUM_BYTES}"
  
  tmp_header_file=upload-header.tmp
  
  # Initial resumable request defining metadata.
  # The upload url is in the response headers dump them to a file.
  curl "https://generativelanguage.googleapis.com/upload/v1beta/files" \
    -H "x-goog-api-key: $GEMINI_API_KEY" \
    -D upload-header.tmp \
    -H "X-Goog-Upload-Protocol: resumable" \
    -H "X-Goog-Upload-Command: start" \
    -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
    -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
    -H "Content-Type: application/json" \
    -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null
  
  upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
  rm "${tmp_header_file}"
  
  # Upload the actual bytes.
  curl "${upload_url}" \
    -H "Content-Length: ${NUM_BYTES}" \
    -H "X-Goog-Upload-Offset: 0" \
    -H "X-Goog-Upload-Command: upload, finalize" \
    --data-binary "@${DISPLAY_NAME}.pdf" 2> /dev/null > file_info.json
  
  file_uri=$(jq ".file.uri" file_info.json)
  echo "file_uri: ${file_uri}"
  
  # Clean up the downloaded PDF
  rm "${DISPLAY_NAME}.pdf"
  
  # Create the cached content request
  echo '{
    "model": "'"$MODEL"'",
    "contents":[
      {
        "parts":[
          {"file_data": {"mime_type": "'"$MIME_TYPE"'", "file_uri": '"$file_uri"'}}
        ],
      "role": "user"
      }
    ],
    "system_instruction": {
      "parts": [
        {
          "text": "'"$SYSTEM_INSTRUCTION"'"
        }
      ],
      "role": "system"
    },
    "ttl": "'"$TTL"'"
  }' > request.json
  
  # Send the cached content request
  curl -X POST "https://generativelanguage.googleapis.com/v1beta/cachedContents" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d @request.json \
  > cache.json
  
  CACHE_NAME=$(cat cache.json | grep '"name":' | cut -d '"' -f 4 | head -n 1)
  echo "CACHE_NAME: ${CACHE_NAME}"
  # Send the generateContent request using the cached content
  curl -X POST "https://generativelanguage.googleapis.com/v1beta/${MODEL}:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
        "contents": [
          {
            "parts":[{
              "text": "'"$PROMPT"'"
            }],
            "role": "user"
          }
        ],
        "cachedContent": "'"$CACHE_NAME"'"
      }' > response.json
  
  cat response.json
  
  jq ".candidates[].content.parts[].text" response.json
  ```

  {% /tab %}

  {% /tabs %}

  {% /tab %}

{% /tabs %}

### List caches  {% id="list-caches" %}

It's not possible to retrieve or view cached content, but you can retrieve
cache metadata (`name`, `model`, `display_name`, `usage_metadata`,
`create_time`, `update_time`, and `expire_time`).

{% tabs %}
  {% tab label="Python" %}
  To list metadata for all uploaded caches, use `CachedContent.list()`:

  ```Python
  for cache in client.caches.list():
    print(cache)
  ```

  To fetch the metadata for one cache object, if you know its name, use `get`:

  ```Python
  client.caches.get(name=name)
  ```

  {% /tab %}

  {% tab label="JavaScript" %}
  To list metadata for all uploaded caches, use `GoogleGenAI.caches.list()`:

  ```javascript
  console.log("My caches:");
  const pager = await ai.caches.list({ config: { pageSize: 10 } });
  let page = pager.page;
  while (true) {
    for (const c of page) {
      console.log("    ", c.name);
    }
    if (!pager.hasNextPage()) break;
    page = await pager.nextPage();
  }
  ```

  {% /tab %}

  {% tab label="Go" %}
  The following example lists all caches.

  ```go
  caches, err := client.Caches.All(ctx)
  if err != nil {
      log.Fatal(err)
  }
  fmt.Println("Listing all caches:")
  for _, item := range caches {
      fmt.Println("   ", item.Name)
  }
  ```

  The following example lists caches using a page size of 2.

  ```go
  page, err := client.Caches.List(ctx, &genai.ListCachedContentsConfig{PageSize: 2})
  if err != nil {
      log.Fatal(err)
  }
  
  pageIndex := 1
  for {
      fmt.Printf("Listing caches (page %d):\n", pageIndex)
      for _, item := range page.Items {
          fmt.Println("   ", item.Name)
      }
      if page.NextPageToken == "" {
          break
      }
      page, err = page.Next(ctx)
      if err == genai.ErrPageDone {
          break
      } else if err != nil {
          return err
      }
      pageIndex++
  }
  ```

  {% /tab %}

  {% tab label="REST" %}
  ```shell
  curl "https://generativelanguage.googleapis.com/v1beta/cachedContents" \
  -H "x-goog-api-key: $GEMINI_API_KEY"
  ```

  {% /tab %}

{% /tabs %}

### Update a cache  {% id="update-cache" %}

You can set a new `ttl` or `expire_time` for a cache. Changing anything else
about the cache isn't supported.

{% tabs %}
  {% tab label="Python" %}
  The following example shows how to update the `ttl` of a cache using
    `client.caches.update()`.

  ```python
  from google import genai
  from google.genai import types
  
  client.caches.update(
    name = cache.name,
    config  = types.UpdateCachedContentConfig(
        ttl='300s'
    )
  )
  ```

  To set the expiry time, it will accepts either a `datetime` object
    or an ISO-formatted datetime string (`dt.isoformat()`, like
    `2025-01-27T16:02:36.473528+00:00`). Your time must include a time zone
    (`datetime.utcnow()` doesn't attach a time zone,
    `datetime.now(datetime.timezone.utc)` does attach a time zone).

  ```Python
  from google import genai
  from google.genai import types
  import datetime
  
  # You must use a time zone-aware time.
  in10min = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(minutes=10)
  
  client.caches.update(
    name = cache.name,
    config  = types.UpdateCachedContentConfig(
        expire_time=in10min
    )
  )
  ```

  {% /tab %}

  {% tab label="JavaScript" %}
  The following example shows how to update the `ttl` of a cache using
    `GoogleGenAI.caches.update()`.

  ```javascript
  const ttl = `${2 * 3600}s`; // 2 hours in seconds
  const updatedCache = await ai.caches.update({
    name: cache.name,
    config: { ttl },
  });
  console.log("After update (TTL):", updatedCache);
  ```

  {% /tab %}

  {% tab label="Go" %}
  The following example shows how to update the `TTL` of a cache.

  ```go
  // Update the TTL (2 hours).
  cache, err = client.Caches.Update(ctx, cache.Name, &genai.UpdateCachedContentConfig{
      TTL: 7200 * time.Second,
  })
  if err != nil {
      log.Fatal(err)
  }
  fmt.Println("After update:")
  fmt.Println(cache)
  ```

  {% /tab %}

  {% tab label="REST" %}
  The following example shows how to update the `ttl` of a cache.

  ```shell
  curl -X PATCH "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"ttl": "600s"}'
  ```

  {% /tab %}

{% /tabs %}

### Delete a cache  {% id="delete-cache" %}

The caching service provides a delete operation for manually removing content
from the cache. The following example shows how to delete a cache:

{% tabs %}
  {% tab label="Python" %}
  ```python
  client.caches.delete(cache.name)
  ```

  {% /tab %}

  {% tab label="JavaScript" %}
  ```javascript
  await ai.caches.delete({ name: cache.name });
  ```

  {% /tab %}

  {% tab label="Go" %}
  ```go
  _, err = client.Caches.Delete(ctx, cache.Name, &genai.DeleteCachedContentConfig{})
  if err != nil {
      log.Fatal(err)
  }
  fmt.Println("Cache deleted:", cache.Name)
  ```

  {% /tab %}

  {% tab label="REST" %}
  ```shell
  curl -X DELETE "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME" \
  -H "x-goog-api-key: $GEMINI_API_KEY"
  ```

  {% /tab %}

{% /tabs %}

### Explicit caching using the OpenAI library  {% id="caching-using-openai" %}

If you're using an [OpenAI library](/docs/openai), you can enable
explicit caching using the `cached_content` property on
[`extra_body`](/docs/openai#extra-body).

## When to use explicit caching  {% id="when-to-use-caching" %}

Context caching is particularly well suited to scenarios where a substantial
initial context is referenced repeatedly by shorter requests. Consider using
context caching for use cases such as:

* Chatbots with extensive [system instructions](/docs/text-generation)
* Repetitive analysis of lengthy video files
* Recurring queries against large document sets
* Frequent code repository analysis or bug fixing

### How explicit caching reduces costs  {% id="cost-efficiency" %}

Context caching is a paid feature designed to reduce cost. Billing is based on
the following factors:

1. **Cache token count:** The number of input tokens cached, billed at a
   reduced rate when included in subsequent prompts.

2. **Storage duration:** The amount of time cached tokens are stored (TTL),
   billed based on the TTL duration of cached token count. There are no minimum
   or maximum bounds on the TTL.

3. **Other factors:** Other charges apply, such as for non-cached input tokens
   and output tokens.


For up-to-date pricing details, refer to the Gemini API [pricing
page](/docs/pricing). To learn how to count tokens, see the [Token
guide](/docs/tokens).

### Additional considerations  {% id="considerations" %}

Keep the following considerations in mind when using context caching:

* The *minimum* input token count for context caching varies by model. The
  *maximum* is the same as the maximum for the given model. (For more on
  counting tokens, see the [Token guide](/docs/tokens)).

* The model doesn't make any distinction between cached tokens and regular
  input tokens. Cached content is a prefix to the prompt.

* There are no special rate or usage limits on context caching; the standard
  rate limits for `GenerateContent` apply, and token limits include cached
  tokens.

* The number of cached tokens is returned in the `usage_metadata` from the
  create, get, and list operations of the cache service, and also in
  `GenerateContent` when using the cache.

* Implicit caching doesn't apply when a request uses an explicit cache. Any
  additional input tokens sent alongside the explicit cache are billed at the
  standard input rate.


