Bug: Missing null validation for cost_metadata in CostTracker.record_usage #10549

Open
opened 2026-04-18 17:08:53 +00:00 by HAL9000 · 0 comments
Owner

Metadata

Commit: HEAD (current working tree)
Branch: main

Background and Context

The record_usage() method in src/cleveragents/providers/cost_tracker.py does not validate that the cost_metadata parameter is not None before calling methods on it. This violates defensive programming practices and results in unclear error messages when the method is called with invalid input.

Currently, if a caller passes cost_metadata=None, the method will crash with an AttributeError instead of raising a clear ValueError with a descriptive message.

Expected Behavior

The record_usage() method should:

  1. Validate that cost_metadata is not None at the start of the method
  2. Raise a clear ValueError with a descriptive message if validation fails
  3. Allow the method to proceed safely if validation passes

Actual Behavior

When cost_metadata=None is passed to record_usage(), the method crashes with:

AttributeError: 'NoneType' object has no attribute 'record_usage'

This occurs at the first method call on the cost_metadata parameter:

cost_metadata.record_usage(
    input_tokens=input_tokens,
    output_tokens=output_tokens,
    cost=cost,
    provider=provider,
)

Acceptance Criteria

  • record_usage() validates that cost_metadata is not None
  • A ValueError is raised with message: "cost_metadata cannot be None"
  • The validation occurs before any method calls on cost_metadata
  • Existing tests pass with the new validation in place
  • New unit test covers the None case and verifies the ValueError is raised

Subtasks

  • Add null validation check at the start of record_usage() method
  • Raise ValueError with descriptive message if validation fails
  • Write unit test for None input case
  • Verify all existing tests still pass
  • Update method docstring if needed to document the validation

Definition of Done

This issue is complete when:

  1. The record_usage() method validates cost_metadata is not None
  2. A clear ValueError is raised if validation fails
  3. Unit tests confirm the validation works correctly
  4. All existing tests continue to pass
  5. Code review approved

Automated by CleverAgents Bot
Agent: new-issue-creator

## Metadata **Commit:** HEAD (current working tree) **Branch:** main ## Background and Context The `record_usage()` method in `src/cleveragents/providers/cost_tracker.py` does not validate that the `cost_metadata` parameter is not None before calling methods on it. This violates defensive programming practices and results in unclear error messages when the method is called with invalid input. Currently, if a caller passes `cost_metadata=None`, the method will crash with an AttributeError instead of raising a clear ValueError with a descriptive message. ## Expected Behavior The `record_usage()` method should: 1. Validate that `cost_metadata` is not None at the start of the method 2. Raise a clear `ValueError` with a descriptive message if validation fails 3. Allow the method to proceed safely if validation passes ## Actual Behavior When `cost_metadata=None` is passed to `record_usage()`, the method crashes with: ``` AttributeError: 'NoneType' object has no attribute 'record_usage' ``` This occurs at the first method call on the `cost_metadata` parameter: ```python cost_metadata.record_usage( input_tokens=input_tokens, output_tokens=output_tokens, cost=cost, provider=provider, ) ``` ## Acceptance Criteria - [ ] `record_usage()` validates that `cost_metadata` is not None - [ ] A `ValueError` is raised with message: "cost_metadata cannot be None" - [ ] The validation occurs before any method calls on `cost_metadata` - [ ] Existing tests pass with the new validation in place - [ ] New unit test covers the None case and verifies the ValueError is raised ## Subtasks - [ ] Add null validation check at the start of `record_usage()` method - [ ] Raise `ValueError` with descriptive message if validation fails - [ ] Write unit test for None input case - [ ] Verify all existing tests still pass - [ ] Update method docstring if needed to document the validation ## Definition of Done This issue is complete when: 1. The `record_usage()` method validates `cost_metadata` is not None 2. A clear `ValueError` is raised if validation fails 3. Unit tests confirm the validation works correctly 4. All existing tests continue to pass 5. Code review approved --- **Automated by CleverAgents Bot** Agent: new-issue-creator
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#10549
No description provided.