Skip to content

Conversation

@oai-coding-agent
Copy link
Contributor

Summary
Add UsageEvent to internal agent events and map ResponseCompletedEvent from SDK to this new event so downstream consumers can track token usage.

Implementation Details

  • Defined UsageEvent dataclass and added it to AgentEvent union.
  • Imported RawResponsesStreamEvent and ResponseCompletedEvent and updated map_sdk_event_to_agent_event to emit UsageEvent with input, output, and total token counts, including a null-check on usage.
  • Rendering logic unchanged to continue ignoring UsageEvent.

Assumptions

  • When ResponseCompletedEvent.response.usage is not present, no UsageEvent is emitted.
  • Tests bypass Pydantic model validation via construct(), so actual ParsedResponse model instantiation is not required.

Testing
Added a unit test in tests/agent/test_events.py:

  • Constructs a ResponseCompletedEvent via construct() with sample usage counts.
  • Verifies mapping to UsageEvent with correct token counts.
    All linting, type checks, and tests pass.

Considerations
Alternative approaches (e.g., manually constructing Pydantic models or using model_construct()) were considered; .construct() was chosen for simplicity in tests.

- Define UsageEvent dataclass and add to AgentEvent union
- Import RawResponsesStreamEvent and ResponseCompletedEvent
- Map RawResponsesStreamEvent carrying ResponseCompletedEvent to UsageEvent in mapping function
- Add null-check for optional usage to satisfy type checking
- Import RawResponsesStreamEvent, ResponseCompletedEvent, UsageEvent in tests
- Update test to construct ResponseCompletedEvent with construct() to bypass validation
- Add test_map_response_completed_event_to_usage_event validating UsageEvent fields
@oai-coding-agent oai-coding-agent bot added the oai label Jun 30, 2025
@mattmorgis mattmorgis merged commit fd3cc99 into main Jun 30, 2025
1 check passed
@mattmorgis mattmorgis deleted the oai/issue-80 branch June 30, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants