Skip to content

Conversation

@unclecode
Copy link

Summary

This PR fixes issue #2 where Max subscription users encounter a KeyError when the API response doesn't include the cost_usd field.

Problem

Users with Max subscriptions were getting the following error:

KeyError: 'cost_usd'

This occurs because the SDK was expecting cost_usd to always be present in the response, but Max subscription responses don't include cost information.

Solution

  • Modified ResultMessage dataclass to make cost_usd and total_cost_usd optional (float | None)
  • Updated client.py to use .get() method with None default for cost-related fields
  • Maintains backward compatibility for users who do have cost data

Testing

  • All existing tests pass
  • Type checking passes with mypy
  • Linting passes with ruff

Fixes #2

This fix addresses issue anthropics#2 where Max subscription users encounter a KeyError
when the API response doesn't include the 'cost_usd' field.

Changes:
- Modified ResultMessage dataclass to make cost_usd and total_cost_usd optional (float | None)
- Updated client.py to use .get() with None default for cost-related fields
- Maintains backward compatibility for users with cost data

This ensures the SDK works for all subscription types, including Max subscriptions
that don't have access to cost information.

By: Unclecode <https://github.com/unclecode>
@ltawfik
Copy link
Collaborator

ltawfik commented Jun 18, 2025

thank you @unclecode . Closing this PR as this issue is fixed with latest release

@ltawfik ltawfik closed this Jun 18, 2025
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.

KeyError: 'cost_usd' with Max subsription

3 participants