Skip to content

Cost and token usage tracking #4

Description

@adamdaum

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions