Skip to content

fix: Preserve Anthropic cost when session ends with error_during_execution#1105

Merged
konard merged 5 commits into
mainfrom
issue-1104-a5cdc3ae300e
Jan 19, 2026
Merged

fix: Preserve Anthropic cost when session ends with error_during_execution#1105
konard merged 5 commits into
mainfrom
issue-1104-a5cdc3ae300e

Conversation

@konard

@konard konard commented Jan 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes a bug where the cost calculated by Anthropic (total_cost_usd) was reported as $0.00 when a Claude Code session ended with an error_during_execution result, 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):

  1. The success result contains the actual cost (e.g., $3.83) with subtype: "success"
  2. The error result contains zero cost ($0.00) with subtype: "error_during_execution"
  3. The code was overwriting the valid cost with the zero value
  4. The failure return path was missing anthropicTotalCostUSD entirely

Changes

src/claude.lib.mjs:

  • Added anthropicTotalCostUSD to all 4 failure return paths in executeClaudeCommand
  • Changed cost capture logic to only extract from subtype === 'success' results
  • This is explicit and reliable - we only trust cost data from successful results
  • Added logging for ignored cost values from non-success results

Documentation:

  • Created comprehensive case study in docs/case-studies/issue-1104/
  • Downloaded and preserved both session logs for future reference
  • Documented timeline, root cause analysis, and solutions considered

Test Plan

  • Verify fix by running a session that encounters error_during_execution errors
  • Check that PR comments show correct Anthropic cost instead of $0.00
  • Verify verbose logs show appropriate cost capture/skip messages

References

Closes #1104


Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #1104
@konard konard self-assigned this Jan 11, 2026
…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>
@konard konard changed the title [WIP] Price calculated by Anthropic was not extracted from json stream output fix: Preserve Anthropic cost when session ends with error_during_execution Jan 11, 2026
@konard konard marked this pull request as ready for review January 11, 2026 03:42
@konard

konard commented Jan 11, 2026

Copy link
Copy Markdown
Contributor Author

⚠️ Solution Draft Finished with Errors

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $5.134010 USD
  • Calculated by Anthropic: $0.000000 USD
  • Difference: $-5.134010 (-100.00%)

Note: The session encountered errors during execution, but some work may have been completed. Please review the changes carefully.

📎 Log file uploaded as Gist (915KB)
🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard

konard commented Jan 11, 2026

Copy link
Copy Markdown
Contributor Author

Why not get cost only from subtype success? So it will be explicit and reliable? Double check original issue, I gave you more examples of pull requests.

@konard konard marked this pull request as draft January 11, 2026 04:17
@konard

konard commented Jan 11, 2026

Copy link
Copy Markdown
Contributor Author

🤖 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.

konard and others added 2 commits January 11, 2026 05:21
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>
@konard konard marked this pull request as ready for review January 11, 2026 04:34
@konard

konard commented Jan 11, 2026

Copy link
Copy Markdown
Contributor Author

⚠️ Solution Draft Finished with Errors

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $2.359019 USD
  • Calculated by Anthropic: $0.000000 USD
  • Difference: $-2.359019 (-100.00%)

Note: The session encountered errors during execution, but some work may have been completed. Please review the changes carefully.

📎 Log file uploaded as Gist (3175KB)
🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard konard merged commit 593aa64 into main Jan 19, 2026
20 checks passed
konard added a commit that referenced this pull request Jan 19, 2026
- 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Price calculated by Anthropic was not extracted from json stream output

1 participant