google adk adding context that can be cached

 With Google ADK, we can easily add instruction via instructions settings but lets say we would like to add more context or pre-defined rules, perhaps safety rules - then we can use static_instructions 

And according to the docs:

 


static_instruction: Optional[types.ContentUnion] = None
  """Static instruction content sent literally as system instruction at the beginning.

  This field is for content that never changes and doesn't contain placeholders.
  It's sent directly to the model without any processing or variable substitution.

  This field is primarily for context caching optimization. Static instructions
  are sent as system instruction at the beginning of the request, allowing
  for improved performance when the static portion remains unchanged. Live API
  has its own cache mechanism, thus this field doesn't work with Live API.

  **Impact on instruction field:**
  - When static_instruction is None: instruction → system_instruction
  - When static_instruction is set: instruction → user content (after static content)

  **Context Caching:**
  - **Implicit Cache**: Automatic caching by model providers (no config needed)
  - **Explicit Cache**: Cache explicitly created by user for instructions, tools and contents

  See below for more information of Implicit Cache and Explicit Cache
  Gemini API: https://ai.google.dev/gemini-api/docs/caching?lang=python
  Vertex API: https://cloud.google.com/vertex-ai/generative-ai/docs/context-cache/context-cache-overview


The following code demonstrate how we can use it. 


from pathlib import Path
from google.adk.agents import LlmAgent

rules = Path("AGENTS.md").read_text(encoding="utf-8")

agent = LlmAgent(
    name="optimizer",
    model="gemini-2.5-flash",
    static_instruction=rules,                 # stable context
    instruction="Optimize the pipeline for: {goal}",  # per-run context
)






Comments

Popular posts from this blog

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

llama cpp running it in google colab