About the Project
The Beginning
Every day millions of people get stuck in negative thought spirals. We worry about presentations, doubt our decisions, and feel paralyzed by stress. I've been there too. What struck me was that professional therapists have these amazing cognitive tools to help people break free from these patterns, but most of us don't have access to that expertise exactly when we need it.
That's what inspired Cognitive Reframer. I wanted to build something that could bring evidence-based mental frameworks to anyone, instantly. Not just generic advice, but the actual techniques from cognitive behavioral therapy and systems thinking that really work.
Building the Agent
The core idea was to create an autonomous AI agent that could analyze a stressful thought and provide two different cognitive reframes, each using a distinct mental model. I chose eight frameworks that research shows are effective for common cognitive distortions. These include inversion thinking, first principles, the dichotomy of control, and five others.
Amazon Bedrock became the foundation. I used Claude v2 for the natural language reasoning because it excels at structured output generation. The challenge was making it reliably produce consistent JSON responses. Early attempts were all over the place. Sometimes the model would add extra text, sometimes the format would vary, and occasionally it would just miss required fields entirely.
I solved this through careful prompt engineering. The system prompt includes detailed definitions of all eight mental models, explicit JSON schema requirements, and few-shot examples showing exactly what good output looks like. I also set the temperature to 0.3 for more deterministic responses. After these changes, I achieved 100% parsing success.
The architecture follows AWS Bedrock AgentCore patterns. There's a main Lambda function that orchestrates everything. It validates input, checks for safety concerns, queries past reframes from DynamoDB for personalization, constructs the prompt, invokes Bedrock, parses the response, and stores the result. I built separate Lambda functions as tools for memory operations and scheduling reminders.
For storage, I use three DynamoDB tables. One holds the reframes themselves, one tracks user profiles, and one manages follow-up reminders. I added a 90 day TTL for privacy. The tables use Global Secondary Indexes for efficient querying by user and timestamp.
The frontend is intentionally simple. Just HTML, CSS, and vanilla JavaScript. I wanted to focus the hackathon time on getting the agent intelligence right rather than building a complex UI. It's hosted on S3 with CloudFront for global delivery.
The Hard Parts
Getting JSON parsing to work reliably was the first major challenge. LLMs are probabilistic by nature, so even with good prompts they sometimes surprise you. I built a robust parser that searches for JSON boundaries in the response text, extracts just that portion, and validates all required fields before accepting it.
Safety was another concern I took seriously. This tool deals with stress and mental health adjacent topics. I needed to detect crisis situations without over-triggering on normal stress. I implemented keyword-based screening for self-harm language. When detected, the system provides crisis resources immediately and skips attempting to reframe. I also added prominent disclaimers that this is not therapy and people should seek professional help for serious issues.
Memory implementation presented an interesting constraint. AgentCore Vector Memory is powerful but requires an embedding generation pipeline. For the hackathon timeline, I went with simpler temporal retrieval using DynamoDB GSI. It queries the most recent reframes for context. This works well for basic personalization. The architecture makes it straightforward to upgrade to vector search with embeddings later.
Cost optimization mattered because I wanted this to be sustainable beyond the hackathon. Bedrock Claude v2 can get expensive at scale. I structured prompts to minimize token usage and documented how to switch to Claude Instant in production, which costs 80% less. The current architecture runs about $131 per month for 10,000 users.
Tool orchestration was tricky because AgentCore Gateway isn't fully available in my account yet. I implemented Lambda-based tools that follow the AgentCore pattern but run independently. When Gateway becomes available, the migration path is clear because I designed with that in mind from the start.
What I Learned
On the technical side, I learned a ton about prompt engineering. Getting consistent structured output from an LLM requires being extremely explicit about what you want. Few-shot examples are incredibly powerful. Temperature settings matter more than I expected. And you always need robust parsing because even the best prompts occasionally produce surprises.
I also gained deep experience with AWS Bedrock's invocation patterns and AgentCore architectural concepts. Understanding how Runtime, Memory, Gateway, and Tools fit together gave me a mental model for building autonomous agents at scale. Serverless orchestration in Lambda works beautifully for this use case because each reframe request is independent.
From a product perspective, I learned that users want concrete action steps, not vague suggestions. Every reframe includes 2-3 micro-actions that take 10 to 30 minutes each. This specificity makes the advice immediately actionable. I also learned that transparency builds trust. Showing which mental model was selected helps users understand why they're getting a particular perspective.
The domain research was fascinating. I dove into cognitive behavioral therapy literature and systems thinking frameworks. Understanding when different mental models apply and how they address specific cognitive distortions gave me appreciation for just how sophisticated these techniques are. There's real science here.
Looking Forward
The agent works end to end right now. You can submit a stressful thought and get back two thoughtful reframes with concrete action steps. The system remembers your history and personalizes future suggestions. Safety screening catches crisis situations. Everything deploys with one command and scales automatically.
But there's so much more to build. Next up is migrating to AgentCore Vector Memory for semantic search across past reframes. Then calendar integration so follow-ups actually appear in your schedule. Multi-turn conversations would let users dig deeper into specific reframes. And eventually, integrations with Slack, email, and mobile apps to meet people where they already are.
The exciting part is that the foundation is solid. The core agent intelligence works. The architecture scales. The mental models are proven. Everything from here is about making it more accessible and more integrated into daily life.
What started as an idea about democratizing cognitive tools has become a working system that anyone can use. That's pretty cool.
Built With
- amazon-api-gateway
- amazon-bedrock
- amazon-bedrock-agentcore
- amazon-cloudwatch
- amazon-dynamodb
- amazon-web-services
- aws-lambda
- aws-sam
- boto3
- css3
- html5
- javascript
- python
Log in or sign up for Devpost to join the conversation.