You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: .github/workflows/agentic-token-optimizer.md
+75-22Lines changed: 75 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
description: Daily optimizer that identifies a high-token-usage agentic workflow, audits its runs, and recommends efficiency improvements
2
+
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
3
3
on:
4
4
schedule:
5
5
- cron: "daily around 14:00 on weekdays"
@@ -111,13 +111,13 @@ steps:
111
111
112
112
# Agentic Workflow Token Usage Optimizer
113
113
114
-
You are the Agentic Workflow Token Optimizer. Pick one high-cost workflow, audit recent runs, and create a conservative optimization issue with measurable savings.
114
+
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.
115
115
116
116
## Objectives
117
117
118
118
1. Select one workflow using repo-memory and pre-aggregated data.
119
-
2. Analyze tokens, turns, errors, and tool usage patterns across multiple runs.
120
-
3. Propose safe, high-impact optimizations with evidence.
119
+
2. Analyze tokens, turns, errors, tool usage patterns, and prompt structure across multiple runs.
120
+
3. Propose safe, high-impact optimizations with evidence, including inline sub-agent refactors only when they are a clear fit.
121
121
4. Publish one issue and update optimization history.
122
122
123
123
## Data Access Guidelines
@@ -172,31 +172,33 @@ Then collect run-level data for the selected workflow:
172
172
- total and average turns
173
173
- conclusions/error patterns
174
174
175
-
## Phase 2 — Analyze
175
+
## Phase 2 — Analyze Runtime Behavior
176
176
177
177
Use this compact analysis matrix:
178
178
179
179
| Area | Required checks | Output |
180
180
|---|---|---|
181
-
| Tool usage | Compare configured tools from workflow source (read with `gh api … --jq '.content' \| base64 -d \| sed -n …` to extract only the frontmatter) vs observed usage across multiple runs | Keep / Consider removing / Remove |
181
+
| Tool usage | Compare configured tools from workflow source vs observed usage across multiple runs | Keep / Consider removing / Remove |
| Structural optimization | Repeated setup/tool-call prefixes and sections suited for inline sub-agents | Extract setup / Add sub-agent / Keep in main agent |
185
186
186
187
### Tool-Usage Efficiency Patterns
187
188
188
189
When auditing runs, check for these common anti-patterns that waste tokens:
189
190
190
-
-**Batch independent reads**: look for sequential file reads or API calls that could be requested in a single tool-use block — each extra turn repeats the full context
191
-
-**Chain bash commands**: look for separate bash tool calls that could be combined with `&&` — each call adds a full context echo
192
-
-**Prefer typed tools**: look for `bash cat`, `bash grep`, `bash find -name`when `view`, `grep`, `glob` would return more concise output
193
-
-**Consolidate GitHub API sequences**: look for multiple sequential `gh api` calls that could be combined into fewer round-trips with `jq` filtering
194
-
-**Don't retry without diagnosing**: look for blind retries of the same failing operation without error analysis — each retry wastes a full turn
191
+
-**Batch independent reads**: sequential file reads or API calls that could be requested in a single block
192
+
-**Chain bash commands**: separate bash calls that could be combined with `&&`
193
+
-**Prefer typed tools**: `bash cat`, `bash grep`, `bash find -name`where a more concise tool exists
194
+
-**Consolidate GitHub API sequences**: multiple `gh api` calls that could be reduced with `jq` filtering
195
+
-**Don't retry without diagnosing**: blind retries of the same failing operation without error analysis
195
196
196
197
Rules:
197
198
198
199
- Audit at least 5 runs when available before removal recommendations.
199
200
- Never recommend removing a tool used in any successful run unless there is strong contrary evidence.
201
+
- Only recommend inline sub-agents when the target workflow has no existing `## agent:` blocks and at least 3 major prompt sections.
200
202
- Prioritize highest expected savings first.
201
203
202
204
## Phase 3 — Read Workflow Source
@@ -205,17 +207,16 @@ Use `gh api` with `--jq` (via cli-proxy) to read the target workflow `.md` sourc
205
207
206
208
```bash
207
209
REPO="${{ github.repository }}"
208
-
# Replace <workflow-name> with the actual .md filename, e.g. "copilot-agent-analysis"
209
210
WF_PATH=".github/workflows/<workflow-name>.md"
210
211
211
-
# Read the full source (only when necessary — prefer targeted slices below)
212
+
# Read the full source only when necessary
212
213
gh api "repos/$REPO/contents/$WF_PATH" --jq '.content'| base64 -d
213
214
214
-
# Extract frontmatter only (tools, features, network, permissions)
215
+
# Extract frontmatter only
215
216
gh api "repos/$REPO/contents/$WF_PATH" --jq '.content'| base64 -d \
216
217
| awk '/^---$/{n++; if(n==2) exit} n==1'
217
218
218
-
# Extract the prompt body only (everything after the closing ---)
219
+
# Extract the prompt body only
219
220
gh api "repos/$REPO/contents/$WF_PATH" --jq '.content'| base64 -d \
220
221
| awk 'f; /^---$/{f=1}'
221
222
```
@@ -225,9 +226,57 @@ Validate from the source:
225
226
- configured tools and feature flags
226
227
- imported shared components
227
228
- prompt structure and verbosity
229
+
- whether the prompt already uses inline sub-agents
228
230
- network/sandbox constraints relevant to recommendations
229
231
230
-
## Phase 4 — Publish Optimization Issue
232
+
## Phase 4 — Structural Optimization Checks
233
+
234
+
### Common Setup Prefix Analysis
235
+
236
+
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.
237
+
238
+
A setup extraction recommendation is warranted only when:
239
+
240
+
- at least 2 sections repeat the same opening tool calls or setup instructions, and
241
+
- moving them into a shared `## Setup` section would not change later section behavior.
242
+
243
+
If you recommend this optimization, capture:
244
+
245
+
- the shared setup text (quote the exact calls)
246
+
- the affected sections
247
+
- the proposed `## Setup` section text
248
+
- a conservative savings estimate (5–15% per duplicated call removed)
249
+
250
+
### Inline Sub-Agent Opportunity Analysis
251
+
252
+
If the workflow has no inline sub-agents yet, score major sections using these dimensions:
253
+
254
+
| Dimension | Meaning | Max |
255
+
|---|---|---|
256
+
| Independence | Can the section run without outputs from other sections? | 3 |
257
+
| Small-model adequacy | Is the work mostly extractive, classificatory, or formatting? | 3 |
258
+
| Parallelism | Could it run concurrently with other sections? | 2 |
259
+
| Size | Is the task substantial enough to justify an agent call? | 2 |
260
+
261
+
Scoring guidance:
262
+
263
+
-`6+`: strong candidate
264
+
-`4–5`: moderate candidate
265
+
-`<4`: keep in the main agent
266
+
267
+
Smaller models are a good fit for:
268
+
269
+
- summarizing one file or one code section
270
+
- extracting specific fields from structured text
271
+
- classifying items into a fixed set of categories
272
+
- checking whether something meets a stated criterion
273
+
- formatting already-derived data into tables or templates
274
+
275
+
Keep with the main agent when the section requires cross-referencing multiple heterogeneous sources, strategic synthesis, or writing the final authoritative issue body.
276
+
277
+
Recommend at most 3 inline sub-agents, and only when the combined opportunity is clearly material. Keep any proposed agent prompt concise and imperative.
278
+
279
+
## Phase 5 — Publish Optimization Issue
231
280
232
281
Create one issue with:
233
282
@@ -239,27 +288,31 @@ Create one issue with:
239
288
- estimated token savings per run
240
289
- concrete action
241
290
- evidence from observed runs
291
+
-**Optional structural optimizations** for shared setup prefixes and inline sub-agents when supported by the analysis
242
292
-**Caveats** (sampling limits, edge cases)
243
293
244
-
Use `<details>` blocks for long supporting tables.
245
-
246
294
### Report Formatting Requirements
247
295
248
-
- Use `###` for main sections and `####` for subsections inside the issue body.
296
+
- Use `###` for main sections and `####` for subsections.
249
297
- Keep the selected workflow, token profile summary, and ranked recommendations visible without collapsible sections.
250
298
- Use `<details><summary>...</summary>` blocks for long supporting tables, raw run evidence, and lower-priority context.
251
299
- 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:**`.
300
+
- 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.
252
301
253
-
## Phase 5 — Update Optimization Log
302
+
## Phase 6 — Update Optimization Log
254
303
255
304
Append one entry to `/tmp/gh-aw/repo-memory/default/optimization-log.json`:
0 commit comments