Update a context cache

This guide shows how to update the expiration time of a context cache. By default, a context cache expires 60 minutes after it's created. Expired caches are deleted and can no longer be used or updated. To extend the life of an unexpired cache, you can update its expiration by setting either a relative duration (ttl) or a specific timestamp (expire_time).

The following table compares the two parameters for updating a context cache.

Parameter Description Use Case
ttl (Time To Live) Sets a relative expiration time. The cache expires after the specified duration has passed from the time of the update. Use when you want the cache to be available for a specific duration, such as "keep this active for the next 2 hours."
expire_time Sets an absolute expiration time. The cache expires at the specified date and time, regardless of when the update is made. Use when you need the cache to expire at a precise moment, such as "this cache must be deleted by midnight on June 30."

Update the context cache using its ttl parameter

The following examples show how to update the cache's expiration time by 3,600 seconds.

Python

Install

pip install --upgrade google-genai

To learn more, see the SDK reference documentation.

Set environment variables to use the Gen AI SDK with Vertex AI:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=us-central1
export GOOGLE_GENAI_USE_VERTEXAI=True

from datetime import datetime as dt
from datetime import timezone as tz
from datetime import timedelta

from google import genai
from google.genai.types import HttpOptions, UpdateCachedContentConfig

client = genai.Client(http_options=HttpOptions(api_version="v1"))

# Get content cache by name
# cache_name = "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111"
content_cache = client.caches.get(name=cache_name)
print("Expire time", content_cache.expire_time)
# Example response
#   Expire time 2025-02-20 15:50:18.434482+00:00

# Update expire time using TTL
content_cache = client.caches.update(
    name=cache_name, config=UpdateCachedContentConfig(ttl="36000s")
)
time_diff = content_cache.expire_time - dt.now(tz.utc)
print("Expire time(after update):", content_cache.expire_time)
print("Expire time(in seconds):", time_diff.seconds)
# Example response
#   Expire time(after update): 2025-02-14 01:51:42.571696+00:00
#   Expire time(in seconds): 35999

# Update expire time using specific time stamp
next_week_utc = dt.now(tz.utc) + timedelta(days=7)
content_cache = client.caches.update(
    name=cache_name, config=UpdateCachedContentConfig(expireTime=next_week_utc)
)
print("Expire time(after update):", content_cache.expire_time)
# Example response
#   Expire time(after update): 2025-02-20 15:51:42.614968+00:00

Go

Learn how to install or update the Go.

To learn more, see the SDK reference documentation.

Set environment variables to use the Gen AI SDK with Vertex AI:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=us-central1
export GOOGLE_GENAI_USE_VERTEXAI=True

import (
	"context"
	"fmt"
	"io"
	"time"

	genai "google.golang.org/genai"
)

// updateContentCache shows how to update content cache expiration time.
func updateContentCache(w io.Writer, cacheName string) error {
	ctx := context.Background()

	client, err := genai.NewClient(ctx, &genai.ClientConfig{
		HTTPOptions: genai.HTTPOptions{APIVersion: "v1"},
	})
	if err != nil {
		return fmt.Errorf("failed to create genai client: %w", err)
	}

	// Update expire time using TTL
	resp, err := client.Caches.Update(ctx, cacheName, &genai.UpdateCachedContentConfig{
		TTL: time.Duration(time.Duration.Seconds(36000)),
	})
	if err != nil {
		return fmt.Errorf("failed to update content cache exp. time with TTL: %w", err)
	}

	fmt.Fprintf(w, "Cache expires in: %s\n", time.Until(resp.ExpireTime))
	// Example response:
	// Cache expires in: 10h0m0.005875s

	// Update expire time using specific time stamp
	inSevenDays := time.Now().Add(7 * 24 * time.Hour)
	resp, err = client.Caches.Update(ctx, cacheName, &genai.UpdateCachedContentConfig{
		ExpireTime: inSevenDays,
	})
	if err != nil {
		return fmt.Errorf("failed to update content cache expire time: %w", err)
	}

	fmt.Fprintf(w, "Cache expires in: %s\n", time.Until(resp.ExpireTime))
	// Example response:
	// Cache expires in: 167h59m59.80327s

	return nil
}

REST

To update the context cache with the REST API, send a PATCH request to the Vertex AI API. The following example updates the expiration date using the ttl parameter.

Before using any of the request data, make the following replacements:

  • PROJECT_ID: Your project ID.
  • LOCATION: The region where the request to create the context cache was processed.
  • CACHE_ID: The ID of the context cache. The context cache ID is returned when you create the context cache. You can also find context cache IDs by listing the context caches for a Google Cloud project using. For more information, see create a context cache and list context caches.
  • SECONDS: A float that specifies the seconds component of the duration before the cache expires.
  • NANOSECONDS: A float that specifies the nanoseconds component of the duration before the cache expires.

HTTP method and URL:

PATCH https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID

Request JSON body:

{
  "seconds":"SECONDS",
  "nanos":"NANOSECONDS"
}

To send your request, choose one of these options:

curl

Save the request body in a file named request.json, and execute the following command:

curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID"

PowerShell

Save the request body in a file named request.json, and execute the following command:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID" | Select-Object -Expand Content

You should receive a JSON response similar to the following:

Example curl command

PROJECT_ID="PROJECT_ID"
LOCATION="us-central1"
CACHE_ID="CACHE_ID"

curl \
-X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json"\
"https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/cachedContents/${CACHE_ID}" -d \
'{
   "ttl": {"seconds":"3600","nanos":"0"}
}'

Update the context cache using its expire_time parameter

The following examples show how to use the expire_time parameter to update the cache's expiration time to 9 AM on June 30, 2024.

REST

To update the context cache with the REST API, send a PATCH request to the Vertex AI API. The following example updates the expiration date using the expire_time parameter.

Before using any of the request data, make the following replacements:

  • PROJECT_ID: .
  • LOCATION: The region where the request to create the context cache was processed.
  • CACHE_ID: The ID of the context cache. You can find the ID in the response when you create the context cache.
  • EXPIRE_TIME: A Timestamp that specifies the time when the context cache expires.

HTTP method and URL:

PATCH https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID

Request JSON body:

{
   "expire_time":"EXPIRE_TIME"
}

To send your request, choose one of these options:

curl

Save the request body in a file named request.json, and execute the following command:

curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID"

PowerShell

Save the request body in a file named request.json, and execute the following command:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID" | Select-Object -Expand Content

You should receive a JSON response similar to the following:

Example curl command

PROJECT_ID="PROJECT_ID"
LOCATION="us-central1"
CACHE_ID="CACHE_ID"

curl \
-X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json"\
"https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/cachedContents/${CACHE_ID}" -d \
'{
   "expire_time":"2024-06-30T09:00:00.000000Z"
}'

What's next