Skip to content

⚡ Claude Token Optimization2026-05-25 — Smoke Claude #3744

Description

@github-actions

Target Workflow: Smoke Claude

**Latest run (redacted) 24 runs analyzed from past 7 days
Estimated cost per run: $0.065
Total tokens per run: ~188K tokens
Cache read rate: 82.6%
Cache write rate: 16.6%
LLM turns: 6.2 average

Current Configuration

Setting Value
Tools loaded 1 (bash only) ✅
Pre-agent steps 3 (GitHub API, file creation, reachability check) ✅
Network groups None
Prompt size ~1,000 chars (concise) ✅
Model claude-haiku-4-5
Max turns 5

Problem Statement

The workflow is already well-optimized in most dimensions:

  • ✅ Minimal tool surface (bash only)
  • ✅ Pre-agent steps implemented
  • ✅ Concise, directive prompt
  • ✅ Simple task scope

However, the agent takes 6.2 turns on average to complete a trivial task:

  1. Read 3 pre-created files
  2. Call a safe-output tool

Expected turns for this task: 1-2 maximum

This results in:

  • 4-5 unnecessary turns
  • ~113K excess tokens per run
  • $0.039 excess cost per run
  • Slower execution time

Root Cause Analysis

Cache metrics reveal the pattern:

  • Turn 1: ~31K tokens written to cache (initial context load)
  • Turns 2-6: ~155K tokens read from cache (prompt reloaded 5 times)
  • New input per turn: ~690 tokens average (3,442 total ÷ 5 turns)

This suggests the agent is:

  1. Making multiple exploratory bash calls instead of batching
  2. Struggling with safeoutputs CLI syntax (leading to retries)
  3. Ignoring "complete in 1 pass" directive
  4. Possibly validating file existence despite pre-step guarantees

Recommendations

1. Add Explicit Command Templates (Highest Impact)

Estimated savings: ~60K tokens/run (~32%)

The current prompt says "call safeoutputs" but doesn't show exactly how. Claude may be experimenting with different syntax patterns.

Implementation:

Add this section to the prompt (after the task list):

## Expected Commands

Execute these 4 bash commands in a single response:

```bash
# 1. Verify GitHub API data
cat /tmp/gh-aw/agent/recent-prs.json

# 2. Verify GitHub reachability  
cat /tmp/gh-aw/agent/smoke-context.txt

# 3. Verify smoke test file
cat /tmp/gh-aw/agent/smoke-test-claude-RUN_ID.txt

# 4. Report results (if PR) OR noop (if not PR)
if [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then
  printf '{"issue_number":%s,"body":"✅ All checks passed"}' "$PR_NUMBER" > /tmp/result.json
  safeoutputs add_comment . < /tmp/result.json
else
  safeoutputs noop --message "Smoke test completed successfully"
fi
```

**Do not explore, validate, or make additional checks. All data is pre-verified.**

This removes ambiguity and provides a clear execution path.


2. Strengthen Single-Turn Constraint (High Impact)

Estimated savings: ~50K tokens/run (~27%)

The current prompt says "Complete in 1 pass" but Claude may interpret this as "minimize passes" rather than "literally one response."

Implementation:

Replace the "IMPORTANT — Complete in 1 pass:" section with:

**CRITICAL — Single Response Execution:**

This workflow MUST complete in exactly 1 LLM turn (your first response).

All required data exists in pre-created files. There is nothing to explore, investigate, or validate beyond reading the 3 files listed above.

Steps:
1. Make ONE bash tool call containing all 4 commands (shown below)
2. Make ONE safeoutputs tool call based on the trigger type
3. End your response — task complete

If you find yourself thinking "I should check..." or "Let me verify..." — STOP. The pre-steps already verified everything.

This makes the single-turn requirement unambiguous.


3. Pre-Export Environment Variables (Medium Impact)

Estimated savings: ~10K tokens/run (~5%)

The agent may be spending turns figuring out how to access GitHub context variables.

Implementation:

Add a new pre-step that exports all needed variables to a file:

steps:
  # ... existing steps ...
  
  - name: Export workflow context
    run: |
      cat > /tmp/gh-aw/agent/workflow-context.env << 'ENVEOF'
      export GITHUB_EVENT_NAME="EVENT_NAME_HERE"
      export GITHUB_RUN_ID="RUN_ID_HERE"
      export PR_NUMBER="PR_NUMBER_HERE"
      ENVEOF
      echo "Context exported to /tmp/gh-aw/agent/workflow-context.env"

Then update the prompt to reference it:

Source workflow context from `/tmp/gh-aw/agent/workflow-context.env` before executing commands.

4. Reduce Max Turns to 2 (Enforcement)

Estimated savings: Forces single-turn discipline

If the agent can't complete in 1 turn, it should fail fast rather than accumulate 5 extra turns.

Implementation:

engine:
  id: claude
  model: claude-haiku-4-5
  max-turns: 2  # Changed from 5

This creates backpressure: if recommendations 1-3 work, the agent completes in 1 turn. If not, the workflow fails at turn 2 (rather than burning 5 turns), forcing us to iterate on the prompt.


5. Add Post-Step Turn Counter (Observability)

Not a direct optimization, but helps measure impact

Add a post-step to track actual turns used:

post-steps:
  # ... existing steps ...
  
  - name: Report turn usage
    if: always()
    run: |
      # This will be visible in logs for comparison
      echo "::notice::Smoke test completed in N turns (target: 1)"

This makes it easier to validate whether optimizations are working.

Cache Analysis (Anthropic-Specific)

Metric Value Notes
Avg cache write 31,292 tokens ~$0.031/run
Avg cache read 155,311 tokens ~$0.012/run
Cache efficiency 82.6% Excellent reuse
Cache write cost % 48% of total High but justified

Cache write amortization: ✅ Cache writes in Turn 1 are reused across all subsequent turns (5+ reads per write)

Cache cost vs benefit: ✅ Despite high write cost ($0.031), the total cache reads ($0.012) represent 5× reuse, making caching cost-effective

Verdict: Cache strategy is optimal. The issue is not caching but excessive turn count.

Expected Impact

Metric Current Projected Savings
Total tokens/run 188K 75K -60%
Cost/run $0.065 $0.026 -60%
LLM turns 6.2 1-2 -70%
Session time ~60s ~20s (est.) -67%

Annual savings (730 runs/year at 12h intervals):

  • Token reduction: ~82M tokens/year
  • Cost reduction: ~$28.47/year

Implementation Checklist

Notes

This workflow is a smoke test — it should be blazingly fast and deterministic. The current 6.2-turn pattern suggests the agent is overthinking a simple task.

The recommendations above are designed to create a "golden path" that makes it trivial for the agent to succeed in 1 turn, with strong constraints to prevent exploration.

If these changes reduce turn count to 1-2, this workflow will become a model for other simple validation workflows.

Generated by Daily Claude Token Optimization Advisor · ● 3.8M ·

Activity

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions