Configuration for context caching across all agents in an app. This configuration enables and controls context caching behavior for all LLM agents in an app. When this config is present on an app, context caching is enabled for all agents. When absent (None), context caching is disabled.
| 25 | |
| 26 | @experimental(FeatureName.AGENT_CONFIG) |
| 27 | class ContextCacheConfig(BaseModel): |
| 28 | """Configuration for context caching across all agents in an app. |
| 29 | |
| 30 | This configuration enables and controls context caching behavior for |
| 31 | all LLM agents in an app. When this config is present on an app, context |
| 32 | caching is enabled for all agents. When absent (None), context caching |
| 33 | is disabled. |
| 34 | |
| 35 | Context caching can significantly reduce costs and improve response times |
| 36 | by reusing previously processed context across multiple requests. |
| 37 | |
| 38 | Caching begins on the second turn of a session at the earliest and requires |
| 39 | the prior request to reach Gemini's hard 4096-token minimum, so short or |
| 40 | single-turn sessions are never cached. |
| 41 | |
| 42 | Attributes: |
| 43 | cache_intervals: Maximum number of invocations to reuse the same cache before refreshing it |
| 44 | ttl_seconds: Time-to-live for cache in seconds |
| 45 | min_tokens: Minimum prior-request tokens required to enable caching |
| 46 | """ |
| 47 | |
| 48 | model_config = ConfigDict( |
| 49 | extra="forbid", |
| 50 | ) |
| 51 | |
| 52 | cache_intervals: int = Field( |
| 53 | default=10, |
| 54 | ge=1, |
| 55 | le=100, |
| 56 | description=( |
| 57 | "Maximum number of invocations to reuse the same cache before" |
| 58 | " refreshing it" |
| 59 | ), |
| 60 | ) |
| 61 | |
| 62 | ttl_seconds: int = Field( |
| 63 | default=1800, # 30 minutes |
| 64 | gt=0, |
| 65 | description="Time-to-live for cache in seconds", |
| 66 | ) |
| 67 | |
| 68 | min_tokens: int = Field( |
| 69 | default=0, |
| 70 | ge=0, |
| 71 | description=( |
| 72 | "Minimum prior-request tokens required to enable caching. This gates" |
| 73 | " on the previous request's actual prompt token count, not an" |
| 74 | " estimate of the current request. Gemini enforces a hard 4096-token" |
| 75 | " minimum that always applies, so values below 4096 have no" |
| 76 | " additional effect. No cache is created on the first request of a" |
| 77 | " session; caching begins on the second turn once a previous token" |
| 78 | " count is known. Set higher to avoid caching small requests where" |
| 79 | " storage overhead may exceed benefits." |
| 80 | ), |
| 81 | ) |
| 82 | |
| 83 | create_http_options: types.HttpOptions | None = Field( |
| 84 | default=None, |
no outgoing calls