Files

73 lines
3.5 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# plan-cost.sh — estimate the *agentic* cost of implementing a plan, and compare
# continuing THIS conversation vs starting a NEW one. Wraps `appe estimate-agent`
# (APPE's empirical agent-cost model, fitted to real Claude Code runs), so the
# `plan` skill can put a grounded dollar figure — and the continue-vs-new call —
# in the plan frontmatter instead of a vibe.
#
# Prints shell-eval-friendly KEY=value lines + a human hint:
# TURNS the turn count used (from --turns or the typology)
# TYPOLOGY the task typology
# EST_COST_NEW $ cost on Opus if run in a NEW conversation
# EST_COST_CONT $ cost on Opus if CONTINUING the current one
# EST_TOKENS total tokens (new-conversation run)
# CHEAPEST_MODEL cheapest capable model + its cost (for the "which model" note)
# RECOMMENDATION new | continue — whichever is cheaper (usually new)
#
# Best-effort: if the appe CLI isn't built, prints WHY and exits 0 (never fails
# the caller — the skill falls back to a hand estimate).
#
# Usage (from this skill's dir):
# bash scripts/plan-cost.sh --turns 120 [--typology feature] [--existing-context 120000]
# bash scripts/plan-cost.sh --typology research # turns from the typology median
set -uo pipefail
# Locate the built appe CLI (canonical checkout; overridable with $APPE_CLI).
APPE_CLI="${APPE_CLI:-$HOME/projects/appe/packages/cli/dist/appe.js}"
if [[ ! -f "$APPE_CLI" ]]; then
printf '# plan-cost: appe CLI not found at %s\n' "$APPE_CLI"
printf '# build it: (cd ~/projects/appe && npm run build:cli), then re-run.\n'
printf '# Falling back to a hand estimate for this plan.\n'
exit 0
fi
# Forward every arg to `appe estimate-agent --continue --json`, then extract the
# numbers with node (already a dependency of the CLI). Default existing-context
# to 120k tok (a mid-size in-progress conversation) unless the caller overrode it.
have_existing=0
for a in "$@"; do [[ "$a" == "--existing-context" ]] && have_existing=1; done
extra=()
[[ $have_existing -eq 0 ]] && extra=(--existing-context 120000)
JSON="$(node "$APPE_CLI" estimate-agent --continue --json "$@" "${extra[@]}" 2>/dev/null)" || {
printf '# plan-cost: appe estimate-agent failed — falling back to a hand estimate.\n'
exit 0
}
node -e '
let d; try { d = JSON.parse(process.argv[1]); } catch { process.exit(0); }
const h = d.headline || {};
const c = d.compareContinueVsNew || {};
const cfg = d.config || {};
const results = d.results || [];
const cheapest = results[0];
const money = (n) => n == null ? "?" : (n < 1 ? `$${n.toFixed(2)}` : `$${n.toFixed(2)}`);
const costNew = c.new ? c.new.cost : h.cost;
const costCont = c.continue ? c.continue.cost : h.cost;
const rec = (costCont != null && costNew != null && costCont > costNew) ? "new" : "continue";
const p = (k, v) => console.log(`${k}=${v}`);
p("TURNS", cfg.turns ?? "?");
p("EST_COST_NEW", money(costNew));
p("EST_COST_CONT", money(costCont));
p("EST_TOKENS", (h.tokens && h.tokens.total) ? h.tokens.total : "?");
if (cheapest) p("CHEAPEST_MODEL", `${cheapest.name} (${money(cheapest.cost)})`);
p("RECOMMENDATION", rec);
if (c.deltaCost != null && costNew) {
const pct = Math.round((c.deltaCost / costNew) * 100);
console.log(`# Continuing this conversation costs ${money(Math.abs(c.deltaCost))} ${c.deltaCost>=0?"more":"less"} (${pct>=0?"+":""}${pct}%): the whole transcript is re-read every turn.`);
}
console.log(`# Headline is Opus 4.8; cheaper models re-scale the bill (see the CLI table without --json).`);
' "$JSON"
exit 0