feat(ge-demo-generator): optimize agent runtime latency and context caching - #3089
Merged
holtskinner merged 2 commits intoSep 3, 2026
Conversation
…aching - Lever A: Optimize preflight gate with trivial query bypass and low thinking level - Lever B: Stabilize system instruction for context cache fingerprint persistence and add cache metrics callback - Lever C: Asynchronously query and acknowledge autonomous task completions without blocking main event loop - Lever D: Lazy load Cloud Logging and Agent Engine sandbox code executor to reduce cold start latency - Fix: Fallback to SELF_URL when APP_URL is empty in agent card metadata
holtskinner
approved these changes
Sep 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR introduces critical latency optimizations and context caching stabilization to the Gemini Enterprise Demo Generator agent runtime (
agent_template/adk_agent/app/), reducing Time to First Token (TTFT) and cold-turn overhead while strictly preserving the scale-to-zero default (min-instances 0) and 100% compatibility with A2UI v0.9 cards.Key Changes
1. Lever A: Preflight Gate Bypass & Thinking Level Optimization (
fast_api_app.py)_is_trivial_preflight_bypassheuristic to fast-track greetings ("Hi","Hello"), short conversational turns, and action button callbacks directly to the agent without triggering the Preflight LLM classification gate (cutting greeting turn overhead to 0ms).ThinkingConfig(thinking_level="LOW")inGenerateContentConfigfor_classify_for_preflightand_localize_ui_stringsto avoid unbounded thinking latency on structured JSON classifications.2. Lever B: Context Cache Fingerprint Stabilization (
agent.py,fast_api_app.py)_root_instructioninto immutable memory (_ROOT_INSTRUCTION) instead of dynamically regenerating it every turn._bg_task_results) are now appended as standard conversation history[SYSTEM NOTE]parts rather than mutating the root system prompt._log_cache_and_model_telemetryto recordcached_content_token_count,prompt_token_count, and cache efficiency metrics.3. Lever C: Non-Blocking / Conditional Firestore I/O (
fast_api_app.py)asyncio.to_thread(_query_and_ack_tasks_sync)), eliminating synchronous event-loop stalls._has_autonomous_tasksis false.4. Lever D: Lazy Module & Sandbox Initialization (
fast_api_app.py,agent.py)_LazyCloudLoggerwith_PyLoggerAdapterto delay Google Cloud Logging client initialization until first log emission, speeding up container import._LazyAgentEngineCodeExecutorsubclassingBaseCodeExecutorto lazily resolve the Vertex AI Agent Engine Sandbox resource upon the first dynamic Python execution.5. Fix: AgentCard URL Fallback (
fast_api_app.py)SELF_URLwhenAPP_URLis omitted, ensuring robust A2A agent card endpoint resolution.Validation & Results
MaterialCard,MaterialTable,VegaChart,SuggestionChips, Pattern I buttons) render flawlessly.python3 -m py_compile.