Skip to content

Conversation

@r488it
Copy link

@r488it r488it commented Jun 13, 2025

Summary

Modified the ResultMessage class to make cost_usd and total_cost_usd fields optional, preventing errors when API responses don't include cost information.

Changes Made

  • Changed cost_usd field from float to float | None = None in ResultMessage class
  • Changed total_cost_usd field from float to float | None = None in ResultMessage class
  • Reordered fields to place optional fields at the end of the dataclass

Problem Solved

  • API responses sometimes don't include cost information (cost_usd, total_cost)
  • Current implementation has these fields commented out in client.py, which could cause initialization errors for ResultMessage
  • Need to handle messages reliably regardless of whether cost information is available

Files Modified

  • claude_code_sdk/types.py: Updated ResultMessage dataclass definition
  • claude_code_sdk/_internal/client.py: Maintained consistency with existing commented-out cost fields

Backward Compatibility

  • Existing code using cost information will need to add None checks
  • New implementation returns None when cost information is unavailable

Testing

  • Verified functionality with responses containing cost information
  • Verified functionality with responses missing cost information
  • Confirmed existing test cases still pass

Related Issue

This change improves the robustness of the SDK when handling different API response formats and prevents potential runtime errors due to missing cost data.

@musab-mk
Copy link

I got this on new Claude CLI update:

• SDK: Renamed total_cost to total_cost_usd


It might be related to that.

@106-
Copy link

106- commented Jun 14, 2025

As noted in the CHANGELOG.md, you can replace total_cost with total_cost_usd instead of just commenting it out.
I ran the actual command this library calls internally (tested with version 1.0.24), and got the following result:

$ claude --output-format stream-json --verbose --print "What is 2 + 2?"
...
{
  "type": "result",
  "subtype": "success",
  "is_error": false,
  "duration_ms": 2905,
  "duration_api_ms": 2702,
  "num_turns": 1,
  "result": "4",
  "session_id": "xxx",
  "total_cost_usd": 0.04995525000000001,
  "usage": {
    "input_tokens": 3,
    "cache_creation_input_tokens": 13291,
    "cache_read_input_tokens": 0,
    "output_tokens": 7,
    "server_tool_use": {
      "web_search_requests": 0
    }
  }
}

As you can see, total_cost_usd is indeed present in the response.

@ltawfik
Copy link
Collaborator

ltawfik commented Jun 18, 2025

thank you @r488it, this is fixed with the latest release, pip install --upgrade claude-code-sdk

@ltawfik ltawfik closed this Jun 18, 2025
golfballnut pushed a commit to golfballnut/claude-agent-sdk-python that referenced this pull request Oct 29, 2025
…cking, webhooks

Fixed 3 critical bugs causing 100% enrichment failure rate:

Bug anthropics#1: Contact Aggregation Logic
- Changed from REPLACE to MERGE strategy for fallback contacts
- Agent 2.1 (LinkedIn) and 2.2 (Perplexity) now accumulate contacts
- Lowered threshold from 2+ to 1+ contacts (proceed if ANY found)
- Only raises exception if 0 contacts from all sources
- Impact: Beach Buddy (1 contact) and Bear Lake (1 contact) now succeed

Bug anthropics#2: Missing course_id in Failures
- Added course_id to exception handler result dict
- Enables webhook firing even on enrichment failures
- Allows error tracking in ClickUp and database status updates
- Impact: All courses now tracked, even partial failures

Bug anthropics#3: Incorrect Cost Tracking
- Fixed Agent 1 cost reading from wrong variable
- Was: course_data.get("cost") (Agent 2's cost)
- Now: url_result.get("cost") (Agent 1's cost)
- Impact: Accurate budget tracking per agent

Tested against production logs showing 3 failed courses.
Expected: All should now complete with 1+ contacts.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
ollie-anthropic pushed a commit that referenced this pull request Dec 9, 2025
- Add SubagentExecutionConfig for controlling parallel vs sequential
  subagent execution when multiple Task tools are invoked in same turn
- Add execution_mode parameter to AgentDefinition for per-agent control
- Add subagent_execution field to ClaudeAgentOptions for global config
- Improve HookMatcher docstring with comprehensive examples including
  MCP tool matching patterns (mcp__server__tool format)
- Add new type aliases: SubagentExecutionMode, MultiInvocationMode,
  SubagentErrorHandling
- Add comprehensive tests for all new types

Addresses issues #2 (Critical: subagent execution mode) and #3 (Medium:
HookMatcher documentation).
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.

4 participants