Skip to content

[agentic-token-optimizer] Daily XKCD Comic — optimization: pre-compute selection in setup step to cut turns and token growth #314

Description

@github-actions

Target Workflow

Daily XKCD Comic — selected as highest-AIC workflow not optimized in the last 14 days (last optimized 2026-07-13, 8 days ago). It accounted for 309 AIC / 152k tokens in the most recent 7-day window across 6 successful runs.


Analysis Period

Metric Value
Snapshots analyzed 15 daily snapshots (2026-07-01 → 2026-07-21)
Runs audited 15 runs (all success)
Analysis window 21 days

Spend Profile

Metric 7-day window Full 21-day history
Runs 6 15
Total AIC 309.3 1 466.2
Avg AIC / run 51.5 97.7
Total tokens ~957k ~4.1M
Avg tokens / run ~159 500 ~273 400
Avg turns / run 6.5 10.1
Max turns (single run) 33 (2026-07-06)
Errors / warnings 0 / 0 0 / 0

Trend: A previous optimization (2026-07-13) dramatically reduced turn counts from 9–33 down to 5–9. The workflow now completes reliably, but further structural savings are available.


Ranked Recommendations

1 · Pre-compute comic selection inside the setup steps: block

Estimated savings: ~10–15 AIC/run (~20%)

The current setup step fetches only the latest comic number, leaving the agent to:

  1. Read latest_num.txt (1 tool call)
  2. Optionally decide to check the latest comic (1 web_fetch)
  3. Compute date +%j mod list-size to pick a curated number (1–2 tool calls)
  4. Fetch the selected comic JSON (1 web_fetch)

These 4–5 agent turns could run in the pre-step at near-zero marginal cost (bash only). Each saved agent turn eliminates not just the tool call itself but also its accumulated context overhead (the conversation grows quadratically).

Concrete action: Extend the existing steps: block to:

steps:
  - name: Fetch latest XKCD and pre-select today's comic
    run: |
      set -euo pipefail
      mkdir -p /tmp/gh-aw/agent/xkcd

      # Latest comic
      curl -sSf "(xkcd.com/redacted) -o /tmp/gh-aw/agent/xkcd/latest.json
      LATEST=$(jq -r '.num' /tmp/gh-aw/agent/xkcd/latest.json)
      echo "$LATEST" > /tmp/gh-aw/agent/xkcd/latest_num.txt

      # Pre-select a curated comic by day-of-year rotation
      CURATED="303 327 353 386 456 519 664 705 722 737 844 859 936 1068 1168 1205 1319 1425 1443 1537 1739 55 135 174 687 712 715 881 882 1236 1261 1379 1478 1754 2048 2100 1838 1875 2050 2173 2347 2440 2501 2545 2581 2674 2746 2785 2860 2916 2925"
      COUNT=$(echo $CURATED | wc -w)
      DOY=$(date +%j)
      IDX=$(( DOY % COUNT ))
      SELECTED=$(echo $CURATED | tr ' ' '\n' | sed -n "$((IDX + 1))p")
      echo "$SELECTED" > /tmp/gh-aw/agent/xkcd/selected_num.txt

      # Pre-fetch selected comic JSON
      curl -sSf "(xkcd.com/redacted) -o /tmp/gh-aw/agent/xkcd/selected.json
      echo "Pre-selected comic #$SELECTED for today (day $DOY of year)"

Then update the Comic Selection prompt section to:

The setup step has already:
- Fetched the latest comic to `/tmp/gh-aw/agent/xkcd/latest.json`
- Pre-selected today's curated comic and written its number to `/tmp/gh-aw/agent/xkcd/selected_num.txt`
- Pre-fetched the selected comic JSON to `/tmp/gh-aw/agent/xkcd/selected.json`

Read both files. If the latest comic is relevant (title/alt touches code, math, ML, AI, debugging, or tech culture), use it. Otherwise use the pre-selected comic from `selected.json`. If that comic is also irrelevant, pick the adjacent number from the list (max 1 additional `web_fetch`).

Evidence: Across 15 runs, the agent consistently spends 2–4 turns on the selection + fetch loop. The 2026-07-06 outlier reached 33 turns, likely from a relevance-retry spiral that could not have occurred if the pre-step had already resolved the selection.


2 · Remove expr from the allowed bash tools

Estimated savings: negligible AIC, but cleaner tool surface

expr is listed as an allowed bash tool alongside date and bash. Modern bash arithmetic ($(( ))) handles all the same operations without a subprocess. No run was observed using expr — it is dead configuration.

Concrete action: Remove expr from the tools.bash list in the workflow frontmatter:

tools:
  web-fetch:
  bash:
    - "curl"
    - "jq"
    - "bash"
    - "echo"
    - "date"
    # remove: "expr"

3 · Trim the curated comic list from the prompt body

Estimated savings: ~3–5 AIC/run (~6–8%)

The prompt embeds 50 comic numbers across three labeled categories. This block appears verbatim in every turn's context (as part of conversation history), adding ~600–800 prompt tokens per turn. At 6.5 turns/run that is ~4 000–5 000 extra tokens per run consumed entirely in context repetition.

Concrete action: After moving list rotation into the setup step (Rec. 1), the prompt no longer needs the full list. Replace the three category blocks with a single fallback line:

The full curated list is embedded in the setup step. For a fallback (if all pre-selected options fail), try comic #1205, #2347, or #936.

Evidence: The curated list section is ~350 characters (~90 tokens). Across 6.5 turns × 6 runs/week = ~39 context inclusions per week of unchanged list content.


Caveats

  • Turn counts for runs prior to 2026-07-10 reflect pre-optimization behavior; recent steady-state is 5–9 turns.
  • Cache efficiency is not broken down in available snapshot data; actual token savings may be partially offset by reduced cache hit rates if context structure changes significantly.
  • The 2026-07-06 outlier (33 turns, 328 AIC) is a single data point; its root cause (likely a relevance-retry loop) is inferred, not confirmed from logs.
  • All 6 recent runs completed successfully — no reliability risk is evident.
Full run history (15 snapshots)
Date AIC Tokens Turns Run
2026-07-01 137.5 380 975 15 §28510978928
2026-07-02 170.5 479 254 16 §28582320608
2026-07-03 102.9 275 286 9 §28653719659
2026-07-06 328.4 923 611 33 §28785726911
2026-07-07 93.9 238 597 8 §28858698838
2026-07-08 86.4 230 218 9 §28933933909
2026-07-09 104.0 274 623 9 §29010948440
2026-07-10 57.8 160 452 6 §29085684331
2026-07-13 61.0 180 230 7 §29242060714
2026-07-14 64.6 189 692 9 §29322817156
2026-07-15 51.0 152 192 6 §29405639243
2026-07-16 52.0 153 795 6 §29488563805
2026-07-17 62.3 181 683 7 §29571043582
2026-07-20 42.7 127 037 5 §29734221047
2026-07-21 51.3 152 765 6 §29820403487

References: §29820403487 · §29734221047 · §28785726911

Generated by Agentic Workflow AIC Usage Optimizer · 259.2 AIC · ⊞ 21.6K ·

  • expires on Jul 28, 2026, 3:12 PM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    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