Skip to content

⚡ Claude Token Optimization2026-06-03 — security-guard #4255

Description

@github-actions

Target Workflow: security-guard

Source report: #4253
Estimated cost per run: $0.43 (agent runs only)
Total tokens per run: ~506K
Cache read rate: N/A (token_usage_summary not present in logs)
Cache write rate: N/A
Effective token multiplier: ~6.6× (3.3M effective / 506K actual)
LLM turns: avg 7.6/run (success avg 5.7; failure avg 11.0)
Model: claude-sonnet-4-5
Period: 25 runs, $10.70 total ($0.43/run avg)

⚠️ 48% failure rate (12/25 runs): every single failed run hit the max-turns ceiling (11 turns), consuming 74% more tokens than successful runs before giving up.

Current Configuration

Setting Value
Tools loaded github (toolsets: pull_requests, repos) — already restricted
Network groups github only — already minimal
Pre-agent steps Yes — PR diff pre-fetched, security relevance pre-checked
Prompt size ~921 tokens (~3,686 chars)
max-turns 10
Model claude-sonnet-4-5
Failure rate 48% (12/25 runs failed)

Run Breakdown

Outcome Runs Avg Tokens Avg Turns Avg Cost
Success 13 408K 5.7 $0.41
Failure 12 714K 11.0 (max) $0.55
Skipped (0 tokens) 3 0 0 $0.00
Overall 25 506K 7.6 $0.43

Turn distribution (all 25 runs):

  • 0 turns: 3 (skipped by security-relevance pre-check ✅)
  • 2 turns: 5 (fast noop — no security changes ✅)
  • 9 turns: 3 (success)
  • 10 turns: 2 (failure, near ceiling)
  • 11 turns: 12 (all failures — every failed run hit the max-turns ceiling)

Recommendations

1. Reduce max-turns from 10 to 6

Estimated savings: ~3.9M tokens/period (~31%), ~$3.20/period

Every failing run exhausts all 10 turns before stopping. Successful runs only need 5.7 turns on average. Cutting max-turns to 6 will cap failed runs before they spiral, while leaving 1 turn of headroom for successful runs.

Change in .github/workflows/security-guard.md:

engine:
  id: claude
  model: claude-sonnet-4-5
  max-turns: 6   # was 10; success avg is 5.7 turns

Per-period projection: failed runs capped at 6 turns instead of 11 → token usage drops from 714K to ~390K. Mixed avg: ~400K/run (vs 506K now).

2. Investigate and fix the root cause of 48% failure rate

Estimated savings: ~$2.00/period if failure rate drops to 10%

Every failed run currently wastes ~$0.55 in tokens without producing useful output. With 12 failures/25 runs, roughly $4.66 of the $10.70 total is spent on failed runs.

The failure pattern strongly correlates with temporary_id_map_status: missing on all 12 failed runs (0 on successful runs). This is a high-confidence signal: the temporary_id map is absent on exactly the runs that fail, suggesting safeoutputs add_comment or safeoutputs noop is failing due to a missing map, causing the agent to retry until max-turns.

Investigate:

# View logs for a specific failed run
gh run view 26868586783 --log | tail -200
# Also check run 26867322267, 26864943353

Look for: safeoutputs CLI errors, tool call failures in the last 3 turns, or loops where the agent re-calls the same tool after a failure.

Fix path: Ensure the agentic workflow runtime populates the temporary_id map for every triggered run, or remove any temporary_id cross-references from the prompt/body that might require the map.

3. Move "stop early" instruction to top of prompt

Estimated savings: ~200K tokens/period (faster noop on trivial PRs)

The current prompt buries STOP EARLY inside the Output Format section. The 5 runs that exit at 2 turns are already doing this correctly, but reinforcing it at the start reduces the risk of the agent reading unnecessary files first.

Current placement (buried in Output Format section):

**STOP EARLY:** If the pre-fetched diff shows no security-weakening changes,
call `safeoutputs noop` immediately.

Proposed (first block in prompt, before Repository Context):

## ⚡ Fast Path

Read the pre-fetched diff below first. If it contains **no** security-weakening
changes (no ACCEPT/DROP edits, no capability additions, no ACL regressions,
no seccomp relaxations), call `safeoutputs noop` immediately — do not read
additional files or make further tool calls.

4. Trim verbose "Security Checks" section

Estimated savings: ~40 tokens/turn × 7.6 turns = ~300 tokens/run (minor)

Condense the inline checklist to reduce prompt tokens:

## Security Checks
Focus: weakened DROP/REJECT, added capabilities (SYS_ADMIN/NET_RAW), expanded ACCEPT,
Squid ACL regressions, seccomp relaxation, DNS bypass, input validation removal.

Cache Analysis

No per-turn token_usage_summary data is available — Anthropic cache hit/write metrics are not in the current log format. However, the 6.6× effective token multiplier (3.3M effective vs 506K actual) strongly suggests Anthropic automatic prompt caching is active.

Metric Value
Avg time between turns 27.7s
Cache TTL (Anthropic) ~5 min
Within-run cache reuse ✅ Expected (27.7s < 5 min TTL)
Cross-run cache reuse ❌ Unlikely (PR diff changes every run)

The system prompt + AGENTS.md constant prefix should be cached after turn 1 within each run. No cross-run optimization is available since the PR diff varies.

Expected Impact

Metric Current Projected (Rec 1+2) Savings
Total tokens/run 506K ~350K -31%
Cost/run $0.43 ~$0.30 -30%
LLM turns (failed runs) 11 6 -45%
Period cost (25 runs) $10.70 ~$7.50 -$3.20
Failure waste ~$4.66 ~$1.80 -$2.86

Implementation Checklist

  • Reduce max-turns to 6 in .github/workflows/security-guard.md
  • Move "stop early" / fast-path block to top of prompt
  • Investigate temporary_id_map_status: missing on all 12 failed runs (start with run 26868586783)
  • Fix the safeoutputs failure causing the agent to loop to max-turns
  • Trim Security Checks enumeration
  • Recompile: gh aw compile .github/workflows/security-guard.md
  • Post-process: npx tsx scripts/ci/postprocess-smoke-workflows.ts
  • Verify CI passes on PR
  • Compare token usage on 5+ new runs vs baseline (target: <400K tokens/run, <15% failure rate)

Generated by Daily Claude Token Optimization Advisor · sonnet46 1.9M ·

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions