Skip to content

feat(copilot-opt): reduce first-turn context size with lazy skill blocks and pre-agent guards#46969

Merged
pelikhan merged 3 commits into
mainfrom
copilot/copilot-opt-reduce-initial-context-payloads
Jul 21, 2026
Merged

feat(copilot-opt): reduce first-turn context size with lazy skill blocks and pre-agent guards#46969
pelikhan merged 3 commits into
mainfrom
copilot/copilot-opt-reduce-initial-context-payloads

Conversation

Copilot AI commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

The copilot-opt workflow was inlining all Phase 2–5 instructions into the initial system message, contributing to first-turn inputs of 24k–80k+ tokens. Three changes reduce this:

Lazy-load Phases 2–5 via ## skill: blocks

Detailed instructions for Performance Analysis, PR Cross-Analysis, Recommendation Selection, and Issue Creation are moved out of the main prompt body into four inline skill blocks (opt-phase-performance, opt-phase-pr-analysis, opt-phase-recommendations, opt-phase-issue-creation). The gh-aw runtime strips these from the initial system message at activation and restores them as on-demand skill files — removing ~5KB (~1,600 tokens) from every first turn.

## Phase 2 — Performance Analysis

Invoke skill `opt-phase-performance` for the full performance analysis procedure.

## skill: `opt-phase-performance`
---
description: Phase 2 — detailed per-session performance analysis procedure
---
For each session summary: ...

Token budget guard step

A pre-agent step reads the compiled prompt.txt, estimates tokens at a conservative 3 chars/token, and emits a ::warning:: annotation if the estimate exceeds 50k — surfacing budget overruns in CI before the agent starts.

Trim large session logs step

A pre-agent step trims any session log file over 10KB to first 5KB + retrieval notice + last 1KB, preventing the agent from accidentally loading full multi-turn transcripts (which can exceed 40KB for 80-turn sessions) in a single cat.

Copilot AI and others added 2 commits July 21, 2026 05:02
…ll blocks and adding guards

- Move Phase 2 (Performance Analysis) to skill 'opt-phase-performance'
- Move Phase 3 (PR Cross-Analysis) to skill 'opt-phase-pr-analysis'
- Move Phase 4 (Recommendation Selection) to skill 'opt-phase-recommendations'
- Move Phase 5 (Issue Creation) to skill 'opt-phase-issue-creation'
- Add token budget guard step: warns when estimated first-turn input exceeds 50k tokens
- Add trim-large-session-logs step: strips boilerplate from session files >10KB before agent reads them
- Recompile copilot-opt.lock.yml

Closes #46903

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
…y comment

- Rename STRIPPED_KB → REMOVED_KB to clarify the variable represents removed content in KB
- Use %d (integer) format specifier instead of %s for printf
- Add comment explaining why overlap guard is unnecessary (THRESHOLD > FIRST_CHUNK+LAST_CHUNK)
- Update token estimate comment to match divisor (3 chars/token conservative estimate)

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix oversized initial context payloads causing elevated costs feat(copilot-opt): reduce first-turn context size with lazy skill blocks and pre-agent guards Jul 21, 2026
Copilot AI requested a review from pelikhan July 21, 2026 05:24
@pelikhan
pelikhan marked this pull request as ready for review July 21, 2026 05:25
Copilot AI review requested due to automatic review settings July 21, 2026 05:25
@pelikhan
pelikhan merged commit 9ae8489 into main Jul 21, 2026
1 check passed
@pelikhan
pelikhan deleted the copilot/copilot-opt-reduce-initial-context-payloads branch July 21, 2026 05:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Reduces Copilot Opt’s initial context size through lazy-loaded phase instructions and pre-agent context guards.

Changes:

  • Moves Phases 2–5 into inline skills.
  • Adds prompt-size monitoring and session-log trimming.
  • Regenerates the compiled workflow.
Show a summary per file
File Description
.github/workflows/copilot-opt.md Defines lazy skills and context guards.
.github/workflows/copilot-opt.lock.yml Compiles the updated workflow.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Medium

Comment on lines +67 to +69
PROMPT_CHARS=$(wc -c < "$PROMPT_FILE")
# Conservative approximation: ~3 chars/token (technical content)
ESTIMATED_TOKENS=$((PROMPT_CHARS / 3))
# THRESHOLD (10240) > FIRST_CHUNK+LAST_CHUNK (6144), so no chunk overlap possible
REMOVED_KB=$(( (SIZE - FIRST_CHUNK - LAST_CHUNK) / 1024 ))
echo "Trimming large session log: $f (${SIZE} bytes, stripping ~${REMOVED_KB}KB of boilerplate)"
{ head -c "$FIRST_CHUNK" "$f"; printf "...[%dKB stripped — fetch raw on demand if needed]\n" "$REMOVED_KB"; tail -c "$LAST_CHUNK" "$f"; } > "${f}.trimmed"
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.82.15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[copilot-opt] Reduce oversized initial context payloads causing elevated first-turn token costs

3 participants