|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Example: multi-agent workflow with governance plugin. |
| 16 | +
|
| 17 | +This example shows how to register VeronicaGovernancePlugin on an ADK |
| 18 | +Runner to enforce per-agent budgets, block tools, and degrade to a |
| 19 | +cheaper model when budget runs low. |
| 20 | +
|
| 21 | +Usage: |
| 22 | + export GOOGLE_API_KEY="your-key" |
| 23 | + python main.py |
| 24 | +""" |
| 25 | + |
| 26 | +import asyncio |
| 27 | +import logging |
| 28 | + |
| 29 | +from google.adk import Runner |
| 30 | +from google.adk.agents import Agent |
| 31 | +from google.adk.sessions import InMemorySessionService |
| 32 | + |
| 33 | +from google.adk_community.governance import ( |
| 34 | + GovernanceConfig, |
| 35 | + VeronicaGovernancePlugin, |
| 36 | +) |
| 37 | + |
| 38 | +logging.basicConfig(level=logging.INFO, format="%(message)s") |
| 39 | + |
| 40 | + |
| 41 | +def main(): |
| 42 | + # Configure governance limits |
| 43 | + config = GovernanceConfig( |
| 44 | + max_cost_usd=0.50, # org-level: 50 cents |
| 45 | + agent_max_cost_usd=0.25, # per-agent: 25 cents |
| 46 | + failure_threshold=3, # circuit breaker after 3 failures |
| 47 | + recovery_timeout_s=30.0, |
| 48 | + degradation_threshold=0.7, # degrade at 70% budget |
| 49 | + fallback_model="gemini-2.0-flash-lite", |
| 50 | + blocked_tools=["shell_exec"], |
| 51 | + disable_tools_on_degrade=["web_search"], |
| 52 | + ) |
| 53 | + |
| 54 | + plugin = VeronicaGovernancePlugin(config=config) |
| 55 | + |
| 56 | + # Define agents |
| 57 | + researcher = Agent( |
| 58 | + model="gemini-2.5-flash", |
| 59 | + name="researcher", |
| 60 | + instruction=( |
| 61 | + "You are a research assistant. Answer questions using your" |
| 62 | + " knowledge. Be concise." |
| 63 | + ), |
| 64 | + ) |
| 65 | + |
| 66 | + summarizer = Agent( |
| 67 | + model="gemini-2.5-flash", |
| 68 | + name="summarizer", |
| 69 | + instruction=( |
| 70 | + "You summarize text provided to you. Keep summaries to 2-3" |
| 71 | + " sentences." |
| 72 | + ), |
| 73 | + ) |
| 74 | + |
| 75 | + # Orchestrator delegates to sub-agents |
| 76 | + orchestrator = Agent( |
| 77 | + model="gemini-2.5-flash", |
| 78 | + name="orchestrator", |
| 79 | + instruction=( |
| 80 | + "You coordinate research tasks. Use the researcher agent to" |
| 81 | + " find information, then the summarizer to condense it." |
| 82 | + ), |
| 83 | + sub_agents=[researcher, summarizer], |
| 84 | + ) |
| 85 | + |
| 86 | + # Create runner with governance plugin |
| 87 | + session_service = InMemorySessionService() |
| 88 | + runner = Runner( |
| 89 | + agent=orchestrator, |
| 90 | + app_name="governance_demo", |
| 91 | + session_service=session_service, |
| 92 | + plugins=[plugin], |
| 93 | + ) |
| 94 | + |
| 95 | + async def run(): |
| 96 | + session = await session_service.create_session( |
| 97 | + app_name="governance_demo", |
| 98 | + user_id="demo_user", |
| 99 | + ) |
| 100 | + |
| 101 | + from google.genai import types |
| 102 | + |
| 103 | + user_message = types.Content( |
| 104 | + role="user", |
| 105 | + parts=[types.Part(text="What is agent governance?")], |
| 106 | + ) |
| 107 | + |
| 108 | + async for event in runner.run_async( |
| 109 | + session_id=session.id, |
| 110 | + user_id="demo_user", |
| 111 | + new_message=user_message, |
| 112 | + ): |
| 113 | + if event.content and event.content.parts: |
| 114 | + for part in event.content.parts: |
| 115 | + if part.text: |
| 116 | + print(f"[{event.author}] {part.text[:200]}") |
| 117 | + |
| 118 | + # After run, the plugin logs a governance summary automatically. |
| 119 | + # You can also inspect programmatically: |
| 120 | + snap = plugin.budget.snapshot() |
| 121 | + print(f"\nTotal spent: ${snap.org_spent_usd:.4f}") |
| 122 | + for agent, spent in snap.agent_spent.items(): |
| 123 | + print(f" {agent}: ${spent:.4f}") |
| 124 | + |
| 125 | + asyncio.run(run()) |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main() |
0 commit comments