fix: Preserve Anthropic cost when session ends with error_during_execution#1105
Conversation
Adding CLAUDE.md with task information for AI processing. This file will be removed when the task is complete. Issue: #1104
…ution When Claude Code emits multiple result events (success followed by error_during_execution), the cost from the error result ($0) was overwriting the valid cost from the success result. Changes: - Added anthropicTotalCostUSD to all failure return paths in executeClaudeCommand - Changed cost capture logic to keep maximum non-zero value - Skip zero/negative cost updates to preserve valid earlier costs - Added detailed case study documenting root cause analysis Closes #1104 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This reverts commit 75b87d2.
|
|
Why not get cost only from subtype |
|
🤖 AI Work Session Started Starting automated work session at 2026-01-11T04:17:53.111Z The PR has been converted to draft mode while work is in progress. This comment marks the beginning of an AI work session. Please wait working session to finish, and provide your feedback. |
Changed cost capture logic to only extract total_cost_usd from result events with subtype === 'success'. This is more explicit and reliable than the previous approach of keeping maximum non-zero cost. Per PR review feedback: error_during_execution results have zero cost, so we should explicitly only trust the success subtype for cost data. Fixes #1104 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
- Add ESLint max-lines rule (1500 lines) to match CI workflow check - Extract handleClaudeRuntimeSwitch to claude.runtime-switch.lib.mjs - Reduce claude.lib.mjs from 1506 to 1354 lines - Add case study documentation for issue #1141 The CI failure was caused by a PR merge race condition where PR #1105 passed validation against an older base branch but caused failures when merged with newer changes on main. This fix: 1. Adds ESLint enforcement for the 1500-line limit, providing local development feedback before CI runs 2. Extracts the runtime switch module to keep claude.lib.mjs maintainable 3. Documents the root cause and prevention strategies Closes #1141 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary
Fixes a bug where the cost calculated by Anthropic (
total_cost_usd) was reported as$0.00when a Claude Code session ended with anerror_during_executionresult, even though valid cost data was available from an earlier success result event.Root Cause
When Claude Code emits multiple result events in a session (a success result followed by an error_during_execution result):
$3.83) withsubtype: "success"$0.00) withsubtype: "error_during_execution"anthropicTotalCostUSDentirelyChanges
src/claude.lib.mjs:anthropicTotalCostUSDto all 4 failure return paths inexecuteClaudeCommandsubtype === 'success'resultsDocumentation:
docs/case-studies/issue-1104/Test Plan
error_during_executionerrors$0.00References
Closes #1104
Generated with Claude Code