Skip to content

perf: debounce translation for growing segments (sub-issue 2/4 of #4651) #4713

Closed

Description

@beastoin

Parent Issue

Sub-issue of #4651 — Translate API cost optimization (target: 84–89% reduction)

Objective

Cost saving: The 0.6s processing loop re-translates growing segments 5–10x, billing the same characters repeatedly. This is the second-largest cost driver after detection double-billing.

Problem

stream_transcript_process() at transcribe.py:1414 fires every 0.6s. As Deepgram adds words, the same segment appears in updated_segments on every tick with slightly more text. The translate() function re-translates the full segment text each time:

T=0.0: "Hello"                              →  5 chars billed
T=0.6: "Hello how are you"                  → 17 chars billed (incl "Hello" again)
T=1.2: "Hello how are you doing today"      → 28 chars billed (incl everything again)
Total: 50 chars billed for 28 chars of final text (1.8x)

For segments with 10 updates: ~5.5x overbilling.

Changes Required

1. Add per-segment debounce tracking

In transcribe.py, alongside the existing translate() function:

pending_translations = {}  # segment_id → {text, hash, updated_at, task}

2. Debounce logic

  • First appearance: translate immediately (zero UX delay)
  • Updates: cancel any pending debounce task, schedule new one with 0.8–1.2s trailing window
  • Final segment (STT marks complete): translate immediately, skip debounce
  • On fire: only translate if last_hash unchanged since task creation (prevents stale)

3. Segment versioning for out-of-order safety

  • Track version/hash per segment
  • If a slow translate response arrives after a newer update was already translated, discard the stale response
  • Prevents overwriting newer translations with older ones

Files to Modify

File Change
backend/routers/transcribe.py Add debounce queue logic in/around translate() function

Test Plan

  • First segment appearance translates immediately (no delay)
  • Rapid updates within 1s don't trigger multiple translate calls
  • Final/stable segment translates promptly after debounce window
  • Out-of-order responses don't overwrite newer translations
  • Translation still appears in real-time for users (UX not degraded)

Acceptance Criteria

  • Growing segments translated 1–2x instead of 5–10x
  • First translation appears immediately
  • No stale translation overwrites

Dependencies

  • Should be implemented AFTER sub-issue 1 (detect elimination) since translate response shape changes

Sub-issue 2/4 of #4651. Second priority — 60–80% reduction in translation characters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    intelligencemaintainerp1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions