Skip to content

Commit a2d9f13

Browse files
seanzhougooglecopybara-github
authored andcommitted
chore: Add span for context caching handling and new cache creation
PiperOrigin-RevId: 820852233
1 parent 0df6759 commit a2d9f13

File tree

2 files changed

+65
-48
lines changed

2 files changed

+65
-48
lines changed

src/google/adk/models/gemini_context_cache_manager.py

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -353,57 +353,66 @@ async def _create_gemini_cache(
353353
Returns:
354354
Cache metadata with precise creation timestamp
355355
"""
356-
# Prepare cache contents (first N contents + system instruction + tools)
357-
cache_contents = llm_request.contents[:cache_contents_count]
358-
359-
cache_config = types.CreateCachedContentConfig(
360-
contents=cache_contents,
361-
ttl=llm_request.cache_config.ttl_string,
362-
display_name=(
363-
f"adk-cache-{int(time.time())}-{cache_contents_count}contents"
364-
),
365-
)
366-
367-
# Add system instruction if present
368-
if llm_request.config and llm_request.config.system_instruction:
369-
cache_config.system_instruction = llm_request.config.system_instruction
370-
logger.debug(
371-
"Added system instruction to cache config (length=%d)",
372-
len(llm_request.config.system_instruction),
356+
from ..telemetry.tracing import tracer
357+
358+
with tracer.start_as_current_span("create_cache") as span:
359+
# Prepare cache contents (first N contents + system instruction + tools)
360+
cache_contents = llm_request.contents[:cache_contents_count]
361+
362+
cache_config = types.CreateCachedContentConfig(
363+
contents=cache_contents,
364+
ttl=llm_request.cache_config.ttl_string,
365+
display_name=(
366+
f"adk-cache-{int(time.time())}-{cache_contents_count}contents"
367+
),
373368
)
374369

375-
# Add tools if present
376-
if llm_request.config and llm_request.config.tools:
377-
cache_config.tools = llm_request.config.tools
370+
# Add system instruction if present
371+
if llm_request.config and llm_request.config.system_instruction:
372+
cache_config.system_instruction = llm_request.config.system_instruction
373+
logger.debug(
374+
"Added system instruction to cache config (length=%d)",
375+
len(llm_request.config.system_instruction),
376+
)
378377

379-
# Add tool config if present
380-
if llm_request.config and llm_request.config.tool_config:
381-
cache_config.tool_config = llm_request.config.tool_config
378+
# Add tools if present
379+
if llm_request.config and llm_request.config.tools:
380+
cache_config.tools = llm_request.config.tools
382381

383-
logger.debug(
384-
"Creating cache with model %s and config: %s",
385-
llm_request.model,
386-
cache_config,
387-
)
388-
cached_content = await self.genai_client.aio.caches.create(
389-
model=llm_request.model,
390-
config=cache_config,
391-
)
392-
# Set precise creation timestamp right after cache creation
393-
created_at = time.time()
394-
logger.info("Cache created successfully: %s", cached_content.name)
382+
# Add tool config if present
383+
if llm_request.config and llm_request.config.tool_config:
384+
cache_config.tool_config = llm_request.config.tool_config
395385

396-
# Return complete cache metadata with precise timing
397-
return CacheMetadata(
398-
cache_name=cached_content.name,
399-
expire_time=created_at + llm_request.cache_config.ttl_seconds,
400-
fingerprint=self._generate_cache_fingerprint(
401-
llm_request, cache_contents_count
402-
),
403-
invocations_used=1,
404-
contents_count=cache_contents_count,
405-
created_at=created_at,
406-
)
386+
span.set_attribute("cache_contents_count", cache_contents_count)
387+
span.set_attribute("model", llm_request.model)
388+
span.set_attribute("ttl_seconds", llm_request.cache_config.ttl_seconds)
389+
390+
logger.debug(
391+
"Creating cache with model %s and config: %s",
392+
llm_request.model,
393+
cache_config,
394+
)
395+
cached_content = await self.genai_client.aio.caches.create(
396+
model=llm_request.model,
397+
config=cache_config,
398+
)
399+
# Set precise creation timestamp right after cache creation
400+
created_at = time.time()
401+
logger.info("Cache created successfully: %s", cached_content.name)
402+
403+
span.set_attribute("cache_name", cached_content.name)
404+
405+
# Return complete cache metadata with precise timing
406+
return CacheMetadata(
407+
cache_name=cached_content.name,
408+
expire_time=created_at + llm_request.cache_config.ttl_seconds,
409+
fingerprint=self._generate_cache_fingerprint(
410+
llm_request, cache_contents_count
411+
),
412+
invocations_used=1,
413+
contents_count=cache_contents_count,
414+
created_at=created_at,
415+
)
407416

408417
async def cleanup_cache(self, cache_name: str) -> None:
409418
"""Clean up cache by deleting it.

src/google/adk/models/google_llm.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,18 @@ async def generate_content_async(
115115
cache_metadata = None
116116
cache_manager = None
117117
if llm_request.cache_config:
118+
from ..telemetry.tracing import tracer
118119
from .gemini_context_cache_manager import GeminiContextCacheManager
119120

120-
cache_manager = GeminiContextCacheManager(self.api_client)
121-
cache_metadata = await cache_manager.handle_context_caching(llm_request)
121+
with tracer.start_as_current_span('handle_context_caching') as span:
122+
cache_manager = GeminiContextCacheManager(self.api_client)
123+
cache_metadata = await cache_manager.handle_context_caching(llm_request)
124+
if cache_metadata:
125+
if cache_metadata.cache_name:
126+
span.set_attribute('cache_action', 'active_cache')
127+
span.set_attribute('cache_name', cache_metadata.cache_name)
128+
else:
129+
span.set_attribute('cache_action', 'fingerprint_only')
122130

123131
logger.info(
124132
'Sending out request, model: %s, backend: %s, stream: %s',

0 commit comments

Comments
 (0)