Workflow 2: Progressive Tool and Runbook Pruning#
governed_agents/shared_decision_guards/progressive_tool_pruner.py implements an ADK before_model_callback (progressive_tool_pruner_callback and make_progressive_tool_pruner_callback) that evaluates 36 structured questions in a single Gemini Decision 1 pass (0 output tokens) on Step 1 and pins the pruned tool set and runbook across subsequent tool-continuation steps.
Across 10 live 3-step SOC trajectories over a 35-tool catalog and 6 operational runbooks, the pruner achieves 90.00% runbook accuracy (9/10), 100.00% tool recall (0 missing required tools), 68.56% mean tool precision, and an 83.87% mean prefill reduction (16,027.2 bytes saved per step, ~4,006.5 estimated tokens saved per step). With Turn-1 KV-cache prefix pinning (prune_with_session_pinning_async), Steps 2 and 3 execute in 0.0 ms with 0 Decision 1 RPCs and 10/10 byte-identical prefix hashes (compute_prefix_hash), yielding 9,301.5 net tokens saved per 3-step trajectory (93,015 tokens total across 10 trajectories).
Figure 1. Baseline vs. pruned LlmRequest prefill bytes and 3-step trajectory savings with Turn-1 KV-cache prefix pinning (10/10 intact prefix hashes, 10 total RPCs across 30 steps).#
Single-pass 36-question scoring and live catalog extraction#
build_pruning_questions constructs one Decision 1 request containing:
1
choicequestion (select_runbook): Matches the prompt againstRUNBOOK_CRITERIAfor the 6 playbooks inSOC_RUNBOOKS(credential_dump_hunt,phishing_triage,cti_enrichment,crown_jewel_containment,detection_rule_tuningfor Chronicle SIEM YARA-L 2.0 UDM rules rather than standard file/payload YARA, andgeneral_triage).35+
noulquestions (tool_needed_<tool_name>): Evaluates each tool inSOC_TOOL_CATALOGor any dynamically registeredFunctionDeclaration.descriptionextracted at runtime by_extract_request_tool_descriptions.
Because gemini-decision-1 scores all 36 questions in one forward pass over the shared input prompt, Step-1 evaluation finishes with 0 output tokens (362.38 ms mean, 319.83 ms p50, 458.46 ms p95 in the Wave 1 verification run).
Turn-1 KV-cache prefix pinning and canary preservation#
Re-pruning tools on every step of a multi-step tool loop invalidates the downstream LLM’s KV cache whenever the system prompt or tool declaration list changes. prune_with_session_pinning_async prevents cache thrashing:
Step 1: Runs Decision 1, stores
tool_pruner_pinned_plan(kept_tools,selected_runbook,runbook_confidence) andtool_pruner_pinned_promptin session state, and mutatesLlmRequest.Steps 2 and 3 (
_is_tool_continuation_step): Reapplies_apply_pinned_planin 0.0 ms with 0 Decision 1 RPCs, preserving a byte-identicalcompute_prefix_hashacross all 10 trajectories (10/10 intact).Minimum catalog guard (
DEFAULT_MIN_TOOLS_TO_PRUNE = 12): Skips Decision 1 overhead when an agent already has fewer than 12 declared tools.Canary preservation (
preserve_canary_tools): Retains designatedADVERTISED_DENIED_TOOLSalongsidesafety_floor_toolsso Agent Gateway allowlist tests and security canaries are never pruned away.
Figure 2. Live run of scripts/governance/workflows/run_workflow2_tool_pruning.py showing 10/10 KV-cache prefix pinning, 100.00% tool recall, 83.87% prefill reduction, and 9,301.5 net tokens saved per 3-step trajectory.#
Live 10-trajectory results#
Scenario ID |
Selected runbook (match) |
Kept tools |
Recall / precision |
3-step net tokens saved |
|---|---|---|---|---|
|
|
6 / 35 |
1.00 / 0.83 |
8,708 |
|
|
2 / 35 |
1.00 / 1.00 |
10,402 |
|
|
9 / 35 |
1.00 / 0.56 |
8,244 |
|
|
5 / 35 |
1.00 / 0.80 |
9,228 |
|
|
8 / 35 |
1.00 / 0.50 |
8,279 |
|
|
1 / 35 |
1.00 / 1.00 |
10,651 |
|
|
7 / 35 |
1.00 / 0.57 |
8,753 |
|
|
4 / 35 |
1.00 / 0.50 |
9,775 |
|
|
7 / 35 |
1.00 / 0.43 |
8,914 |
|
|
3 / 37 |
1.00 / 0.67 |
10,061 |
Reproduction#
.venv/bin/python -m pytest -o addopts="" tests/test_workflow2_tool_pruning.py
.venv/bin/python scripts/governance/workflows/run_workflow2_tool_pruning.py