Back to Blog Engineering

The Brain Service Architecture

At the heart of BRF is what we call the "Brain Service" - an AI-powered system that understands user intent, categorizes habits, and provides intelligent coaching. This post dives into how we built it using AWS services and the architectural decisions we made along the way.

The Technology Stack

AWS Bedrock

Claude 3.5 Sonnet for natural language understanding and response generation

AWS Lambda

Serverless compute for request handling and orchestration

AWS Comprehend

Sentiment analysis and key phrase extraction

DynamoDB

Conversation history and user context storage

How It Works

When a user creates a habit like "I want to stop smoking," the Brain Service processes this through several stages:

1. Intent Detection

First, we analyze the user's input to understand what they're trying to do. Key signals include:

  • Action words: "start," "begin," "stop," "quit," "reduce"
  • Sentiment: Is this framed positively or negatively?
  • Context: What does the user's history tell us?

2. Habit Categorization

Based on the intent, we categorize the habit as BUILD or BREAK. The prompt we send to Bedrock includes:

Analyze this habit request and determine:
1. Is this a BUILD habit (adding positive behavior)
   or BREAK habit (removing negative behavior)?
2. Suggest an appropriate name, icon, and color
3. Identify the underlying motivation

User input: "{user_input}"
User intent keywords: {detected_keywords}

3. Response Generation

Claude generates a response that acknowledges the user's goal and provides encouragement. We use a system prompt that establishes BRF's personality - supportive but not preachy, practical but optimistic.

Conversation Context

One of the key features of the Brain Service is maintaining conversation context. We store the last N messages in DynamoDB and include them in each request to Bedrock. This allows for natural follow-up interactions:

User: "I want to exercise more"
BRF:  "That's a great BUILD habit! How often would you
      like to exercise - daily, or specific days?"
User: "Maybe 3 times a week"
BRF:  "Perfect! I've set up 'Exercise' as a 3x weekly
      habit. Which days work best for you?"

Cost Optimization

Running AI at scale can be expensive. Here's how we keep costs manageable:

  • Prompt caching: Common intents get cached responses
  • Token limits: We cap conversation history to prevent runaway costs
  • Model selection: Claude 3.5 Sonnet balances quality and cost
  • Batching: Non-urgent analysis happens in batches

Error Handling

AI services can be unpredictable. Our error handling strategy includes:

  • Graceful fallbacks when Bedrock is unavailable
  • Input validation before sending to the model
  • Response validation to catch hallucinations
  • Rate limiting to prevent abuse

What's Next

We're continuously improving the Brain Service. Upcoming features include:

  • Personalized coaching based on habit history
  • Proactive suggestions based on patterns
  • Voice interactions using AWS Polly and Transcribe
  • Multi-modal inputs (images, voice notes)

The Brain Service is a core part of what makes BRF different from traditional habit trackers. By understanding user intent and providing intelligent responses, we help users stay motivated and on track with their goals.