Context caching allows you to save and reuse precomputed input tokens that you wish to use repeatedly, for example when asking different questions about the same media file. This can lead to cost and speed savings, depending on the usage. For a detailed introduction, see the Context caching guide.
Method: cachedContents.create
Creates CachedContent resource.
Endpoint
posthttps: / /generativelanguage.googleapis.com /v1beta /cachedContents
Request body
The request body contains an instance of CachedContent.
contents[]object (Content)
Optional. Input only. Immutable. The content to cache.
Optional. Input only. Immutable. A list of Tools the model may use to generate the next response
expirationUnion type
expiration can be only one of the following:Timestamp in UTC of when this resource is considered expired. This is always provided on output, regardless of what was sent on input.
Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z", "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30".
Input only. New TTL for this resource, input only.
A duration in seconds with up to nine fractional digits, ending with 's'. Example: "3.5s".
displayNamestring
Optional. Immutable. The user-generated meaningful display name of the cached content. Maximum 128 Unicode characters.
modelstring
Required. Immutable. The name of the Model to use for cached content Format: models/{model}
systemInstructionobject (Content)
Optional. Input only. Immutable. Developer set system instruction. Currently text only.
Optional. Input only. Immutable. Tool config. This config is shared for all tools.
Example request
Basic
Python
Node.js
Go
Shell
From name
Python
Node.js
Go
From chat
Python
Node.js
Go
Response body
If successful, the response body contains a newly created instance of CachedContent.
Method: cachedContents.list
Lists CachedContents.
Endpoint
gethttps: / /generativelanguage.googleapis.com /v1beta /cachedContents
Query parameters
pageSizeinteger
Optional. The maximum number of cached contents to return. The service may return fewer than this value. If unspecified, some default (under maximum) number of items will be returned. The maximum value is 1000; values above 1000 will be coerced to 1000.
pageTokenstring
Optional. A page token, received from a previous cachedContents.list call. Provide this to retrieve the subsequent page.
When paginating, all other parameters provided to cachedContents.list must match the call that provided the page token.
Request body
The request body must be empty.
Response body
Response with CachedContents list.
If successful, the response body contains data with the following structure:
List of cached contents.
nextPageTokenstring
A token, which can be sent as pageToken to retrieve the next page. If this field is omitted, there are no subsequent pages.
| JSON representation |
|---|
{
"cachedContents": [
{
object ( |
Method: cachedContents.get
Reads CachedContent resource.
Endpoint
gethttps: / /generativelanguage.googleapis.com /v1beta /{name=cachedContents /*}
Path parameters
namestring
Required. The resource name referring to the content cache entry. Format: cachedContents/{id} It takes the form cachedContents/{cachedcontent}.
Request body
The request body must be empty.
Example request
Python
Node.js
Go
Shell
Response body
If successful, the response body contains an instance of CachedContent.
Method: cachedContents.patch
Updates CachedContent resource (only expiration is updatable).
Endpoint
patchhttps: / /generativelanguage.googleapis.com /v1beta /{cachedContent.name=cachedContents /*}
PATCH https://generativelanguage.googleapis.com/v1beta/{cachedContent.name=cachedContents/*}
Path parameters
cachedContent.namestring
Output only. Identifier. The resource name referring to the cached content. Format: cachedContents/{id} It takes the form cachedContents/{cachedcontent}.
Query parameters
The list of fields to update.
This is a comma-separated list of fully qualified names of fields. Example: "user.displayName,photo".
Request body
The request body contains an instance of CachedContent.
expirationUnion type
expiration can be only one of the following:Timestamp in UTC of when this resource is considered expired. This is always provided on output, regardless of what was sent on input.
Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z", "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30".
Input only. New TTL for this resource, input only.
A duration in seconds with up to nine fractional digits, ending with 's'. Example: "3.5s".
Example request
Python
Node.js
Go
Shell
Response body
If successful, the response body contains an instance of CachedContent.
Method: cachedContents.delete
Deletes CachedContent resource.
Endpoint
deletehttps: / /generativelanguage.googleapis.com /v1beta /{name=cachedContents /*}
Path parameters
namestring
Required. The resource name referring to the content cache entry Format: cachedContents/{id} It takes the form cachedContents/{cachedcontent}.
Request body
The request body must be empty.
Example request
Python
Node.js
Go
Shell
Response body
If successful, the response body is an empty JSON object.
REST Resource: cachedContents
- Resource: CachedContent
- ToolConfig
- FunctionCallingConfig
- Mode
- RetrievalConfig
- LatLng
- UsageMetadata
- Methods
Resource: CachedContent
Content that has been preprocessed and can be used in subsequent request to GenerativeService.
Cached content can be only used with model it was created for.
contents[]object (Content)
Optional. Input only. Immutable. The content to cache.
Optional. Input only. Immutable. A list of Tools the model may use to generate the next response
Output only. Creation time of the cache entry.
Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z", "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30".
Output only. When the cache entry was last updated in UTC time.
Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z", "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30".
Output only. Metadata on the usage of the cached content.
expirationUnion type
expiration can be only one of the following:Timestamp in UTC of when this resource is considered expired. This is always provided on output, regardless of what was sent on input.
Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z", "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30".
Input only. New TTL for this resource, input only.
A duration in seconds with up to nine fractional digits, ending with 's'. Example: "3.5s".
namestring
Output only. Identifier. The resource name referring to the cached content. Format: cachedContents/{id}
displayNamestring
Optional. Immutable. The user-generated meaningful display name of the cached content. Maximum 128 Unicode characters.
modelstring
Required. Immutable. The name of the Model to use for cached content Format: models/{model}
systemInstructionobject (Content)
Optional. Input only. Immutable. Developer set system instruction. Currently text only.
Optional. Input only. Immutable. Tool config. This config is shared for all tools.
| JSON representation |
|---|
{ "contents": [ { object ( |
ToolConfig
The Tool configuration containing parameters for specifying Tool use in the request.
Optional. Function calling config.
Optional. Retrieval config.
includeServerSideToolInvocationsboolean
Optional. If true, the API response will include the server-side tool calls and responses within the Content message. This allows clients to observe the server's tool interactions.
| JSON representation |
|---|
{ "functionCallingConfig": { object ( |
FunctionCallingConfig
Configuration for specifying function calling behavior.
Optional. Specifies the mode in which function calling should execute. If unspecified, the default value will be set to AUTO.
allowedFunctionNames[]string
Optional. A set of function names that, when provided, limits the functions the model will call.
This should only be set when the Mode is ANY or VALIDATED. Function names should match [FunctionDeclaration.name]. When set, model will predict a function call from only allowed function names.
| JSON representation |
|---|
{
"mode": enum ( |
Mode
Defines the execution behavior for function calling by defining the execution mode.
| Enums | |
|---|---|
MODE_UNSPECIFIED |
Unspecified function calling mode. This value should not be used. |
AUTO |
Default model behavior, model decides to predict either a function call or a natural language response. |
ANY |
Model is constrained to always predicting a function call only. If "allowedFunctionNames" are set, the predicted function call will be limited to any one of "allowedFunctionNames", else the predicted function call will be any one of the provided "functionDeclarations". |
NONE |
Model will not predict any function call. Model behavior is same as when not passing any function declarations. |
VALIDATED |
Model decides to predict either a function call or a natural language response, but will validate function calls with constrained decoding. If "allowedFunctionNames" are set, the predicted function call will be limited to any one of "allowedFunctionNames", else the predicted function call will be any one of the provided "functionDeclarations". |
RetrievalConfig
LatLng
An object that represents a latitude/longitude pair. This is expressed as a pair of doubles to represent degrees latitude and degrees longitude. Unless specified otherwise, this object must conform to the WGS84 standard. Values must be within normalized ranges.
latitudenumber
The latitude in degrees. It must be in the range [-90.0, +90.0].
longitudenumber
The longitude in degrees. It must be in the range [-180.0, +180.0].
| JSON representation |
|---|
{ "latitude": number, "longitude": number } |
UsageMetadata
Metadata on the usage of the cached content.
totalTokenCountinteger
Total number of tokens that the cached content consumes.
| JSON representation |
|---|
{ "totalTokenCount": integer } |