refactor(openai): Split token counting by API for easier deprecation#5930
2 issues
Medium
Manual output token counting missing for non-streaming Responses API - `sentry_sdk/integrations/openai.py:291-295`
In _calculate_responses_token_usage, manual output token counting only handles streaming_message_responses but lacks a fallback for non-streaming responses. The completions version (line 220-223) has elif hasattr(response, 'choices') to extract output from non-streaming responses, but the responses version has no equivalent elif hasattr(response, 'output') branch. When the Responses API doesn't provide usage data and streaming_message_responses is None (non-streaming case at line 637), output tokens will incorrectly remain 0 instead of being manually counted.
Low
Missing manual output token counting for non-streaming Responses API without usage data - `sentry_sdk/integrations/openai.py:633-638`
In _calculate_responses_token_usage (lines 291-295), when output_tokens == 0 and streaming_message_responses is None, there's no fallback to manually count output tokens from response.output. The analogous function _calculate_completions_token_usage has this fallback (lines 220-223) which counts from response.choices. For non-streaming Responses API calls where the API doesn't return usage data and tiktoken is configured, output tokens won't be recorded.
4 skills analyzed
| Skill | Findings | Duration | Cost |
|---|---|---|---|
| code-review | 1 | 2m 26s | $1.46 |
| find-bugs | 1 | 4m 36s | $3.27 |
| skill-scanner | 0 | 2m 53s | $0.42 |
| security-review | 0 | 1m 58s | $0.77 |
Duration: 11m 53s · Tokens: 4.0M in / 37.8k out · Cost: $5.95 (+extraction: $0.01, +dedup: $0.00)