AI-Powered Sleep Coach for CBTI Protocol Delivery

rest · Healthcare · 2024 · source Agentic: yes · promotional bias: med · source-backed · extracted with prompt 0.5.4 (schema v2)

Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.

Problem

Domain: Sleep coaching and wellness behavior change

Rest wanted to make CBTI-inspired sleep coaching more accessible for people struggling with insomnia-like sleep problems, while preserving trust, personalization, clinical boundaries, and daily adherence in a sensitive wellness domain.

Constraints:

Architecture

Rest evolved from a text chatbot into a voice-first AI sleep coach. Users log sleep every morning, then receive a short report and a personalized check-in. The coach uses OpenAI models for reasoning, Vapi for voice agent infrastructure, file-based RAG for sleep/CBTI knowledge, a custom memory process that extracts user facts from past conversations, and dynamic agenda generation that creates prioritized prompt snippets based on last night's sleep, program stage, recent data, and relevant memories. Weekly error analysis, Langfuse traces, domain-expert review, LLM-powered safety evals, and guardrails around medication/medical advice guide prompt, RAG, and memory improvements.

Diagram confidence: reconstructed (reconstructed from prose).

flowchart LR
  sleep-log-flow["sleep-log-flow"] --> dynamic-agenda-generator["dynamic-agenda-generator"]
  dynamic-agenda-generator --> coach-checkin-interface["coach-checkin-interface"]
  coach-checkin-interface --> openai-text-agent["openai-text-agent"]
  coach-checkin-interface --> vapi-voice-agent["vapi-voice-agent"]
  openai-text-agent --> file-based-rag-store["file-based-rag-store"]
  vapi-voice-agent --> file-based-rag-store
  openai-text-agent --> user-memory-store["user-memory-store"]
  vapi-voice-agent --> user-memory-store
  post-conversation-memory-extractor["post-conversation-memory-extractor"] --> user-memory-store
  user-memory-store --> dynamic-agenda-generator
  openai-text-agent --> langfuse-trace-and-eval-layer["langfuse-trace-and-eval-layer"]
  vapi-voice-agent --> langfuse-trace-and-eval-layer
  langfuse-trace-and-eval-layer --> weekly-error-analysis["weekly-error-analysis"]
Drag to pan · scroll or use +/− to zoom · reconstructed from the source's described component topology

Components

Component Role Interacts with
sleep-log-flow Daily structured sleep diary capturing bedtime, wake time, awakenings, and related sleep data. dynamic-agenda-generator, coach-checkin-interface
coach-checkin-interface User-facing daily check-in available through text or voice after sleep logging. openai-text-agent, vapi-voice-agent, dynamic-agenda-generator
openai-text-agent Text-based assistant stack for chat coaching. coach-checkin-interface, file-based-rag-store, user-memory-store
vapi-voice-agent Voice stack for speech-to-text, streaming conversation, text-to-speech, and OpenAI-backed LLM responses. coach-checkin-interface, file-based-rag-store, user-memory-store
file-based-rag-store Retrieval layer for general sleep, CBTI, and expert-codified knowledge removed from the system prompt. openai-text-agent, vapi-voice-agent
user-memory-store Stores extracted user context and recency metadata for future personalization. dynamic-agenda-generator, openai-text-agent, vapi-voice-agent, post-conversation-memory-extractor
post-conversation-memory-extractor Prompt-driven process that reviews conversations after they happen and stores relevant memories. user-memory-store
dynamic-agenda-generator Creates prioritized mini-prompts for the next check-in based on sleep data, program stage, and memories. sleep-log-flow, user-memory-store, coach-checkin-interface
weekly-error-analysis Human/domain-expert review process using traces, annotations, support feedback, and interviews to drive AI roadmap changes. langfuse-trace-and-eval-layer
langfuse-trace-and-eval-layer Trace sampling, annotation queues, and LLM-powered safety/boundary evaluations. weekly-error-analysis, openai-text-agent, vapi-voice-agent

Data flow

A user logs numeric sleep diary data after waking. Rest generates a short report, then prepares a tailored agenda before the check-in. The agenda process gathers last-night sleep data, previous-week context, program stage, and memories extracted from earlier conversations. In text mode, the agent runs through OpenAI's assistant stack; in voice mode, Vapi handles speech-to-text, streaming, and text-to-speech while still using OpenAI as the LLM provider. The agent can retrieve sleep/CBTI knowledge from file-based RAG, run the agenda-driven conversation, and after the conversation another prompt extracts memories for future sessions. Traces feed weekly annotation/error analysis and safety evals.

4 supporting quotes
“People come to the app every single day to go to just log their sleep. They keep a sleep diary.”transcript, stated
“the AI comes with an agenda every day and it's driving the conversation”transcript, stated
“we started working with this platform called V API which is a platform for building voice agents”transcript, stated
“Another prompt reviews the conversation after it happened and it extracts things that happened”transcript, stated

Why this architecture

Rest chose voice because trust, intimacy, and fast disclosure matter when users discuss sleep struggles and must change habits. It kept the initial architecture simple, using OpenAI Assistants and later Vapi rather than a fully custom LangGraph-style agent, because the team wanted to learn from users before over-engineering. RAG moved general CBTI knowledge out of an overgrown system prompt, memory provided continuity across the eight-week journey, and dynamic agendas let the coach start each day with program-aware goals rather than waiting for the user to ask.

4 supporting quotes
“voice is warmer than just text and it's more intimate in a way and that lets the users for on one hand open up more about something as private as sleep is the”transcript, stated
“a huge system prompt grew and grew and became a problem because of the size of the context”transcript, stated
“let's start simple and see what we learn and then we can then we can make our architecture more complex.”transcript, stated
“the user doesn't have to come to us with an intent. We can start the conversation as a coach.”transcript, stated

Models used: OpenAI GPT-4 · OpenAI Assistants · Vapi · Langfuse · Hamming · file-based RAG

Scale

Alternatives considered (roads not taken)

sleep audio content as the main product

Rejected because: Audio helped some users fall asleep, but the team saw a deeper problem around chronic sleep habits and CBTI-style protocol guidance.

“we started building rest as an sleep audio app with you original content”transcript, stated
“there actually is a better way that has like clinically researched and proven to work”transcript, stated

text-only chatbot

Rejected because: Users opened up more and had longer interactions with voice.

“you could continue in text a regular old fashioned chat or you could you could try voice.”transcript, stated
“users were opening up more and we're having longer interactions once we enable voice”transcript, stated

one huge system prompt

Rejected because: The context became too large and made it harder for the LLM to focus on the right knowledge at the right moment.

“a huge system prompt grew and grew and became a problem because of the size of the context”transcript, stated
“it was harder for the LLM to focus on the right thing at the right moment.”transcript, stated

fully custom LangGraph or memory architecture upfront

Rejected because: The team judged it premature before learning the exact problems from users and error analysis.

“Should we be building a huge complex architecture with Langraph to have

all of the control that we want over everything that happens in our”transcript, stated

“maybe, but clearly not right now. So that that will have to wait until we fully understand what are the problems that we need to solve and we have the initial”transcript, stated

Patterns

rag-retrieval (knowledge-grounding)

General sleep, CBTI, and expert-codified knowledge moved from the system prompt into file-based retrieval used by both text and voice agents.

“we fell back into OpenAI's filebased rag.”transcript, stated
“remove a lot of the burden of the general knowledge,

some of the program knowledge”transcript, stated

agent-memory (context-management)

A post-conversation prompt extracts user memories, and later conversations retrieve recent or relevant memories to personalize coaching.

“we added a very naive memory to our agent”transcript, stated
“every time we have a new conversation with the user we try to pull from that memory”transcript, stated

eval-driven-development (quality)

Weekly error analysis, domain-expert review, trace annotation, and LLM evals determine AI engineering priorities.

“the underpinning was like a weekly error analysis process.”transcript, stated
“we rely heavily on Langfuse, the Langfus platform where we have annotation cues”transcript, stated

guardrails-io-filtering (safety)

The coach has explicit boundaries against diagnosis, medication discussion, and medical advice, with LLM evals to catch violations.

“we have explicit guard rails against that talking about eos”transcript, stated
“we are very vigilant about not having our coach talk about medication or any medical advice.”transcript, stated

prompt-versioning (model-operations)

Rest tracks prompt versions across the text and voice branches, though the team notes branch drift is messy.

“we are we have version control for the prompts but then start like branching out”transcript, stated

Tradeoffs

Use third-party managed agent/voice platforms before building a custom agent stack.

Chose OpenAI Assistants plus Vapi over full custom LangGraph and memory architecture upfront.

Rationale: Managed platforms reduced initial engineering burden and let the team focus on user value, but they also reduced control.

“that gave us a lot of advantages and we lost some of the control”transcript, stated
“clearly not right now. So that that will have to wait until we fully understand what are the problems”transcript, stated

Keep user data central in the prompt while moving general knowledge to RAG.

Chose always-present user context plus retrieved sleep/CBTI knowledge over putting all user information behind semantic retrieval.

Rationale: Personalization needed to be consistently available, while general knowledge could be retrieved when relevant.

“the user information at this for now is staying in the prompt because we want it to be very central”transcript, stated
“we wanted the user data to be always present”transcript, stated

Use voice despite privacy/context concerns.

Chose voice-first coaching for most interactions over text-only chat.

Rationale: Voice increased intimacy, disclosure, and interaction length, but required maintaining a second technical branch.

“voice is warmer than just text and it's more intimate in a way”transcript, stated
“you need to figure out like how do we adapt it and then how we do maintain it”transcript, stated

Gotchas (what bit them)

Wellness coaching can drift toward medical advice.

Root cause: The product discusses sleep, CBTI principles, and user struggles in a health-adjacent context.

Resolution: Avoid diagnosis, medication advice, and therapy claims; use disclaimers, legal review, experts, and safety evals.

“we're not a medical treatment and that we're not in the app you won't see us diagnose anyone.”transcript, stated
“this is not CBTI therapy. This is this is an app that is inspired on those principles”transcript, stated

Maintaining text and voice parity became difficult.

Root cause: Text and voice use different agent stacks and do not always interpret prompt/tool changes the same way.

Resolution: Version prompts and adapt changes per branch, while recognizing the voice branch currently leads.

“we had a new problem. Now we had to maintain two different ways that should be doing roughly the same thing text and voice”transcript, stated
“it's almost two separate like parallel branches of development”transcript, stated

Memory can become stale or overlong.

Root cause: User context like travel or temporary sleep disruptions may matter briefly but not forever.

Resolution: Use recency heuristics, explore expiration dates, and split recent versus older memories.

“if a memory is old enough, maybe we shouldn't be putting it into the prompt for now.”transcript, stated
“how do we identify the expiration date of a memory”transcript, stated

RAG tool use did not work automatically after moving knowledge out of the prompt.

Root cause: The agent needed immediate-context cues and prompt guidance to know when retrieval was appropriate.

Resolution: Add explicit prompt guidelines about the moments where retrieval should happen.

“we had to be very clear in the prompt what are the guidelines and what are the moments in which there is time to retrieve”transcript, stated
“that was a bit of a trial and an error process.”transcript, stated

Evaluation

Rest evaluates quality through weekly error analysis, domain-expert review of conversations, Langfuse annotation queues, LLM-powered safety/boundary evals, user interviews, customer support signals, and experiments with voice-scenario replication in Hamming.

Metrics: weekly error analysis · weekly user interviews · hundreds of hours of voice conversations per month · LLM-powered medication/medical-advice safety evals · manual annotation queues from everyday conversation traces

3 supporting quotes
“we started working with a sleep expert who reviewed the different interactions.”transcript, stated
“we have some LM powered evals that are there for us to capture things that have to do with safety or boundaries.”transcript, stated
“sample traces of everyday conversations”transcript, stated

Outcomes

The source reports an eight-week protocol, approximately 10% original podcast sleep use, hundreds of voice hours/month, weekly error analysis, and weekly user interviews.

3 supporting quotes
“we introduced voice started testing it out. Right now it's the majority of the user interactions”transcript, stated
“really just in response to your error analysis.”transcript, stated
“we're trying to actually use that knowledge to deliver better solutions”transcript, stated