# Caches

# Caches


```
interface Caches
```

<br />

*** ** * ** ***

Provides an interface to explicitly manage caches for `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel`.

## Summary

| ### Public functions |
|---|---|
| `suspend https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CachedContext` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Caches#create(com.google.mlkit.genai.prompt.CreateCachedContextRequest)(request: https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CreateCachedContextRequest)` Creates a cached context that can be used in future `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest`s. |
| `suspend https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Caches#delete(kotlin.String)(name: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)` Deletes the cache with the given name. |
| `suspend https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CachedContext?` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Caches#get(kotlin.String)(name: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)` Returns the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CachedContext` with the given name. |
| `suspend https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CachedContext>` | `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Caches#list()()` Queries the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CachedContext`s created by `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Caches#create(com.google.mlkit.genai.prompt.CreateCachedContextRequest)`. |

## Public functions

### create

```
suspend fun create(request: CreateCachedContextRequest): CachedContext
```

Creates a cached context that can be used in future `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest`s.

This experimental method can be used to create a cached context that stores the pre-processed state of a reusable part of a prompt to improve inference performance. This is useful for scenarios where a common prefix is used across multiple generation requests, such as with few-shot examples.

The created `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CachedContext` can be used in a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerateContentRequest`. Each cache is identified by a unique name across all `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/GenerativeModel` instances within the same app. If `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Caches#create(com.google.mlkit.genai.prompt.CreateCachedContextRequest)` is called with a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CreateCachedContextRequest` for an existing cache name, the old cache will be overwritten.

Caches may become invalid over time (e.g., due to model update). If an invalid cache is used, a GenAiException with ErrorCode.CACHE_PROCESSING_ERROR will be thrown, indicating that the cache should be deleted or recreated.

This is a non-blocking, cancellable coroutine. If the operation is no longer needed, cancelling the coroutine will stop the execution.

Note that cache creation requests may fail under certain conditions such as:

- The prefix prompt length exceeds model limitations.

- The `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CreateCachedContextRequest` contains invalid or malformed input.

- Usage quotas are exceeded or safety checks fail.

| Throws |
|---|---|
| `GenAiException` | if the cache creation fails. |

### delete

```
suspend fun delete(name: String): Boolean
```

Deletes the cache with the given name.

This experimental method removes the cached context created by `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Caches#create(com.google.mlkit.genai.prompt.CreateCachedContextRequest)` from storage, freeing up space.

| Parameters |
|---|---|
| `name: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html` | the name of the cache to delete. |

### get

```
suspend fun get(name: String): CachedContext?
```

Returns the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CachedContext` with the given name.

This experimental method retrieves a `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CachedContext` created by `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Caches#create(com.google.mlkit.genai.prompt.CreateCachedContextRequest)`, returning `null` if no cache with that name exists.

### list

```
suspend fun list(): List<CachedContext>
```

Queries the `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CachedContext`s created by `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/Caches#create(com.google.mlkit.genai.prompt.CreateCachedContextRequest)`.

This experimental method returns a list of `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CachedContext` objects. This method can be used to inspect and manage existing caches.

| Returns |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html<https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CachedContext>` | a `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html` of `https://developers.google.com/android/reference/kotlin/com/google/mlkit/genai/prompt/CachedContext`s. |