Skip to content

feat: add "Optimize token consumption" section to agent failure issues when a guardrail triggers#39069

Merged
pelikhan merged 4 commits into
mainfrom
copilot/update-agent-failure-message-template
Jun 13, 2026
Merged

feat: add "Optimize token consumption" section to agent failure issues when a guardrail triggers#39069
pelikhan merged 4 commits into
mainfrom
copilot/update-agent-failure-message-template

Conversation

Copilot AI commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Agent failure issues caused by guardrail limits (max-ai-credits, max-daily-ai-credits, max-tool-denials, max-turns/timeout) previously only offered a "Debug" path. This adds an "Optimize token consumption" details section inserted before the debug section, giving a direct path to reduce costs rather than just re-investigate the failure.

New files

  • optimize.md — Root-level self-contained loader (mirrors debug.md): installs gh aw, then delegates to .github/aw/optimize-agentic-workflow.md
  • .github/aw/optimize-agentic-workflow.md — Optimization prompt with guardrail-specific entry points (max-ai-credits, max-daily-ai-credits, max-tool-denials, max-turns/timeout), a structured analysis plan (measure → identify top cost drivers → quick wins → sub-agents → experiment → validate), and editing workflow
  • .github/skills/optimize-agentic-workflow/SKILL.md — Proper skill (with name: + description: frontmatter) exposing the same optimization guidance; referenced by the agentic-workflows router for "Optimize token usage and cost" tasks
  • actions/setup/md/optimize_token_consumption_context.md — Template rendered into the issue section:
<details>
<summary>Optimize token consumption</summary>

This failure was triggered by a guardrail limit (max-ai-credits). Use this prompt with any coding agent...

Optimize the agentic workflow token consumption using https://raw.githubusercontent.com/github/gh-aw/main/optimize.md

The workflow run is at {run_url}

</details>
```

### Modified files

- **`agent_failure_issue.md`** — Adds `{optimize_token_consumption_context}` placeholder before the "Debug with any coding agent" section
- **`handle_agent_failure.cjs`** — Adds `buildOptimizeTokenConsumptionContext({ maxAICreditsExceeded, hasDailyAICExceeded, hasToolDenialsExceeded, isTimedOut, runUrl })` — returns empty string when no guardrail was triggered; wired into the issue template context only (not the comment)
- **`handle_agent_failure.test.cjs`** — 6 new tests covering the no-op case, each guardrail label, and priority ordering
- **`agentic_workflows_fallback_aw_files.json`** — Syncs `optimize-agentic-workflow.md` into the fallback file list
- **`.github/skills/agentic-workflows/SKILL.md`** — Routes "Optimize token usage and cost" tasks to the new `.github/skills/optimize-agentic-workflow/SKILL.md` skill

…for guardrail failures

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title feat: add "Optimize token consumption" section to agent failure issues for guardrail failures feat: add "Optimize token consumption" section to agent failure issues when a guardrail triggers Jun 13, 2026
Copilot AI requested a review from pelikhan June 13, 2026 11:50
Comment thread .github/aw/optimize-agentic-workflow.md
Copilot AI and others added 2 commits June 13, 2026 12:37
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI requested a review from pelikhan June 13, 2026 12:47
@pelikhan pelikhan marked this pull request as ready for review June 13, 2026 13:14
Copilot AI review requested due to automatic review settings June 13, 2026 13:14

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

Adds an “Optimize token consumption” path to guardrail-triggered agent failure issues, so users can reduce AI cost/turn usage directly (instead of only debugging). This extends the agent-failure issue template, adds a rendered details block template, and introduces new optimization prompt/skill content that can be loaded via fallback files.

Changes:

  • Add an optimize_token_consumption_context details section (template + renderer) and wire it into agent-failure issue creation when guardrails trigger.
  • Introduce new optimization prompt content (optimize.md, .github/aw/optimize-agentic-workflow.md) and a dedicated skill (.github/skills/optimize-agentic-workflow/SKILL.md).
  • Sync the new .github/aw/optimize-agentic-workflow.md into the fallback file list and reference it from the router skill.
Show a summary per file
File Description
pkg/cli/data/agentic_workflows_fallback_aw_files.json Adds optimize-agentic-workflow.md to the fallback .github/aw file set.
optimize.md New root-level loader prompt that installs gh aw and delegates to the optimization prompt.
actions/setup/md/optimize_token_consumption_context.md New issue-template snippet rendered when a guardrail triggers.
actions/setup/md/agent_failure_issue.md Inserts {optimize_token_consumption_context} before the existing debug section.
actions/setup/js/handle_agent_failure.cjs Adds buildOptimizeTokenConsumptionContext(...) and threads it into issue template context.
actions/setup/js/handle_agent_failure.test.cjs Adds unit tests for buildOptimizeTokenConsumptionContext guardrail rendering and priority.
.github/skills/optimize-agentic-workflow/SKILL.md New skill describing a guardrail-aware token/cost optimization workflow.
.github/skills/agentic-workflows/SKILL.md Adds .github/aw/optimize-agentic-workflow.md to the router’s load list.
.github/aw/optimize-agentic-workflow.md New optimization prompt with guardrail entry points and a structured improvement plan.

Copilot's findings

Tip

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

  • Files reviewed: 9/9 changed files
  • Comments generated: 3

Comment on lines 33 to 36
- `.github/aw/messages.md`
- `.github/aw/network.md`
- `.github/aw/optimize-agentic-workflow.md`
- `.github/aw/patterns.md`
Comment thread optimize.md
Comment on lines +61 to +63
- GitHub CLI (`gh`) is installed and authenticated
- The installation script completed without errors
- `~/.local/share/gh/extensions` is in your PATH
Comment on lines +93 to +97
After measuring token usage, produce a prioritized plan:

1. **Measure** — run `gh aw audit <run-id>` and summarize AI Credits and per-call token breakdown
2. **Identify top cost drivers** — list the three most expensive phases/tool calls
3. **Apply quick wins first** — DataOps pre-steps, `gh-proxy`, `cli-proxy`, prompt trimming
Comment thread actions/setup/md/agent_failure_issue.md Outdated
**Assign this issue to an agent** to debug and fix the issue.

<details>
{optimize_token_consumption_context}<details>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot must have a new line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in the latest commit — {optimize_token_consumption_context} now occupies its own line before the <details> block.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
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.

3 participants