Back to Case Studies

AI for a CBT Digital Therapeutic

Guided Anxiety Sessions and Safe Open-Ended Coaching

ProductionHealthcare & WellbeingJul 2025 – Dec 2025NLP, Knowledge Systems & Generative AI
< 3 s
Response time for 95% of interactions
> 85%
Structured CBT sessions completed
99.9%
Uptime, under 1% failed chats
Launched
Full app live with users

My Role

Role
Pre-sale author, architect & AI lead
At Dataforest
Team
Full delivery team with QA, DevOps, DM
Stage reached
App launched fully
Key decisions
  • Separate engines for structured sessions and coaching
  • Sessions as JSON trees on a state machine
  • Safety pipeline around every coaching reply

Approach at a Glance

Details
  1. 1Two Modules, Two Designs
  2. 2Sessions as Conversation Trees on a State Machine
  3. 3Hybrid Answer Validation
  4. 4Homework into PDFs
  5. 5Coaching with Guardrails
  6. 6Crisis Detection and Escalation
  7. 7Voice, Mood and Personalisation
  8. 8Quality and Reliability

Executive Summary

The AI layer of a digital therapeutic app for people with generalized anxiety disorder (GAD), based on cognitive behavioural therapy (CBT). It has two conversational modules with very different needs:

  • Structured CBT sessions: guided exercises that follow the therapy programme step by step, such as sorting worries into productive and unproductive, with homework that ends up as PDFs.
  • Open-ended AI coaching: supportive conversations between sessions that teach coping skills, inside strict therapeutic boundaries and with crisis escalation.

I designed both modules on Azure OpenAI GPT-4. The structured sessions run as JSON conversation trees on a state machine, with the LLM only validating and explaining. The coaching module uses a safety pipeline around every reply. Voice sessions, mood and sentiment tracking and personalised exercises completed the platform.

The system met its targets: under 3 seconds for 95% of replies, over 85% of structured sessions completed, and 99.9% uptime with under 1% failed conversations. I was the pre-sale author, architect and AI tech lead from July to December 2025, in a full delivery team with engineers, QA, DevOps and a delivery manager. The app launched fully.


Client Profile

The client was building a CBT-based digital therapeutic for adults with generalized anxiety. The therapy programme already existed as sessions, exercises and homework designed by clinicians. The app had to deliver it through conversation, and offer support between sessions.

Mental-health AI has to be trustworthy first and clever second:

  • The programme is clinical. Sessions have defined steps and goals. A free-wheeling chatbot that drifts off the exercise is not therapy.
  • Answers need judging, not matching. In an exercise, the user's answer may be right in substance but worded in a way no keyword list expects.
  • Safety is non-negotiable. The AI must never give medical advice, must recognise a crisis immediately, and must know when to hand over to a human.
  • Context has to survive. Users come back across days and weeks, and losing where they were in a session or what they've learned breaks the therapeutic relationship.
  • Health data is sensitive. Logging, storage and access had to meet HIPAA-level expectations.

Technical Approach

  1. Two Modules, Two Designs – I split the AI into a structured session engine and an unstructured coaching engine, because they need opposite behaviour. Structured sessions must be consistent and predictable, so they run with low LLM temperature (0.3). Coaching must feel natural and empathetic, so it runs at 0.7. Each module has its own prompt templates carrying the session, step, user progress and clinical goal.

  2. Sessions as Conversation Trees on a State Machine – Each CBT session is a JSON conversation tree with steps, expected responses, branches and checkpoints, executed by a finite state machine. The clinical content stays in data that can be reviewed and changed without code, and every user always knows where they are in the programme. Progress is tracked through the checkpoints, and state persists in Cosmos DB, with live context in Redis, so a session can resume after a break.

  3. Hybrid Answer Validation – In exercises such as worry sorting, where the user labels 6–8 example worries as productive or unproductive, each answer is checked in layers:

    • keyword matching for clear answers;
    • LLM semantic analysis for nuanced ones;
    • a fallback for unexpected input.

    The LLM then gives context-aware feedback explaining why an answer is right or not, and the session adapts to the user's progress. Pre-written responses cover critical therapeutic moments, so the most sensitive steps never depend on generation.

  4. Homework into PDFs – Homework runs as three guided chat flows: a worry log, a worry-time review and a refocus tracker. They collect structured answers, and Azure Functions fill templates to generate a PDF for each, stored encrypted in Blob Storage with download tracking and access control.

  5. Coaching with Guardrails – Every coaching message goes through intent classification → context retrieval → response generation → safety check. The system prompt holds the therapeutic boundaries: evidence-based CBT techniques, no medical advice, no crisis counselling, escalate when needed. Replies draw on a coping-skills knowledge base and the user's history, including completed sessions, learned skills and recent concerns, so the coach builds on the programme rather than starting from scratch.

  6. Crisis Detection and Escalation – Content filtering blocks harmful content. Crisis detection triggers immediate escalation to a human provider, and the provider dashboard shows sessions and alerts in real time. Every conversation is logged for audit, with HIPAA-compliant logging and monitoring.

  7. Voice, Mood and Personalisation – Azure Speech Services add voice-guided sessions for accessibility. Sentiment analysis runs over session responses and journal entries to track mood and risk. A personalisation engine recommends exercises and adjusts the approach to each user's progress.

  8. Quality and Reliability – Response quality is checked for therapeutic appropriateness, accuracy and empathy, with clinical review by therapists and A/B tests of prompt strategies. Azure autoscaling, caching and session recovery kept replies under 3 seconds for 95% of interactions, with 99.9% uptime and under 1% failed conversations.


Business Outcome

The app launched fully, delivering a clinician-designed CBT programme through conversation and supporting users between sessions. It met all its technical targets: replies under 3 seconds for 95% of interactions, over 85% of structured sessions completed, 99.9% uptime and under 1% failed conversations.

The design choice that mattered most was deciding where the LLM is allowed to be creative. In structured sessions, the programme lives in conversation trees and the model only judges and explains answers. In coaching, the model speaks freely, but always inside a safety pipeline that can stop it and bring in a human.

Technologies

Azure OpenAI GPT-4Finite State MachineJSON Conversation TreesAzure SpeechSentiment AnalysisCosmos DBRedisAzure FunctionsAzure AD B2C

Planning something similar?

Tell me about it on a short call. New projects can start within 2 weeks