Skip to content

[agentic-token-optimizer] [AIC Optimizer] Detection Analysis Report — 5 optimizations (~45 AIC/run savings) #45770

Description

@github-actions

Target Workflow

Detection Analysis Report (detection-analysis-report.md) — selected as highest-AIC unoptimized workflow (164.75 AIC, 1 run, 24 action-minutes) with no prior optimization entry in the last 14 days.

Analysis Period

  • Window: 2026-07-08 to 2026-07-15 (7 days)
  • Runs analyzed: 1 (only 1 run in window — scheduled daily)
  • Engine: Claude Code (claude-opus-4-8)
  • Conclusion: success

Cost Profile

Metric Value
Total AIC 164.75
Avg AIC / run 164.75
Raw tokens (total) 28,075
Input tokens 10,291
Output tokens 17,784
Cache read tokens 1,497,388
Cache write tokens 72,673
Action minutes / run 24
Avg turns / run 0 (not tracked)
GitHub API calls 12

Note: Cache read is very high (1.5M), indicating large shared context being re-read every run. Output tokens (17,784) dominate input (10,291), suggesting heavy generative work — likely chart generation code and report writing that could be offloaded.

References: §29417833633


Ranked Recommendations

1. Remove the shared/otlp.md import — it injects empty content

Estimated savings: ~5–8 AIC/run

The compiled prompt includes {{#runtime-import .github/workflows/shared/otlp.md}} which resolves to an empty string (two blank lines). This import contributes zero instructions but still triggers a file-read at compile time. Remove or mark it optional: true to eliminate the dead import.

Action: Delete or comment out the otlp.md import from the imports: block in detection-analysis-report.md.

Evidence: prompt-import-tree.json shows rawContent: "\n\n" for the otlp.md import.


2. Replace inline Python chart generation with a reusable shared script

Estimated savings: ~20–30 AIC/run

Step 5 instructs the agent to write a full Python script to /tmp/gh-aw/python/detection_comparison.py at runtime, then execute it. This pattern causes the agent to generate ~100+ lines of Matplotlib/Seaborn code from scratch each run, consuming significant output tokens (contributing to the 17,784 output token total).

Action: Extract the chart-generation script into a committed file (e.g., .github/scripts/detection_comparison.py) and replace Step 5 with a single bash invocation:

python3 .github/scripts/detection_comparison.py \
  --metrics /tmp/gh-aw/python/data/metrics.json \
  --output /tmp/gh-aw/python/charts/detection_comparison.png

This turns runtime code generation into a simple script call, saving ~100 output tokens per chart step.

Evidence: Step 5 asks the agent to "write a Python script" with fully specified axes, annotations, DPI, and style — all of which are static parameters that don't need LLM generation each run.


3. Consolidate Step 1 MCP logs call — avoid re-describing the MCP API in the prompt

Estimated savings: ~10 AIC/run

Step 1 describes the agentic-workflows MCP logs tool interface inline using a markdown code block, duplicating information already in the system context. The prompt then also explains the aw_info.json schema that the MCP tool already makes accessible. This adds ~300 tokens of redundant instruction.

Action: Simplify Step 1 to:

Call `agenticworkflows logs --start-date -1d` and read the run directories from `/tmp/gh-aw/aw-mcp/logs`. Each run's `aw_info.json` contains `features["gh-aw-detection"]` (boolean), `status`, and `tokens`.

Evidence: The compiled prompt includes a full re-description of the MCP tool parameters and output format in a fenced block, adding tokens that the agent already receives via the tool-list injection.


4. Collapse Steps 2–4 into a single classification pass

Estimated savings: ~5–8 AIC/run

Steps 2, 3, and 4 (Classify Runs, Identify Misconfigured Workflows, Compute Metrics) each have separate headers and repeated field lists (workflow_name, status, total_tokens, engine_id). The agent must hold context across three separate section boundaries to complete what is effectively one pass over the data.

Action: Merge Steps 2–4 under a single ### Analyze Runs section with one compact instruction block and one output table schema. This reduces inter-section context duplication and compresses the prompt by ~80 tokens.

Evidence: Step 3's "For each misconfigured workflow, record" list re-enumerates fields already listed in Step 2. Step 4's metrics table duplicates fields from Step 2's classification output.


5. Add model-level caching to trending-charts-simple.md shared import

Estimated savings: ~5 AIC/run

The shared/trending-charts-simple.md import contributes ~70 lines of Python boilerplate (import statements, chart templates, JSON Lines examples) that are identical every run. This large static block is being written into cache each run (cache_write_tokens: 72,673). If this shared component is declared with a cache: true hint or moved to a system-level import, cache write cost drops significantly.

Action: Evaluate whether trending-charts-simple.md can be promoted to a system-level shared import or annotated with a cache directive to avoid re-writing 70+ lines of unchanged boilerplate on every run.

Evidence: cache_write_tokens (72,673) greatly exceeds input_tokens (10,291), indicating large static content is being re-cached unnecessarily.


Inline Sub-Agent Candidates

No existing ## agent: blocks are present. Two sections score ≥ 6 on the sub-agent scoring matrix.

Candidate A — Chart Generation Step (Step 5)

Dimension Score Rationale
Independence 3 Reads only metrics.json; no cross-section dependency
Small-model adequacy 3 Purely mechanical: write boilerplate Python + run it
Parallelism 2 Can run concurrently with Step 6 (trending)
Size 2 Substantial output (~100 lines Python)
Total 10 Strong candidate

Proposed invocation (replace Step 5 body):

## agent: generate-detection-chart
model: small
Write and execute a Python script at /tmp/gh-aw/python/detection_comparison.py.
Input: /tmp/gh-aw/python/data/metrics.json (keys: regular_runs, detection_runs, regular_success_rate, detection_success_rate).
Output: /tmp/gh-aw/python/charts/detection_comparison.png at 150 DPI using seaborn whitegrid.
Chart: grouped bars (left y-axis: run counts), line overlay (right y-axis: success rate %).

Candidate B — Historical Trending Append (Step 6, trending JSONL write)

Dimension Score Rationale
Independence 3 Reads only the metrics computed in Step 4
Small-model adequacy 3 Append-only JSONL write + conditional trend chart
Parallelism 2 Can run in parallel with Step 5
Size 1 Small but repeatable
Total 9 Strong candidate

Proposed invocation (replace Step 6 body):

## agent: update-detection-trending
model: small
Append one JSON line to /tmp/gh-aw/cache-memory/trending/detection-metrics/history.jsonl with timestamp and metrics from /tmp/gh-aw/python/data/metrics.json.
If the file has ≥7 entries, generate a 30-day trend line chart at /tmp/gh-aw/python/charts/detection_trend.png.

Caveats

  • Only 1 run was available for analysis (daily schedule, 7-day window). Recommendations are based on static prompt analysis rather than multi-run tool-usage patterns.
  • Cache token counts include system-level context (security policy, safe-output instructions) which cannot be removed — savings estimates are conservative.
  • Inline sub-agent recommendations require the workflow to run with an engine that supports nested agents; verify engine compatibility before implementing Candidates A/B.

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • awmgmcpg

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "awmgmcpg"

See Network Configuration for more information.

Generated by Agentic Workflow AIC Usage Optimizer · 54.4 AIC · ⊞ 6.9K ·

  • expires on Jul 22, 2026, 7:28 AM UTC-08:00

Metadata

Metadata

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions