Skip to content

⚡ Copilot Token Optimization2026-07-08 — Refactoring Opportunity Scanner #6021

Description

@github-actions

Target Workflow: refactoring-scanner

Source report: Created by Daily Copilot Token Usage Analyzer (run #28930856537)
Estimated cost per run: $186.59 AIC
Total tokens per run: ~2.6M
Cache hit rate: 95.6%
LLM turns: ~100 Copilot API calls (run-28919141556)
Model: claude-sonnet-4.6

Current Configuration

Setting Value
Tools loaded 2: bash, github (issues toolset)
Tools actually used 2: bash (grep/awk analysis), github (search + create issues)
Network groups node, github
Pre-agent steps Yes — file-sizes and function-counts pre-computed
Prompt body size 8,016 chars (194 lines)

Analysis

The workflow is already well-optimized on the tools and network dimensions — it uses only 2 tools and restricts GitHub to the issues toolset. The cost is driven entirely by output verbosity and sequential multi-phase analysis:

  • 54,548 output tokens for 4 issues = ~13,600 tokens per issue body (extremely verbose)
  • 86,132 cache_write tokens — long context builds up across 6 sequential analysis phases
  • 100 API calls — each phase triggers multiple reasoning+tool-call cycles
  • The issue template has 7+ sections including full bash command blocks that the agent quotes back verbatim

Recommendations

1. Simplify the Issue Body Template

Estimated savings: ~27,000 output tokens/run (~21% of total cost, ~$39 AIC/run)

The current issue format in Phase 6 has 7 mandatory sections including Evidence (full grep output), Proposed Split (detailed module breakdown), Affected Callers (with embedded bash command), Effort Estimate, and Benefits. Each issue body averages ~13,600 output tokens.

Current template (in .github/workflows/refactoring-scanner.md, Phase 6 section):

### Issue Format
**Body**:
- ## Refactoring Opportunity  (+ Summary, Evidence, Proposed Split, Affected Callers, 
  Effort Estimate, Benefits, Footer)

Proposed simpler template (~50% shorter):

### Issue Format

**Title**: `[Refactoring] Split <file> into focused modules`

**Body**:
## Summary
- **File**: `path/to/file.ts` (**X lines**, N responsibilities)
- **Score**: N points
- **Priority**: High/Medium

## Evidence
[2–3 specific function names or grep output lines that demonstrate the problem — NOT full grep output, just the key findings]

## Proposed Split
[Bullet list of proposed modules with line count estimates only — no bash commands embedded]

---
*Detected by Refactoring Scanner. Run: ${{ github.run_id }}*

Remove these sections entirely:

  • Affected Callers section with embedded bash — agent can add this if needed but it's rarely acted on immediately
  • Benefits section — boilerplate, always the same
  • Full grep output blocks (summarize findings instead of quoting all output)

Implementation: Edit .github/workflows/refactoring-scanner.md, Phase 6 "Issue Format" section. Replace the 7-section template with the 3-section version above.


2. Pre-compute Phase 2 Bash Analysis (Nesting + Function Length)

Estimated savings: ~7,000 tokens/run (~5% cost, ~$4 AIC/run)

Phase 2 (Detect Deep Nesting and Complex Functions) contains 3 bash commands that the agent executes at runtime:

  • awk for finding deeply nested lines (reads entire docker-manager.ts)
  • awk heuristic for long functions
  • grep for functions with 5+ parameters

These are deterministic and can be pre-computed in steps:, same as file-sizes and function-counts.

Add to the steps: section in the frontmatter:

steps:
  - name: Measure file sizes
    id: file-sizes
    run: |   # (existing step)

  - name: Count functions per file
    id: function-counts
    run: |   # (existing step)

  - name: Detect complex code patterns
    id: complex-patterns
    run: |
      {
        echo "COMPLEX_PATTERNS<<EOF"
        echo "=== Deeply nested code (20+ leading spaces) ==="
        awk 'length($0) - length(ltrim($0)) >= 20 {print NR": "$0}' src/docker-manager.ts 2>/dev/null \
          | grep -v "^\s*//" | head -20
        echo "=== Functions with 5+ parameters ==="
        grep -n "function.*(.*, .*, .*, .*, .*," src/docker-manager.ts src/cli.ts 2>/dev/null | head -20
        echo "EOF"
      } >> "$GITHUB_OUTPUT"

Then remove Phase 2 entirely from the prompt body and reference ${{ steps.complex-patterns.outputs.COMPLEX_PATTERNS }} instead.

Implementation: Add complex-patterns step to frontmatter; replace Phase 2 body with a data reference.


3. Remove Unused node Network Group

Estimated savings: 0 tokens (security hygiene only)

The node network group (registry.npmjs.org, nodejs.org, etc.) was in the network allow-list but zero requests were made to any node-related domain in the last run. The workflow uses only bash analysis and GitHub API.

Edit .github/workflows/refactoring-scanner.md frontmatter:

# Before:
network:
  allowed:
    - node
    - github

# After:
network:
  allowed:
    - github

This reduces the attack surface if the agent is ever prompt-injected to exfiltrate data via npm.


4. Consolidate Phase 3 Grep Commands into Pre-Step

Estimated savings: ~3,000 tokens/run (~2% cost)

Phase 3 (Detect Mixed Responsibilities) has 5 bash grep commands that run at agent time:

grep -n "generate|config|Config|yaml|..." src/docker-manager.ts
grep -n "volume|mount|bind|..." src/docker-manager.ts
grep -n "cleanup|teardown|..." src/docker-manager.ts
grep -n "docker|container|..." src/cli.ts

These read the same large files multiple times. Pre-computing them into a responsibility-zones step saves multiple tool-call round trips.

Add to steps::

  - name: Detect responsibility zones
    id: responsibility-zones
    run: |
      {
        echo "RESPONSIBILITY_ZONES<<EOF"
        echo "=== docker-manager.ts: responsibility zones ==="
        grep -n "^export " src/docker-manager.ts 2>/dev/null | head -40
        echo "=== config/generate mixing ==="
        grep -c "generate\|config\|Config\|yaml\|compose" src/docker-manager.ts 2>/dev/null
        echo "=== volume/mount mixing ==="
        grep -c "volume\|mount\|bind\|Volume\|Mount" src/docker-manager.ts 2>/dev/null
        echo "=== cli.ts docker references ==="
        grep -c "docker\|container\|compose" src/cli.ts 2>/dev/null
        echo "EOF"
      } >> "$GITHUB_OUTPUT"

Remove the inline bash in Phase 3 and use ${{ steps.responsibility-zones.outputs.RESPONSIBILITY_ZONES }}.

Expected Impact

Metric Current Projected Savings
Total tokens/run ~2.6M ~2.2M -15%
Output tokens/run 54,548 ~27,000 -50%
Cache write/run 86,132 ~60,000 -30%
Cost/run (AIC) $186.59 ~$140 -25%
LLM turns (API calls) ~100 ~70 -30%
Session time 21m ~15m (est.) -30%

Implementation Checklist

  • Edit .github/workflows/refactoring-scanner.md: simplify Phase 6 issue template (Rec. 1)
  • Edit .github/workflows/refactoring-scanner.md: add complex-patterns step and remove Phase 2 body (Rec. 2)
  • Edit .github/workflows/refactoring-scanner.md: remove node from network.allowed (Rec. 3)
  • Edit .github/workflows/refactoring-scanner.md: add responsibility-zones step and remove Phase 3 body (Rec. 4)
  • Recompile: gh aw compile .github/workflows/refactoring-scanner.md
  • Verify CI passes on PR
  • Compare token usage on next scheduled run vs baseline ($186.59 AIC)

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 Daily Copilot Token Optimization Advisor · 159.9 AIC · ⊞ 6.4K ·

Metadata

Metadata

Assignees

No one assigned

    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