Work/Voice AI/Real-Time Voice Companion

Real-Time Voice Companion

Designing a voice agent for mental health that listens to how something is said

5
listening modules running in parallel
2
kinds of delay, each with its own target
30 days
to an internal test version, one testable thing per day
System
Design case study
Domain
Voice AI · System Design
Period
Oct 2026
Stack
Deepgram · ElevenLabs · PostgreSQL · Python · FastAPI
01

Overview

The system

A real-time voice companion for mental health and personal development: long, sensitive calls where pausing, interrupting and staying quiet all mean something.

What I did

Designed the architecture end to end (listening modules, state, policy, memory, crisis path, failure handling, phone side, data and cost), made the voice-stack decision, and planned the first 30 days. Then built the general version of the listening layer as an open-source project.

A design case study. The safety rules and the therapeutic content belong to a clinical research team; the rules shown are examples.

The way I see it, most voice agents today are a text bot with a microphone and a speaker attached. The user speaks, the system turns it into text, an LLM answers, and the answer is read out loud. Everything about how the user said it is lost: the tone, the pauses, how long they stayed quiet, the shake in their voice.

In a mental health companion, how something is said matters as much as what is said, so I built the design around that.

The short version: signals from the voice run in parallel with the transcript and go into one state. A policy in the middle decides what the agent does, and only then does the LLM speak. The risk level is read first, before anything else.

One boundary. I own the voice and communication system end to end. A research team owns the safety rules and the therapeutic content. I build the place where their work plugs in, and nothing goes to real users before their research is ready.

02

The Challenge

I started by imagining one call, second by second, and asking at every moment: who handles this?

  • The user pauses for three seconds. Are they done, or holding back tears?
  • Their voice shakes.
  • They talk over the agent.
  • They go quiet.

Every one of these is a design problem, and each one became a component or a decision in the architecture.

It also changes what "fast" means. In customer support, faster is always better. In a mental health call, talking too early is worse than talking late. If the user pauses and the agent jumps in after 600 ms, it feels bad. A calm pause of one second often feels caring.

Note

This is a design case study. The numbers below are design targets and estimates, not measurements. The general listening layer is built and open source (see the links).

03

Architecture

The server is a pipeline. The audio comes in over WebRTC and passes through five modules at the same time. They don't decide anything, they only report what they see into one state. The policy reads the state and decides what the agent does. Only when the policy says "speak" does the LLM write a reply, and the voice says it with a matching tone.

In
Voice
WebRTC from the phone
Listening modules · report only
Transcript
Words while the user is still talking
End of turn
Finished, or just thinking? Silence in ms, never reset
Emotion
From the voice, not the words. UNKNOWN when unsure
Style pattern
This user's pace and usual pause, saved over calls
Risk
One level, written into the state
Facts
State
Decides
Policy
risk firstwaitmmspeak
Only if told
LLM
Out
Voice
Style matches the user

The five modules run during the whole call. Memory sits next to the policy, not in the input pipeline.

04

My Contribution

  1. Designed the listening layer

    Five modules that run in parallel and only report, one state with facts only, and a rule-based policy that drives the agent.

  2. Split policy from prompt

    When and whether the agent speaks lives in code, how a sentence sounds lives in the prompt. The LLM can break a prompt rule, never a policy rule.

  3. Wired in the crisis path

    A risk module the policy checks before every other rule. The levels and scripts belong to the research team; I made sure nothing can skip it.

  4. Planned for failure

    A safe default for every part, with three rules: fail toward safe, never go silent without a reason, never pretend.

  5. Made the voice-stack decision

    A cascade first, behind one interface, with a fair bake-off against speech-to-speech and full duplex before committing to more.

  6. Built the general version

    The state, the policy, end of turn against the user's own pause, fast reactions and a scenario replay harness, open source as Beyond Words.

05

Technical Deep Dive

The state

The state is one snapshot of the call right now. It has facts only, no decisions, and it is updated many times a second.

{
  "call_minutes": 12,
  "last_transcript": "I just don't know if it's worth trying again",
  "end_of_turn": {"probability": 0.35},
  "silence_ms": 2400,
  "user_baseline_pause_ms": 1800,
  "emotions": [{"label": "sad", "confidence": 0.71}],
  "risk": "watch"
}

How I read it: he said something heavy, he is sad, and he has been silent for 2.4 seconds. His normal pause is 1.8 seconds, so this is a little longer than usual. The state does not say what to do with this. That is the job of the policy.

The policy

Rule-based code, so it is fast, the same every time, and testable. The first rule that matches wins. On the state above, rule 4 matches: the agent says a soft "mm" and stays quiet. No LLM call, so the reaction is fast and costs nothing.

IfThen
1risk is high or urgentstart the crisis path
2the user talks while the agent is speakingstop the agent right away
3silence is shorter than the user's usual pausewait
4silence is a bit longer than usual, and the last thing was heavysoft "mm", no LLM
5silence is much longer than usualgentle question: "Take your time. Want to say more?"
6the user is donecall the LLM, with guidance from the state

Example rules to show the idea. The real rules and numbers come from the research team.

ExamplesTarget
Fast reactionsa soft "mm", "aha", stopping when the user interrupts. No LLMunder 300 ms
Full repliespolicy, LLM, voiceabout 800–1200 ms after the policy decides

Two kinds of delay instead of one latency number.

What it holdsHow it is used
Profile factsfamily, work, likes, with date and sourceloaded at call start; the user can edit and delete
Episodesone short summary per callfound by topic and date
Goalsplans and goals, with a statusthe policy checks in on them
In-callrolling summary and the last few turnskeeps the prompt small
Raw transcriptsthe full text, with consentrebuild the summaries if they are wrong

Memory in layers. One summary rewritten again and again loses detail, and one wrong line can hurt every later call.

What the system does
Speech-to-text stalls"Sorry, I missed that. Can you say it again?"
End of turn failsa plain silence timer based on the user's usual pause
Emotion unsureUNKNOWN; the policy acts neutral
Risk module failsthe value becomes "watch", never lower
LLM slowa short "mm, I'm here", retry once or use a backup LLM
Memory downcontinue without it, and don't claim to remember
Network dropskeep the call state, reconnect, say "I'm back"

Failure handling: fail toward safe, never go silent without a reason, never pretend.

How do we build the voice stack?

D-01
Considered

Cascade (STT, LLM, voice), one speech-to-speech model, or a full-duplex model that listens and speaks at the same time.

Chosen

A cascade first, behind one interface. I hold the text before it is spoken, so every sentence can be checked, which is the strongest reason for mental health. The honest weakness: it may not reach "ultra realistic". It is the best baseline, not the final answer, and I move if another stack wins a fair test without losing safety, delay or cost.

06

Evaluation / Results

This is a design, so the result is a plan I can test, not a number. From day 1, every call is recorded and the delay of each step is logged, with the median and the slowest 5%, because users feel the slow cases. Test calls are replayed with one command, which turns opinions into decisions.

The general listening layer is built: Beyond Words has the state, a rules-file policy, end of turn measured against the caller's own pause, fast reactions from pre-rendered clips, and scenarios that replay call moments through the policy with a fake clock.

What is shipped, and how I test it
Day 1A call that runs locally, calls saved, delay of each step logged. Test: talk to it, read the log
Day 3Turn detection in the state and a first rules file. Test: pause for 3 s and it waits, interrupt and it stops
Day 4The user's usual pause and filler words. Test: talk slowly and it waits longer
Day 5Emotion in the state, UNKNOWN when unsure. Test: speak sadly and the state shows it
Day 7The risk module, checked first. Test: a test phrase switches to the placeholder script
Day 14Memory and cut calls. Test: hang up mid-call, call again, and the agent knows
Day 30Internal test version: crisis scenarios, delay and cost per call, first voice-stack decision

Each day ships one thing a person can test.

07

Engineering Lessons

L.01

A pause is a design decision, not a delay problem. Talking too early can be worse than talking late, so the policy can choose to wait.

L.02

The LLM can break a prompt rule, never a policy rule. Anything about when to speak or about safety belongs in code.

L.03

Fail toward safe. If a safety part breaks, act as if the risk is higher, not lower.

L.04

Measure each step, not one number. Users feel the slow cases, so I watch the slowest 5%, not only the median.

Next case study
Real-Time Voice AI
→