Optimize Short-Term Project Sessions Using Context TTL Settings
Set a 'Time to Live' (TTL) to manage ephemeral project data.
Apply a TTL (Time to Live) value to your cached content to ensure data is automatically deleted after your task is finished, optimizing storage costs.
The Scenario
You are conducting a one-hour brainstorm session where the AI needs to remember 10 previous project documents, but you don't want to pay for permanent storage.
Before & after
Manually managing session state or clearing memory in custom scripts usually involves complex logic and database cleanup, taking 30–60 minutes to architect.
By setting a specific TTL (Time to Live), you can automatically flush the cache after a project session, costing you only pennies for minutes of work.
The Prompt
# Example configuration for a short-lived session
cached_content = genai.Caching.create(
model='models/gemini-1.5-flash',
display_name='short_session',
contents=[[PASTE_TRANSCRIPT_OR_CODE_HERE]],
ttl=datetime.timedelta(minutes=30),
)The TTL (Time to Live) setting allows you to control exactly how long a specific context stays active. If you only need the context for a specific 20-minute meeting or a 1-hour coding session, setting a short TTL prevents unnecessary storage charges.
Source
Release notes | Gemini API | Google AI for Developers"You can also choose how long you want the tokens to be cached before they are automatically deleted... referred to as time to leave (TTL)."
