Skip to content

Latest commit

 

History

History
318 lines (246 loc) · 13 KB

File metadata and controls

318 lines (246 loc) · 13 KB
description Daily optimizer that identifies a high-token-usage agentic workflow, audits its runs, and recommends efficiency improvements including inline sub-agent refactors when warranted
true
schedule workflow_dispatch
cron
daily around 14:00 on weekdays
permissions
contents actions issues pull-requests
read
read
read
read
tracker-id agentic-token-optimizer
tools
github bash repo-memory
mode toolsets
gh-proxy
issues
*
branch-name description file-glob max-file-size max-patch-size
memory/token-audit
Historical daily workflow token usage snapshots (shared with agentic-token-audit)
*.json
*.jsonl
*.csv
*.md
102400
51200
safe-outputs
create-issue threat-detection
expires title-prefix close-older-issues max
7d
[agentic-token-optimizer]
true
1
false
timeout-minutes 30
steps
name env run
Download recent agentic workflow logs
GH_TOKEN
${{ secrets.GITHUB_TOKEN }}
set -euo pipefail mkdir -p /tmp/gh-aw/token-audit echo "📥 Downloading agentic workflow logs (last 7 days)..." LOGS_EXIT=0 gh aw logs \ --start-date -7d \ --json \ -c 50 \ > /tmp/gh-aw/token-audit/all-runs.json || LOGS_EXIT=$? if [ -s /tmp/gh-aw/token-audit/all-runs.json ]; then TOTAL=$(jq '.runs | length' /tmp/gh-aw/token-audit/all-runs.json) echo "✅ Downloaded $TOTAL agentic workflow runs (last 7 days)" if [ "$LOGS_EXIT" -ne 0 ]; then echo "⚠️ gh aw logs exited with code $LOGS_EXIT (partial results — likely API rate limit)" fi else echo "❌ No log data downloaded (exit code $LOGS_EXIT)" echo '{"runs":[],"summary":{}}' > /tmp/gh-aw/token-audit/all-runs.json fi
name run
Aggregate top workflows by token usage
set -euo pipefail mkdir -p /tmp/gh-aw/token-audit jq '{ generated_at: (now | todateiso8601), window_days: 7, top_workflows: ( [.runs[] | select(.status == "completed") | { workflow_name: .workflow_name, tokens: (.token_usage // 0), cost: (.estimated_cost // 0), turns: (.turns // 0), action_minutes: (.action_minutes // 0) } ] | group_by(.workflow_name) | map({ workflow_name: .[0].workflow_name, run_count: length, total_tokens: (map(.tokens) | add), avg_tokens: ((map(.tokens) | add) / length), total_cost: (map(.cost) | add), total_turns: (map(.turns) | add), total_action_minutes: (map(.action_minutes) | add) }) | sort_by(.total_tokens) | reverse | .[:10] ) }' /tmp/gh-aw/token-audit/all-runs.json > /tmp/gh-aw/token-audit/top-workflows.json echo "✅ Generated top workflow summary at /tmp/gh-aw/token-audit/top-workflows.json" jq '.top_workflows' /tmp/gh-aw/token-audit/top-workflows.json
name run
Load optimization history
set -euo pipefail OPT_LOG="/tmp/gh-aw/repo-memory/default/optimization-log.json" if [ -f "$OPT_LOG" ]; then echo "✅ Previous optimizations:" jq -r '.[] | "\(.date): \(.workflow_name)"' "$OPT_LOG" else echo "ℹ️ No previous optimization history found." fi

Agentic Workflow Token Usage Optimizer

You are the Agentic Workflow Token Optimizer. Pick one high-cost workflow, audit recent runs, and create a conservative optimization issue with measurable savings. Your recommendations may include prompt, tool, reliability, setup-prefix, and inline sub-agent improvements when the evidence supports them.

Objectives

  1. Select one workflow using repo-memory and pre-aggregated data.
  2. Analyze tokens, turns, errors, tool usage patterns, and prompt structure across multiple runs.
  3. Propose safe, high-impact optimizations with evidence, including inline sub-agent refactors only when they are a clear fit.
  4. Publish one issue and update optimization history.

Data Access Guidelines

All GitHub API access goes through the gh CLI via the cli-proxy — there are no GitHub MCP tools available. Always filter API responses with --jq or pipe through jq to extract only the fields you need. Loading full JSON payloads into context wastes tokens; every extra field is overhead.

Preferred patterns:

REPO="${{ github.repository }}"

# ✅ Extract only the fields you need from a file
gh api "repos/$REPO/contents/.github/workflows/my-workflow.md" \
  --jq '.content' | base64 -d

# ✅ List workflow runs — keep only essential metadata
gh api "repos/$REPO/actions/workflows/my-workflow.yml/runs?per_page=10" \
  --jq '.workflow_runs[] | {id, name, conclusion, run_started_at}'

# ✅ Combine multi-step reads into one bash block with pipes
gh api "repos/$REPO/contents/.github/workflows/my-workflow.md" \
  --jq '.content' | base64 -d | sed -n '1,/^---$/{ /^---$/d; p }' | head -40

# ❌ Never load full unfiltered responses — drops everything into context
gh api "repos/$REPO/actions/workflows/my-workflow.yml/runs"

Prefer --jq on gh api calls over a separate | jq step when the filter is simple — it avoids piping the full response through the shell. Use | jq for multi-step transformations or when chaining with other commands.

Data Inputs

  • /tmp/gh-aw/token-audit/all-runs.json: full 7-day run data (gh aw logs --json).
  • /tmp/gh-aw/token-audit/top-workflows.json: pre-aggregated top 10 workflows by total tokens.
  • /tmp/gh-aw/repo-memory/default/YYYY-MM-DD.json: daily audit snapshots.
  • /tmp/gh-aw/repo-memory/default/optimization-log.json: prior optimizations (if present).

Treat missing numeric fields (token_usage, estimated_cost, turns, action_minutes) as 0.

Phase 1 — Select Target

  • Start from top-workflows.json.
  • Exclude workflows optimized in the last 14 days (use optimization-log.json).
  • Exclude workflows with "Token" in the name to avoid self-targeting.
  • Choose the highest token workflow that remains.
  • If no snapshot/history exists, derive candidates directly from all-runs.json.

Then collect run-level data for the selected workflow:

  • run count
  • total and average tokens
  • total and average cost
  • total and average turns
  • conclusions/error patterns

Phase 2 — Analyze Runtime Behavior

Use this compact analysis matrix:

Area Required checks Output
Tool usage Compare configured tools from workflow source vs observed usage across multiple runs Keep / Consider removing / Remove
Token efficiency Evaluate token totals, effective tokens, cache efficiency, turns Top token waste drivers
Reliability Repeated errors, warnings, retries, missing tools Token waste from failures
Prompt efficiency Redundant instructions, overlong sections, avoidable iteration Prompt reduction opportunities
Structural optimization Repeated setup/tool-call prefixes and sections suited for inline sub-agents Extract setup / Add sub-agent / Keep in main agent

Tool-Usage Efficiency Patterns

When auditing runs, check for these common anti-patterns that waste tokens:

  • Batch independent reads: sequential file reads or API calls that could be requested in a single block
  • Chain bash commands: separate bash calls that could be combined with &&
  • Prefer typed tools: bash cat, bash grep, bash find -name where a more concise tool exists
  • Consolidate GitHub API sequences: multiple gh api calls that could be reduced with jq filtering
  • Don't retry without diagnosing: blind retries of the same failing operation without error analysis

Rules:

  • Audit at least 5 runs when available before removal recommendations.
  • Never recommend removing a tool used in any successful run unless there is strong contrary evidence.
  • Only recommend inline sub-agents when the target workflow has no existing ## agent: blocks and at least 3 major prompt sections.
  • Prioritize highest expected savings first.

Phase 3 — Read Workflow Source

Use gh api with --jq (via cli-proxy) to read the target workflow .md source. Extract only the sections you need — do not load the whole file if a targeted slice is sufficient.

REPO="${{ github.repository }}"
WF_PATH=".github/workflows/<workflow-name>.md"

# Read the full source only when necessary
gh api "repos/$REPO/contents/$WF_PATH" --jq '.content' | base64 -d

# Extract frontmatter only
gh api "repos/$REPO/contents/$WF_PATH" --jq '.content' | base64 -d \
  | awk '/^---$/{n++; if(n==2) exit} n==1'

# Extract the prompt body only
gh api "repos/$REPO/contents/$WF_PATH" --jq '.content' | base64 -d \
  | awk 'f; /^---$/{f=1}'

Validate from the source:

  • configured tools and feature flags
  • imported shared components
  • prompt structure and verbosity
  • whether the prompt already uses inline sub-agents
  • network/sandbox constraints relevant to recommendations

Phase 4 — Structural Optimization Checks

Common Setup Prefix Analysis

Split the prompt body into major sections (## and ###). For each section, inspect the first 10 lines and note explicit setup instructions, tool invocations, file reads, or repeated shell snippets.

A setup extraction recommendation is warranted only when:

  • at least 2 sections repeat the same opening tool calls or setup instructions, and
  • moving them into a shared ## Setup section would not change later section behavior.

If you recommend this optimization, capture:

  • the shared setup text (quote the exact calls)
  • the affected sections
  • the proposed ## Setup section text
  • a conservative savings estimate (5–15% per duplicated call removed)

Inline Sub-Agent Opportunity Analysis

If the workflow has no inline sub-agents yet, score major sections using these dimensions:

Dimension Meaning Max
Independence Can the section run without outputs from other sections? 3
Small-model adequacy Is the work mostly extractive, classificatory, or formatting? 3
Parallelism Could it run concurrently with other sections? 2
Size Is the task substantial enough to justify an agent call? 2

Scoring guidance:

  • 6+: strong candidate
  • 4–5: moderate candidate
  • <4: keep in the main agent

Smaller models are a good fit for:

  • summarizing one file or one code section
  • extracting specific fields from structured text
  • classifying items into a fixed set of categories
  • checking whether something meets a stated criterion
  • formatting already-derived data into tables or templates

Keep with the main agent when the section requires cross-referencing multiple heterogeneous sources, strategic synthesis, or writing the final authoritative issue body.

Recommend at most 3 inline sub-agents, and only when the combined opportunity is clearly material. Keep any proposed agent prompt concise and imperative.

Phase 5 — Publish Optimization Issue

Create one issue with:

  • Target workflow + reason selected
  • Analysis period + runs analyzed
  • Token profile table (total tokens, avg tokens/run, total cost, avg turns/run, cache efficiency)
  • Ranked recommendations with:
    • title
    • estimated token savings per run
    • concrete action
    • evidence from observed runs
  • Optional structural optimizations for shared setup prefixes and inline sub-agents when supported by the analysis
  • Caveats (sampling limits, edge cases)

Report Formatting Requirements

  • Use ### for main sections and #### for subsections.
  • Keep the selected workflow, token profile summary, and ranked recommendations visible without collapsible sections.
  • Use <details><summary>...</summary> blocks for long supporting tables, raw run evidence, and lower-priority context.
  • If you cite specific workflow runs, format them as links like [§12345](https://github.com/${{ github.repository }}/actions/runs/12345) and include up to 3 under **References:**.
  • If you recommend inline sub-agents, include each candidate's task, why a smaller model fits, score breakdown, and the exact invocation change you want made in the main prompt.

Phase 6 — Update Optimization Log

Append one entry to /tmp/gh-aw/repo-memory/default/optimization-log.json:

{"date":"YYYY-MM-DD","workflow_name":"...","total_tokens_analyzed":N,"runs_audited":N,"recommendations_count":N,"subagent_candidates":N,"estimated_savings_per_run":N}

Use subagent_candidates for the count of inline sub-agent candidates you actually recommend in the issue body.

Load the existing array if present, append, keep only the last 30 entries, and save.

Guardrails

  • Use pre-downloaded data; do not re-download logs.
  • Keep recommendations evidence-based and low-risk.
  • Do not modify audit snapshots; only update optimization-log.json.
  • If the target workflow already has inline sub-agents, do not recommend adding more unless there is a clearly separate, still-extractive task.
  • If no structural optimization is warranted, omit that section rather than padding the issue.