Skip to content

[agentic-token-optimizer] Agentic Workflow AI Credit Spend Optimizer β€” prompt condensation & source-analysis sub-agentΒ #109

@github-actions

Description

@github-actions

🎯 Target Workflow

Agentic Workflow AI Credit Spend Optimizer (.github/workflows/agentic-token-optimizer.lock.yml)
Selected as the only workflow in the repository with no prior optimization history and the longest average agent runtime (~8.4 min/run). All other candidates were optimized between 2026-05-28 and 2026-06-05 (within the 14-day exclusion window). Pre-aggregated token data (top-workflows.json, all-runs.json) returned empty results for the current 7-day window; workflow selection and evidence derive directly from the GitHub Actions API.


πŸ“… Analysis Period & Runs Audited

  • Period: 2026-05-20 β†’ 2026-06-08 (19 days)
  • Runs analyzed: 14 (all weekday runs since workflow creation)
  • Conclusions: 14 Γ— success (100% reliability β€” no reliability waste)
  • **Actual AI-credit (redacted) unavailable (log pipeline returned empty); metrics derived from job-wall-clock timing as proxy

πŸ’° Spend Profile

Metric Value
Runs analyzed 14
Avg run duration 8.4 min
Min / Max duration 6.2 min / 12.7 min
Duration std-dev Β±1.9 min (2Γ— spread between best and worst)
Estimated Actions minutes (14 runs) ~117 min
Cadence 5 runs/week (weekdays)
Avg AI credits / run N/A (log pipeline empty β€” see caveats)
Long-run outliers (>avg+1Οƒ) 2 runs: 12.7 min, 12.3 min

The 2Γ— spread between short (6.2 min) and long (12.7 min) runs indicates the optimizer performs significantly more work when pre-aggregated data is absent (common scenario) and falls back to sequential GitHub API exploration.

Per-run timing table (14 runs)
Run ID Date Duration
Β§27023961142 2026-06-05 12m 43s
Β§26962151666 2026-06-04 8m 11s
Β§26896611891 2026-06-03 8m 1s
Β§26831089942 2026-06-02 6m 15s
Β§26767461335 2026-06-01 6m 49s
Β§26646567699 2026-05-29 12m 19s
Β§26585005176 2026-05-28 8m 7s
Β§26521529625 2026-05-27 7m 49s
Β§26458462510 2026-05-26 7m 8s
Β§26407979066 2026-05-25 9m 29s
Β§26296590578 2026-05-22 6m 57s
Β§26236209003 2026-05-21 7m 53s
Β§26173004813 2026-05-20 7m 4s
Β§26162921957 2026-05-20 8m 40s

πŸ”§ Tool Configuration (from source)

Tool Config Assessment
bash all patterns Keep β€” primary tool for file reads and gh api calls
github mode: gh-proxy, toolsets: [issues] Keep β€” used to list existing issues for context
repo-memory branch memory/token-audit, max 102KB/file Keep β€” reads daily snapshots and writes optimization log

No tools are candidates for removal: all three are used in successful runs.


πŸ“‹ Ranked Recommendations

1. Condense the "Data Access Guidelines" section (~8–12% savings)

Evidence: The ## Data Access Guidelines section spans ~40 lines and contains three separate code blocks demonstrating the same gh api --jq pattern (extract-only, list runs, pipe-with-filter, plus a negative example). The AI agent reads this meta-instruction on every turn. A tighter 12-line version conveying the same rules would reduce per-turn prompt token consumption.

Action: Replace the multi-block example section with a single consolidated block:

# Always filter with --jq or | jq; never load unfiltered responses
gh api "repos/$REPO/contents/.github/workflows/my.md" --jq '.content' | base64 -d
gh api "repos/$REPO/actions/workflows/ID/runs?per_page=10" \
  --jq '.workflow_runs[] | {id, name, conclusion, run_started_at}'
# Chain multi-step reads in one bash block; prefer --jq for simple filters

Remove the # ❌ block (negative example) as it adds tokens without changing behavior.

Estimated savings: ~25 lines removed from 319-line prompt β‰ˆ 8% reduction β†’ ~5–8% credit savings per run.


2. Add inline sub-agent for Phase 3+4 β€” workflow source analysis (~15–20% savings)

Evidence: Phases 3 (Read Workflow Source) and 4 (Structural Optimization Checks) are fully independent from Phase 2 (runtime behavior analysis) after Phase 1 selects the target workflow name. Both phases perform extractive work β€” reading a markdown file, listing sections, checking for ## agent: blocks, and scoring sub-agent opportunities β€” tasks well-suited to a smaller model.

Sub-agent score:

Dimension Score / Max Rationale
Independence 2 / 3 Needs only workflow name from Phase 1; otherwise standalone
Small-model adequacy 3 / 3 Extractive: read markdown, count lines per section, check patterns
Parallelism 2 / 2 Can run concurrently with Phase 2 behavioral analysis
Size 2 / 2 Reading a 200–320 line file + structured scoring
Total 9 / 10 Strong candidate

Action: Add the following block between Phase 1 and Phase 2 in agentic-token-optimizer.md:

## agent: source-analyzer

You receive `workflow_path` (e.g. `.github/workflows/foo.md`) and `repo` variables.

1. Read the file: `gh api "repos/$repo/contents/$workflow_path" --jq '.content' | base64 -d`
2. Return a JSON object with:
   - `sections`: list of all `##`-level headings in order
   - `has_subagents`: true if any `## agent:` block exists
   - `configured_tools`: list of tools in YAML frontmatter
   - `lines_per_section`: map of heading β†’ line count
   - `setup_prefix_repeated`: any shell snippet appearing verbatim in 2+ section openings
3. Output only the JSON object β€” no prose.

In the main prompt, replace Phases 3+4 with: "Use the source-analyzer sub-agent output for structural analysis; do not re-read the workflow source."

Estimated savings: 2–3 main-agent turns delegated to a smaller model β†’ ~15–20% credit reduction for source-analysis phases.


3. Add a no-data fast-path at Phase 1 (~5–8% savings on empty-data runs)

Evidence: All 14 analyzed runs show the log pipeline returning empty data. Issue bodies confirm the agent probes top-workflows.json, all-runs.json, and daily snapshot files sequentially before pivoting to GitHub API β€” adding 3–5 turns of empty reads per run.

Action: Add a single conditional gate at the top of Phase 1:

### Data Availability Check
Run: `jq '.runs | length' /tmp/gh-aw/token-audit/all-runs.json`
- If > 0: proceed with pre-aggregated data as described below.
- If == 0: skip reading `top-workflows.json` and daily snapshots entirely;
  go directly to GitHub API target selection.

This replaces 3+ sequential empty file reads with one check.

Estimated savings: ~3 turns eliminated on empty-data days β†’ ~5–8% per run; proportionally larger on 12+ min outlier runs.


4. Collapse Phase 4 scoring guidance into the table (~5% savings)

Evidence: Phase 4 contains a 4-dimension scoring table plus two prose subsections ("Scoring guidance" with threshold rules + "Smaller models are a good fit for" bulleted list) totaling ~35 lines. These are consulted every run even when no sub-agent analysis is triggered.

Action: Embed threshold column into the scoring table; remove standalone prose blocks:

Dimension Meaning Max Good fit when β‰₯
Independence Runs without other sections' output 3 2
Small-model adequacy Extractive / classificatory / formatting 3 2
Parallelism Can run concurrently 2 1
Size Substantial enough to justify agent call 2 1

Total β‰₯ 6 β†’ recommend; 4–5 β†’ moderate; < 4 β†’ keep in main agent.

Estimated savings: ~25 lines removed β†’ ~5% credit savings per run.


πŸ—οΈ Structural Optimization

Inline Sub-Agent: source-analyzer (Phases 3+4)

As detailed in Recommendation #2. This is the highest-confidence structural change:

  • Task: Read target workflow .md source; return structured JSON (sections, tool config, verbosity, setup-prefix patterns).
  • Why smaller model fits: Fully extractive β€” read one markdown file, identify headings, check for string patterns, count lines. No strategic synthesis or cross-referencing required.
  • Score: 9/10 (Independence 2, Small-model 3, Parallelism 2, Size 2).
  • Main prompt change: Replace ## Phase 3 β€” Read Workflow Source + ## Phase 4 β€” Structural Optimization Checks with a sub-agent call block; consume returned JSON artifact in Phase 5 synthesis.
  • No existing sub-agents β€” this would be the first ## agent: block added to this workflow.

⚠️ Caveats

  • No AI-credit data available. The gh aw logs pipeline returned empty results for all 14 analyzed runs. Savings estimates are proportional reductions inferred from wall-clock durations, not measured AIC values.
  • Duration proxy limitations. Wall-clock time includes ~1–2 min of container startup and artifact upload overhead. Agent computation time is shorter; savings figures are conservative.
  • 14-run sample. Two long-run outliers (12+ min) drove the Β±1.9 min std-dev. Recommendations 1, 3, and 4 disproportionately benefit those outlier runs.
  • Reliability is strong (14/14 success). No reliability-driven optimizations are warranted.

References: Β§27023961142 Β· Β§26646567699 Β· Β§26407979066

Generated by Agentic Workflow AI Credit Spend Optimizer Β· ● 14.4M Β· β—·

  • expires on Jun 15, 2026, 3:49 PM UTC

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    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