Radar Migration Guide: Simplified Internal Chat API Integration
This guide documents the migration from the complex PersonalFinancialAgent to the simplified internal chat API approach.What Changed
Before
- Processor Lambda: 2048MB memory, 15min timeout, VPC required
- Dependencies: ~20 Python packages (agno, anthropic, duckdb, matplotlib, pandas, numpy, yfinance, exa, polygon, psycopg2, seaborn, etc.)
- Code: ~500+ lines across multiple files (agents, prompts, tools, utils)
- Database: Direct Supabase/PostgreSQL connections
- Charts: S3 bucket for chart storage
- Architecture: Complex multi-agent system with tools
After
- Processor Lambda: 512MB memory, 10min timeout, no VPC
- Dependencies: 2 Python packages (boto3, requests)
- Code: ~200 lines in single handler.py file
- Database: No direct connections (uses internal API)
- Charts: None (removed)
- Architecture: Simple HTTP client calling internal chat API
Benefits
✅ 90% Code Reduction - Removed agents, prompts, tools, utils folders✅ 95% Dependency Reduction - From 20+ packages to 2
✅ 75% Memory Reduction - From 2048MB to 512MB
✅ Cost Savings - No VPC, lower memory, faster cold starts
✅ Single Source of Truth - All AI logic in internal chat API
✅ Consistent Behavior - Same responses as interactive chat
✅ Easier Maintenance - One place to update prompts and tools
✅ Faster Deployments - Smaller Docker images, faster builds
Setup Instructions
Step 1: Add Token to Vercel
The internal API requires authentication via bearer token. Token Value:- Go to Vercel Dashboard → Your Project → Settings → Environment Variables
- Add new variable:
- Name:
INTERNAL_API_TOKEN - Value:
YuFqc9tIfFt3Z2kxlYfv8ZLxV1GzYUvMBhrGkYNi+KA= - Environments: Production, Preview, Development
- Name:
- Click “Save”
- Redeploy your application to apply changes
Step 2: Deploy CDK Stack
The token is already configured in the CDK stack. When you deploy, it will be stored in AWS Secrets Manager.- Secret:
dev-RadarInternalApiToken(contains the token) - Updated Lambda: Uses 512MB, no VPC, calls internal API
- Same DynamoDB tables, SQS queue, API Gateway
Step 3: Verify Deployment
After deployment, check the CloudFormation outputs:InternalApiTokenSecret: Name of the secret (should bedev-RadarInternalApiToken)ApiUrl: Your API Gateway URL
Step 4: Test the Processor
Trigger a test radar execution:Architecture Overview
Request Flow
Data Flow
Monitoring
CloudWatch Logs
Processor Lambda:Processing radar for user {clerk_id}Successfully processed radar {radar_id}Error calling internal chat API
DynamoDB Tables
Schedules:Debugging
Check processor logs:Troubleshooting
Error: “Failed to retrieve API token”
Cause: Secret not found or Lambda doesn’t have permission Fix:Error: 401 Unauthorized from internal API
Cause: Token mismatch between AWS and Vercel Fix:- Get token from AWS:
- Compare with Vercel environment variable
- Update if different
Error: Timeout calling internal API
Cause: Internal API taking longer than 10 minutes Fix:- Check Vercel function logs for the internal API
- Consider breaking complex queries into smaller parts
- Increase processor timeout if needed (CDK stack)
Error: No results in DynamoDB
Cause: Schedule not being triggered or processor failing Fix:- Check orchestrator ran at 6am UTC:
- Check SQS for messages
- Check processor logs for errors
Cost Comparison
Before (Complex Agent)
- Lambda Memory: 2048MB
- Lambda Duration: ~5-10 minutes per execution
- VPC: NAT Gateway (32.40/month)
- Network Transfer: VPC to Supabase
- S3: Chart storage and bandwidth
After (Internal API)
- Lambda Memory: 512MB
- Lambda Duration: ~30-60 seconds per execution
- VPC: None
- Network Transfer: Minimal (just API calls)
- S3: None
Migration Checklist
- Simplify processor handler.py to call internal API
- Update requirements.txt (remove all complex dependencies)
- Update Dockerfile (remove agent/prompt/tool copies)
- Modify CDK stack (remove VPC, reduce memory, add API token)
- Remove agents folder
- Remove prompts folder
- Remove tools folder
- Remove utils folder
- Update API handler (remove chart endpoints)
- Remove chart routes from CDK stack
- Add INTERNAL_API_TOKEN to Vercel
- Deploy CDK stack to AWS
- Test processor with sample schedule
- Monitor CloudWatch logs
- Verify cost reduction
Rollback Plan
If you need to rollback:- Restore the old processor code from git:
- Restore the old CDK stack:
- Redeploy:
Support
For issues:- Check Vercel function logs for internal API
- Check CloudWatch logs for Lambda functions
- Check DynamoDB tables for data
- Review this guide’s troubleshooting section