|
| 1 | +--- |
| 2 | +name: optimize-agentic-workflow |
| 3 | +description: Analyze and reduce token consumption in agentic workflows — guardrail-specific entry points, measurement, and optimization techniques. |
| 4 | +--- |
| 5 | + |
| 6 | +# Agentic Workflow Token Optimizer |
| 7 | + |
| 8 | +Help users reduce the AI token usage and cost of GitHub Agentic Workflows in this repository. |
| 9 | + |
| 10 | +## Load These References First |
| 11 | + |
| 12 | +Load these files from `github/gh-aw` (they are not available locally). |
| 13 | + |
| 14 | +- `.github/aw/github-agentic-workflows.md` |
| 15 | +- `.github/aw/token-optimization.md` |
| 16 | +- `.github/aw/workflow-editing.md` |
| 17 | +- `.github/aw/syntax.md` |
| 18 | + |
| 19 | +Load these only when relevant: |
| 20 | + |
| 21 | +- `.github/aw/experiments.md` |
| 22 | +- `.github/aw/safe-outputs.md` |
| 23 | + |
| 24 | +## Available Commands |
| 25 | + |
| 26 | +```bash |
| 27 | +gh aw audit <run-id> --json |
| 28 | +gh aw audit <base-run-id> <optimized-run-id> |
| 29 | +gh aw logs <workflow-name> --json |
| 30 | +gh aw compile <workflow-name> |
| 31 | +gh aw status |
| 32 | +``` |
| 33 | + |
| 34 | +## Start the Conversation |
| 35 | + |
| 36 | +Ask for one of these inputs: |
| 37 | + |
| 38 | +- a workflow run URL (or run ID) to analyze |
| 39 | +- a workflow name to review the source |
| 40 | +- the guardrail that was exceeded (max-ai-credits, max-daily-ai-credits, max-tool-denials, max-turns / timeout) |
| 41 | + |
| 42 | +## Fast Path: Run URL Provided |
| 43 | + |
| 44 | +If the user gives a GitHub Actions run URL: |
| 45 | + |
| 46 | +1. Extract the run ID |
| 47 | +2. Run `gh aw audit <run-id> --json` |
| 48 | +3. Inspect `agent_usage.aic`, `agent_usage.input_tokens`, `agent_usage.output_tokens`, `agent_usage.cache_read_tokens` |
| 49 | +4. Identify the most expensive phases before asking additional questions |
| 50 | + |
| 51 | +## Guardrail-Specific Entry Points |
| 52 | + |
| 53 | +### `max-ai-credits` exceeded |
| 54 | + |
| 55 | +The workflow was stopped because it consumed more AI Credits than the configured per-run budget. |
| 56 | + |
| 57 | +Priority checks: |
| 58 | +1. Which tool calls dominated token usage? (`token-usage.jsonl`) |
| 59 | +2. Is the prompt front-loading large payloads that could be fetched on demand? |
| 60 | +3. Are there repetitive extraction steps that sub-agents could handle cheaply? |
| 61 | +4. Does the frontier model handle tasks that a small model could do? |
| 62 | + |
| 63 | +### `max-daily-ai-credits` exceeded |
| 64 | + |
| 65 | +The workflow is being blocked because its 24-hour AI Credits budget is exhausted. |
| 66 | + |
| 67 | +Priority checks: |
| 68 | +1. What is the run cadence? (scheduled too frequently?) |
| 69 | +2. Does the workflow use cheap triage before escalating to the frontier model? |
| 70 | +3. Is batching or caching applicable to reduce run frequency? |
| 71 | +4. Are there noop early-exits for events that do not require agent action? |
| 72 | + |
| 73 | +### `max-tool-denials` exceeded |
| 74 | + |
| 75 | +The Copilot SDK hit the tool-denial threshold, indicating the prompt attempted actions outside the allowed tool policy. |
| 76 | + |
| 77 | +Priority checks: |
| 78 | +1. What tool was repeatedly denied? (last denied reason in the failure issue) |
| 79 | +2. Is the tool missing from the workflow's permissions/firewall config? |
| 80 | +3. Can the prompt be revised to avoid the denied operation entirely? |
| 81 | +4. Would a DataOps pre-step satisfy the data need without a tool call? |
| 82 | + |
| 83 | +### Timeout / `max-turns` exceeded |
| 84 | + |
| 85 | +The agent ran out of time or turns before completing the task. |
| 86 | + |
| 87 | +Priority checks: |
| 88 | +1. Is the task decomposable into smaller, faster sub-tasks? |
| 89 | +2. Are there long-running tool calls that could be replaced with DataOps pre-steps? |
| 90 | +3. Is the prompt asking the agent to do too much in one run? |
| 91 | +4. Can `max-turns` or `timeout-minutes` be raised, or should the task be split? |
| 92 | + |
| 93 | +## Optimization Analysis Plan |
| 94 | + |
| 95 | +After measuring token usage, produce a prioritized plan: |
| 96 | + |
| 97 | +1. **Measure** — run `gh aw audit <run-id> --json` and summarize AI Credits and per-call token breakdown |
| 98 | +2. **Identify top cost drivers** — list the three most expensive phases/tool calls |
| 99 | +3. **Apply quick wins first** — DataOps pre-steps, `gh-proxy`, `cli-proxy`, prompt trimming |
| 100 | +4. **Sub-agent delegation** — identify repetitive per-item loops suitable for small-model workers |
| 101 | +5. **Prompt caching** — verify stable context appears before dynamic content |
| 102 | +6. **Experiment** — add an `experiments:` entry with `metric: "aic"` to measure the change |
| 103 | +7. **Validate quality** — confirm the optimized run produces equivalent safe outputs |
| 104 | + |
| 105 | +Present the plan clearly before making any edits. Confirm with the user before applying changes. |
| 106 | + |
| 107 | +## Editing Workflow |
| 108 | + |
| 109 | +1. Edit `.github/workflows/<workflow-name>.md` |
| 110 | +2. Recompile: `gh aw compile <workflow-name>` |
| 111 | +3. Commit both the source and the generated `.lock.yml` |
| 112 | +4. Report the estimated savings and link to the PR or commit |
0 commit comments