Fix streaming token usage tracking in OpenAI provider (Issue #4056) #4057
+168
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes issue #4056 where token usage always returns 0 when using async streaming crew kickoff.
Root cause: The streaming completion methods (
_handle_streaming_completionand_ahandle_streaming_completion) inOpenAICompletionnever called_track_token_usage_internal(), unlike the non-streaming methods which correctly track usage.Changes:
stream_options={"include_usage": True}to streaming params so OpenAI API returns usage information in the final chunkfinal_completionin response_model streaming paths_extract_chunk_token_usagemethod forChatCompletionChunkobjectsReview & Testing Checklist for Human
stream=Trueand verifycrew.token_usage,streaming.result.token_usage, andagent._token_process.get_summary()return non-zero values. The unit tests use mocks and don't verify actual API behavior.stream_options={"include_usage": True}works with the installed OpenAI SDK version (~1.83.0). This is a relatively recent feature.get_final_completion(). Verify usage tracking is correct here.Notes