Summary
Each loop iteration makes API calls to Claude (or Codex), but there's no visibility into how many tokens are consumed or what the cost is. A long-running loop could become unexpectedly expensive.
Current Behavior
ralph.sh invokes claude or codex CLI with no cost instrumentation
- No per-iteration or cumulative cost summary
- No way to set a budget limit or cost alert
Proposal
- After each iteration, capture and log token usage (input/output tokens, cache hits)
- Maintain a running total in a structured file (e.g.,
usage.json or appended to progress log)
- Print a summary at the end of
ralph.sh (total tokens, estimated cost, iterations run)
- Optional: add a
--budget flag to ralph.sh that halts the loop if estimated cost exceeds the limit
- Optional: add a
--dry-run flag that estimates cost without executing
Example Output
Loop complete: 8 iterations
Total input tokens: 245,000
Total output tokens: 89,000
Estimated cost: $4.12
Features completed: 6/10
Considerations
- Token counting depends on the CLI tool exposing usage data — check what
claude and codex CLIs output
- Cost estimates need model-specific pricing (Opus vs Sonnet vs Haiku)
- Could integrate with Anthropic's usage API for accurate billing data
Summary
Each loop iteration makes API calls to Claude (or Codex), but there's no visibility into how many tokens are consumed or what the cost is. A long-running loop could become unexpectedly expensive.
Current Behavior
ralph.shinvokesclaudeorcodexCLI with no cost instrumentationProposal
usage.jsonor appended to progress log)ralph.sh(total tokens, estimated cost, iterations run)--budgetflag toralph.shthat halts the loop if estimated cost exceeds the limit--dry-runflag that estimates cost without executingExample Output
Considerations
claudeandcodexCLIs output