Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/claude_code_sdk/_internal/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@ def _parse_message(self, data: dict[str, Any]) -> Message | None:

case "result":
# Map total_cost to total_cost_usd for consistency
# Handle missing cost_usd for Max subscription users
return ResultMessage(
subtype=data["subtype"],
cost_usd=data["cost_usd"],
cost_usd=data.get("cost_usd", None),
duration_ms=data["duration_ms"],
duration_api_ms=data["duration_api_ms"],
is_error=data["is_error"],
num_turns=data["num_turns"],
session_id=data["session_id"],
total_cost_usd=data["total_cost"],
total_cost_usd=data.get("total_cost", None),
usage=data.get("usage"),
result=data.get("result"),
)
Expand Down
4 changes: 2 additions & 2 deletions src/claude_code_sdk/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ class ResultMessage:
"""Result message with cost and usage information."""

subtype: str
cost_usd: float
cost_usd: float | None
duration_ms: int
duration_api_ms: int
is_error: bool
num_turns: int
session_id: str
total_cost_usd: float
total_cost_usd: float | None
usage: dict[str, Any] | None = None
result: str | None = None

Expand Down
25 changes: 25 additions & 0 deletions test_fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3
"""Test script to verify the cost_usd fix works."""

import anyio
from claude_code_sdk import process_query, ClaudeCodeOptions

async def main():
"""Test the SDK with a simple query."""
print("Testing claude-code-sdk with cost_usd fix...")

try:
# Simple test query
async for message in process_query(
"What is 2+2?",
ClaudeCodeOptions()
):
print(f"Message type: {type(message).__name__}")
print(f"Message: {message}")
print("-" * 40)
except Exception as e:
print(f"Error: {type(e).__name__}: {e}")
raise

if __name__ == "__main__":
anyio.run(main)