Skip to content

Token Optimization: CI Optimization Coach — reduce 2.8M tokens/run (4.7x over target) #27395

@lpcox

Description

@lpcox

Summary

The Copilot Token Usage Optimizer (run 24673974785) selected CI Optimization Coach (ci-coach.md) as its optimization target. The agent completed its analysis successfully but the detection step failed, preventing the issue from being published. This issue captures the optimizer's findings.

Target Selection

CI Optimization Coach was the #3 highest-token workflow in the 7-day window:

Rank Workflow Runs Total Tokens Avg Tokens/Run
1 Q 2 5,995,014 2,997,507
2 Test Quality Sentinel 13 3,863,221 297,171
3 CI Optimization Coach 1 2,792,855 2,792,855
4 Documentation Noob Tester 1 2,521,089 2,521,089
5 Contribution Check 1 1,544,888 1,544,888

Token Usage Profile

Metric Value Target
Total tokens 2,792,855 300K-600K
Turns 38
Runtime 16 min
Cache efficiency 49%
Input:output ratio 181:1
Over target 4.7x
Over alert threshold (1M) 2.8x

Root Causes

1. Massive prompt overhead (~26KB combined)

The workflow prompt is built from multiple large files:

  • ci-coach.md: 14,526 bytes — includes a verbose PR template with full example comparisons embedded directly in the prompt
  • shared/ci-data-analysis.md: 5,618 bytes
  • shared/ci-optimization-strategies.md: 6,325 bytes
  • shared/reporting.md: additional shared import

This combined ~26KB prompt is loaded into context on every turn. Across 38 turns, even a 4KB reduction saves ~152K tokens.

2. Half of turns spent on data gathering

About 50% of the 38 turns are iterative data-gathering — the agent explores analysis and refinement cycles rather than executing a single optimized path. Much of this could be moved to deterministic pre-steps.

3. Unnecessary/scattered tool calls

  • search_repositories called only once despite being available (wasted tool schema tokens)
  • issue_read invoked 5 times for reading CI-related issues not directly relevant to optimization
  • Full [default] GitHub toolset loads ~22 tools when only a few are needed

4. Expensive pre-steps generating large context

The ci-data-analysis.md shared component runs a full build cycle (make deps-dev, make lint, make build, make recompile, make test-unit) and downloads 100 CI runs + artifacts. The resulting context files are large and the agent reads them across multiple turns.

5. Poor cache efficiency (49%)

Each run generates new artifacts, and the prompt structure doesn't align well with how the agent processes information, leading to poor context reuse.

6. Excessive turn count for task complexity

38 turns is excessive for what should be: read CI config → identify problems → propose fixes → create PR. The agent explores deeply instead of exiting early when there's nothing actionable to optimize.

Recommendations

R1: Trim the embedded PR template — Est. savings: ~152K tokens/run

The ci-coach.md prompt contains a verbose PR template with full example before/after comparisons. Move this to a separate reference file or condense it to a skeleton that the agent fills in, saving ~4KB from the system prompt × 38 turns.

Action: Refactor the PR template out of the inline prompt in ci-coach.md.

R2: Narrow GitHub toolset — Est. savings: ~50-60K tokens/turn overhead

Replace toolsets: [default] with a minimal set. The workflow primarily needs issues (for reading CI-related issues) and pull_requests (for creating PRs). Remove repos, code_search, and other unused toolsets.

Action: Change toolsets: [default]toolsets: [issues, pull_requests] in ci-coach.md frontmatter.

R3: Pre-compute analysis data in bash steps — Est. savings: ~10-15 turns

Move the data-gathering phase into deterministic bash pre-steps. Parse CI run data, identify failure patterns, and compute statistics before the agent starts, then pass structured summaries rather than raw data.

Action: Add bash pre-steps to ci-coach.md or shared/ci-data-analysis.md that produce a structured JSON summary.

R4: Add early exit criteria

If the CI is healthy (no significant failures, no regressions), the agent should exit early instead of exploring for 38 turns. Add explicit instructions: "If no actionable optimizations found after Phase 1 analysis, publish a no-op issue and exit."

Action: Add an early-exit clause to the prompt after the initial analysis phase.

R5: Condense shared components

The two shared components (ci-data-analysis.md at 5.6KB and ci-optimization-strategies.md at 6.3KB) may contain redundant content. Review and consolidate where possible.

Action: Audit both shared components for overlap and condense.

R6: Enable cli-proxy

Add cli-proxy: true to features to enable direct gh CLI access, reducing dependence on the full GitHub MCP toolset for simple operations like reading files.

Action: Add cli-proxy: true to the features section.

Projected Impact

Scenario Tokens/run
Current 2,792,855
R1 (trim PR template) ~2,640K
R1 + R2 (narrow toolset) ~2,100K
R1 + R2 + R3 + R4 (pre-compute + early exit) ~600K-1.2M
All recommendations ~300K-600K (target range)

Source

Analysis by Copilot Token Usage Optimizer, run 24673974785 (April 20, 2026). The optimizer completed its analysis but the detection step failed, so this issue was manually extracted from the agent's output.

Metadata

Metadata

Assignees

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