Agents Builders

Capture action tokens and cost

Archived
capture-action-tokens-and-cost-2

Created

Jul 01, 15:02

Started

Jul 01, 15:03

Completed

Jul 01, 15:46

DevOps handoff

Type

Feature

Shape

backend

Worktree Slug

capture-action-tokens-and-cost-2

Repositories

mcritchie-studio

Release Slug

Branch

feat/capture-action-tokens-and-cost-2

Local URL

QA URL

Production URL

tooling

Acceptance Criteria

  • Hook stamps tokens from transcript message usage
  • Span and session totals dedupe by turn
  • Cost computed from a configurable model rate
  • Missing usage leaves tokens and cost blank

Expected Test Plan

  • unit
  • integration

Checks Run

  • [unit] bin/rails test test/lib/atomic_capture_hook_test.rb (usage extraction from transcript line, turn_usage sums input+cache, build_payload stamps tokens+source_turn_uuid)
  • [unit] bin/rails test test/models/atomic_action_test.rb (cost_for prices from rate map, null-when-no-rate, strips [1m] tier suffix)
  • [unit] bin/rails test test/helpers/heartbeat_helper_test.rb (heartbeat_usage_totals dedupes by source_turn_uuid; event_totals inherits dedupe)
  • [integration] bin/rails test test/controllers/api/v1/atomic_actions_controller_test.rb (permits tokens+source_turn_uuid, derives cost, cost not caller-settable)
  • [integration] bin/rails test test/models/atomic_action_test.rb (capture derives cost, leaves NULL for unknown model, stores source_turn_uuid)
  • [full-suite] bin/full-suite-check capture-action-tokens-and-cost-2 — rails test 2355 runs 0 failures + rubocop clean @4fb1af4ac1a1
  • [full-suite@4fb1af4ac1a1e6e1063cf09ab82026e19f74b75c] bin/rails test green
  • [rubocop@4fb1af4ac1a1e6e1063cf09ab82026e19f74b75c] bin/rubocop clean

Agent Context

The heartbeat's TOKENS and COST columns are always '—' because the Claude Code PostToolUse payload carries NO usage data (same gap MODEL had — model was solved by reading the transcript JSONL tail in bin/atomic-capture-hook via resolve_model/read_model_from_transcript). Do the SAME for usage: from the assistant turn line in the transcript tail (the one that carries message.model), also read message.usage {input_tokens, output_tokens, cache_creation_input_tokens, cache_read_input_tokens} and the line's 'uuid' (the assistant turn id). Stamp AtomicAction.tokens_in (input+cache) + tokens_out (output) + a NEW nullable column source_turn_uuid (migration; best-effort, indexed). COST = (tokens_in*in_rate + tokens_out*out_rate) from a configurable model->rate map (a small Ruby constant or ENV/Studio::Enumeral); if no rate for the model, leave cost NULL (NEVER fabricate a price). PARALLEL-TOOL-CALL CAVEAT: one assistant turn can fire N tool calls (N actions), all sharing that turn's usage — so per-action stamping duplicates. The heartbeat SPAN + SESSION aggregation (heartbeat_helper heartbeat_event_totals + the session-total, and grade/counts if relevant) MUST dedupe by source_turn_uuid (sum usage over DISTINCT turns), else totals multi-count. Per-action drawer can show the turn's usage as-is. All best-effort + never raise (mirror the model path). Tests: unit for usage extraction from a sample transcript line + cost calc + null-when-no-rate; integration/helper for dedupe-by-turn span totals. Files: bin/atomic-capture-hook, app/models/atomic_action.rb, a migration, app/helpers/heartbeat_helper.rb (aggregation), maybe app/controllers/api/v1/atomic_actions_controller.rb (permit tokens/source_turn_uuid). Part of narrated-events epic — project_opsd_learning_layer, [[feedback_token_capture_fragility]].

Stage Timeline

Who handled each stage, the time it took (measured), and the model / tokens / cost reported (best-effort) — plus who's on it right now. means the agent didn't report that metric.

Sizing Avi · PO MEDIUM Dev MEDIUM Actual LARGE ≠ forecast
  1. Created Designed
    C Caterpie
    Caterpie
    Model
    Duration
    Tokens
    Cost
    Completed Jul 01, 15:02 · 7 days ago
    api
  2. Designed Building
    C Caterpie
    Caterpie
    Model
    claude-opus-4-8
    Duration
    1 minute
    Tokens
    2,350,455
    Cost
    ~$6.28
    Started Jul 01, 15:02
    Completed Jul 01, 15:03 · 7 days ago
    cli
  3. Building Submitted
    C Caterpie
    Caterpie
    Model
    claude-opus-4-8
    Duration
    26 minutes
    Tokens
    3,493,284
    Cost
    ~$2.23
    Started Jul 01, 15:03
    Completed Jul 01, 15:29 · 7 days ago
    cli
  4. Submitted Reviewed Review Activity
    C Carl
    Carl primary
    J Jasper
    Jasper light
    Model
    claude-opus-4-8
    Duration
    1 minute
    Tokens
    1,656,300
    Cost
    ~$1.10
    Started Jul 01, 15:29
    Completed Jul 01, 15:31 · 7 days ago
    cli
  5. Reviewed Assembled
    S Steffon
    Steffon
    Model
    claude-opus-4-8
    Duration
    under a minute
    Tokens
    835,422
    Cost
    ~$0.51
    Started Jul 01, 15:31
    Completed Jul 01, 15:31 · 7 days ago
  6. Assembled Shipped
    A Avi
    Avi
    Model
    claude-opus-4-8
    Duration
    14 minutes
    Tokens
    4,830,892
    Cost
    ~$3.12
    Started Jul 01, 15:31
    Completed Jul 01, 15:46 · 7 days ago
  7. Shipped Archived
    Model
    Duration
    2 days
    Tokens
    Cost
    Started Jul 01, 15:46
    Completed Jul 04, 03:23 · 4 days ago

Conversation

QA review feedback, agent handoffs, and follow-up notes for this task.

No task feedback has been recorded yet.

Sealed-bid sizing

Edit →

Alex (PM)

Avi (PO)

MEDIUM

Dev

MEDIUM

Actual

LARGE