feat(soniox): report output tokens in RECOGNITION_USAGE for token-based billing#6435
Open
Panmax wants to merge 1 commit into
Open
feat(soniox): report output tokens in RECOGNITION_USAGE for token-based billing#6435Panmax wants to merge 1 commit into
Panmax wants to merge 1 commit into
Conversation
…ed billing Soniox bills real-time STT by tokens: input audio tokens (driven by stream duration) plus output text tokens (each element of the `tokens` array in a WebSocket response is one billable output token). The plugin previously reported only `audio_duration`, leaving `RecognitionUsage.output_tokens` (and therefore `STTMetrics.output_tokens`) always 0. Count final transcription tokens as they arrive and report the increment alongside the audio duration in each RECOGNITION_USAGE event: - only `is_final` tokens are counted (non-final tokens are re-sent as final later, so counting them would double-bill) - `<end>`/`<fin>` control markers are excluded - in translation mode both original and translated tokens count, as both are billable provider output - counters reset on reconnect, mirroring `_reported_duration_ms` Input audio tokens are intentionally not reported: the WebSocket API exposes no input token count, and `audio_duration` already carries the billing-relevant quantity without resorting to estimation. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Soniox bills real-time STT by tokens: input audio tokens (driven by stream duration) plus output text tokens — each element of the
tokensarray in a WebSocket response is one billable output token (pricing).The plugin previously reported only
audio_durationinRECOGNITION_USAGEevents, soRecognitionUsage.output_tokens/STTMetrics.output_tokens(which the framework already supports) were always 0, making it impossible to track token-based costs from metrics.This PR counts final transcription tokens as they arrive and reports the increment alongside the audio duration in each
RECOGNITION_USAGEevent.Counting rules
is_finaltokens are counted — non-final tokens are re-sent as final later, so counting them would double-bill.<end>/<fin>control markers are excluded (not transcription output)._reported_duration_msbehavior.Input audio tokens are intentionally not reported: the WebSocket API exposes no input token count, and
audio_durationalready carries the billing-relevant quantity (Soniox charges input tokens for the full stream duration) without resorting to estimation.Testing
Added 4 unit tests to
tests/test_plugin_soniox_stt.pyfollowing the existing fake-WebSocket style:<end>excludedpytest tests/test_plugin_soniox_stt.py --plugin soniox: 28 passed (24 existing + 4 new).ruff format --check,ruff check, and strictmypyall pass.🤖 Generated with Claude Code