Skip to content

feat(soniox): report output tokens in RECOGNITION_USAGE for token-based billing#6435

Open
Panmax wants to merge 1 commit into
livekit:mainfrom
Panmax:soniox-stt-output-tokens
Open

feat(soniox): report output tokens in RECOGNITION_USAGE for token-based billing#6435
Panmax wants to merge 1 commit into
livekit:mainfrom
Panmax:soniox-stt-output-tokens

Conversation

@Panmax

@Panmax Panmax commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

What

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 (pricing).

The plugin previously reported only audio_duration in RECOGNITION_USAGE events, so RecognitionUsage.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_USAGE event.

Counting rules

  • 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 (not transcription output).
  • In translation mode both original and translated tokens count, as both are billable provider output.
  • Reporting is incremental and counters reset on reconnect, mirroring the existing _reported_duration_ms behavior.

Input audio tokens are intentionally not reported: the WebSocket API exposes no input token count, and audio_duration already 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.py following the existing fake-WebSocket style:

  • final tokens counted, <end> excluded
  • non-final tokens excluded (no double-billing)
  • incremental reporting across two endpoints
  • translation mode counts both sides

pytest tests/test_plugin_soniox_stt.py --plugin soniox: 28 passed (24 existing + 4 new). ruff format --check, ruff check, and strict mypy all pass.

🤖 Generated with Claude Code

…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>
@Panmax Panmax requested a review from a team as a code owner July 15, 2026 11:11

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant