Skip to main content

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:
Add to Vercel:
  1. Go to Vercel Dashboard → Your Project → Settings → Environment Variables
  2. Add new variable:
    • Name: INTERNAL_API_TOKEN
    • Value: YuFqc9tIfFt3Z2kxlYfv8ZLxV1GzYUvMBhrGkYNi+KA=
    • Environments: Production, Preview, Development
  3. Click “Save”
  4. Redeploy your application to apply changes
Verify:

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.
What Gets Created:
  • 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:
Look for:
  • InternalApiTokenSecret: Name of the secret (should be dev-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:
Look for:
  • Processing radar for user {clerk_id}
  • Successfully processed radar {radar_id}
  • Error calling internal chat API
Orchestrator Lambda:

DynamoDB Tables

Schedules:
Results:

Debugging

Check processor logs:
Check SQS queue:
Check DynamoDB:

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:
  1. Get token from AWS:
  1. Compare with Vercel environment variable
  2. Update if different

Error: Timeout calling internal API

Cause: Internal API taking longer than 10 minutes Fix:
  1. Check Vercel function logs for the internal API
  2. Consider breaking complex queries into smaller parts
  3. Increase processor timeout if needed (CDK stack)

Error: No results in DynamoDB

Cause: Schedule not being triggered or processor failing Fix:
  1. Check orchestrator ran at 6am UTC:
  1. Check SQS for messages
  2. Check processor logs for errors

Cost Comparison

Before (Complex Agent)

  • Lambda Memory: 2048MB
  • Lambda Duration: ~5-10 minutes per execution
  • VPC: NAT Gateway (0.045/hour=0.045/hour = 32.40/month)
  • Network Transfer: VPC to Supabase
  • S3: Chart storage and bandwidth
Estimated Monthly Cost: ~$50-75/month

After (Internal API)

  • Lambda Memory: 512MB
  • Lambda Duration: ~30-60 seconds per execution
  • VPC: None
  • Network Transfer: Minimal (just API calls)
  • S3: None
Estimated Monthly Cost: ~$5-10/month Savings: ~$40-65/month (80-87% reduction)

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:
  1. Restore the old processor code from git:
  1. Restore the old CDK stack:
  1. Redeploy:

Support

For issues:
  1. Check Vercel function logs for internal API
  2. Check CloudWatch logs for Lambda functions
  3. Check DynamoDB tables for data
  4. Review this guide’s troubleshooting section