In this post, I’ll walk you through how I built SmartFlat AI, a cost-optimized multi-agent AI system for Singapore’s HDB market analysis using AWS Bedrock AgentCore orchestrated by Amazon Nova Lite.
Prerequisites
Start by installing the AWS Bedrock AgentCore Starter Toolkit :
pip install aws-bedrock-agentcore-starter-toolkitEnsure that AWS CLI and SAM CLI are installed and configured properly:
agentcore --help
# Usage: agentcore [OPTIONS] COMMAND [ARGS]...
# BedrockAgentCore CLI
aws --version
# aws-cli/2.30.5 Python/3.13.7 Windows/11 exe/AMD64
aws configure
# AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
# AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
# Default region name [None]:
# Default output format [None]:
sam --version
# SAM CLI, version 1.144.0ap-southeast-2 (Sydney). Amazon Nova Lite, an efficient reasoning model, powers the Supervisor Agent for intelligent query routing.
Multi-Agent Architecture Overview
The system implements a supervisor-agent architecture where a top-level orchestrator delegates queries to specialized sub-agents for optimal cost and performance.
Key Components:
- Supervisor Agent (Nova Lite) – Routes queries intelligently based on cost and complexity.
- Property Agent (Athena) – Handles analytical and statistical queries.
- PostgreSQL Agent (RDS) – Serves frequent and simple queries at low cost.
- Demographics Agent – Fetches live data from government APIs.
Phased Infrastructure Deployment
Deployment is handled in modular CloudFormation stacks, allowing incremental development and testing.
Phase 1: Foundation Stack
Purpose: Establish the web interface, chat handler, and session memory.
Key Components:
- S3 Static Website: Hosts the chat UI
- API Gateway HTTP API: Routes user queries to Lambda
- Lambda Chat Handler: Connects frontend with AgentCore Supervisor
- DynamoDB: Stores session memory
- IAM Roles: Secure access to Bedrock and data stores
# Phase 1 creates the foundation
Resources:
WebHostingBucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: !Sub "${ProjectPrefix}-web-${EnvironmentName}-${VersionTag}"
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: index.html
ChatHandlerLambda:
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: !Sub "${ProjectPrefix}-chat-handler-${EnvironmentName}-${VersionTag}"
Runtime: python3.13
Environment:
Variables:
AGENT_ARN: !Ref AgentArn
DDB_TABLE_NAME: !Ref AgentMemoryTablePhase 2: Athena Analytics Stack
Purpose: Enable serverless analytics for large-scale HDB resale data.
Key Components:
- AWS Glue Database: Schema catalog for HDB datasets
- Amazon Athena: Query engine for analytical workloads
- S3 Data Lake: 300K+ resale transactions from data.gov.sg
- Athena API Lambda: RESTful interface for SQL analytics
- Query Results Bucket: Stores Athena query outputs with lifecycle policies
# Phase 2 adds analytical capabilities
Resources:
HDBGlueTable:
Type: AWS::Glue::Table
Properties:
TableInput:
Name: 'resale_transactions'
StorageDescriptor:
Columns:
- Name: 'month'
Type: 'string'
- Name: 'town'
Type: 'string'
- Name: 'flat_type'
Type: 'string'
- Name: 'resale_price'
Type: 'double'
Location: !Sub 's3://${ProjectPrefix}-rawdata-${EnvironmentName}-${VersionTag}/'Phase 3: Cost-Optimized PostgreSQL Stack
Purpose: Handle frequent, low-latency queries at minimal cost.
Key Components:
- PostgreSQL RDS: Mirrors HDB data for transactional queries
- RDS Proxy: Improves scalability and connection pooling
- PostgreSQL API Lambda: Executes fast SQL lookups
- Secrets Manager: Manages DB credentials securely
- VPC Integration: Provides secure private networking
# Phase 3 adds cost optimization
Resources:
PostgreSQLAPIFunction:
Type: AWS::Lambda::Function
Properties:
Runtime: python3.13
VpcConfig:
SecurityGroupIds:
- !Ref LambdaSecurityGroup
SubnetIds:
- subnet-05ef955b7c5edb66e
- subnet-0be15cf15ab7853eb
Environment:
Variables:
SECRET_ARN: !Ref DatabaseSecret
CONNECTION_TYPE: direct_connectionIntelligent Query Routing: The Cost Optimization Brain
The Supervisor Agent, powered by Amazon Nova Lite, dynamically routes queries across agents based on complexity, latency, and cost.
1. Demographics Path
- Triggers: population, citizen, statistics, demographic
- Routes to: Data Commons API
- Example: “What is the population of Singapore?”
- Cost: Free (public data source)
2. Complex Analytics Path
- Triggers: correlation, trend, statistical, complex
- Routes to: Amazon Athena
- Example: “Complex price trend analysis for 5-room flats in Tampines over 5 years”
- Cost: ~$0.59/query
3. Cost-Optimized Path
- Triggers: average, count, latest, basic, quick
- Routes to: PostgreSQL RDS
- Example: “Average HDB prices in Bishan for 4-room flats”
- Cost: ~$0.01/query (~70% cheaper than Athena)
Routing Logic Implementation
def _route_query(self, query: str) -> str:
"""Intelligent routing for cost optimization"""
query_lower = query.lower()
# Demographics (different data source)
if any(keyword in query_lower for keyword in demographics_keywords):
return 'demographics'
# Complex analytics (use Athena for heavy computation)
if any(keyword in query_lower for keyword in athena_keywords):
return 'property'
# Cost-optimized queries (PostgreSQL for frequent operations)
if any(keyword in query_lower for keyword in postgres_keywords):
return 'postgres'
# Default to cost-optimized PostgreSQL
return 'postgres'Deployment Workflow
# 1. Environment setup
.\setup.ps1
.venv\Scripts\Activate.ps1
# 2. Configure and deploy AgentCore supervisor
agentcore configure -e agent/supervisor.py
agentcore launch
# 3. Deploy infrastructure phases
.\deploy-phase1.ps1 # Foundation: Web + Chat + Memory
.\deploy-phase2.ps1 # Analytics: Athena + S3 + Glue
.\deploy-phase3.ps1 # Optimization: PostgreSQL + RDS Proxy
.\deploy-cloudfront.ps1 # CDN: Global distributionEach phase builds upon the previous, supporting incremental testing, faster iteration, and lower deployment risk.
Multi-Agent Capabilities in Action
Demographics Agent
{"prompt": "What is the population of Singapore?"}Response: Live population data from Data Commons API Routing: Demographics Agent → Data Commons API
Property Agent (Complex Analytics)
{"prompt": "Complex price trend analysis for 5-room flats in Tampines over 5 years"}Response: Growth rate, volatility, and percentile analytics Routing: Property Agent → Athena API → S3 Data Lake (300K+ transactions)
PostgreSQL Agent (Cost-Optimized)
{"prompt": "Average HDB prices in Bishan for 4-room flats"}Response: Quick aggregated result from RDS Routing: PostgreSQL Agent → RDS Proxy → PostgreSQL
Cost Optimization Results
The intelligent routing system achieves significant cost savings:
| Query Type | Agent | Data Source | Cost per Query | Savings |
|---|---|---|---|---|
| Simple Queries | PostgreSQL | RDS | ~$0.01 | 70% cheaper |
| Complex Analytics | Property | Athena | ~$0.59 | Optimized for heavy workloads |
| Demographics | Demographics | Data Commons | Free | Publicly available statistical data |
Key Insight: About 80% of user queries are routed to the PostgreSQL agent, reducing cost substantially without losing analytical depth.
Production Architecture Benefits
💰 Cost Intelligence
- Automatic routing between Athena and PostgreSQL
- ~70% reduction in average query cost
- Pay-per-use scalability
🧠 Scalable Multi-Agent Design
- Supervisor pattern using Bedrock AgentCore
- Amazon Nova Lite for efficient reasoning
- Modular, extensible agents
🗄️ Dual Data Architecture
- Shared HDB data served via Athena (analytical) and PostgreSQL (transactional)
- Optimized for both ad-hoc analysis and frequent lookups
⚙️ Production-Ready Infrastructure
- CloudFront CDN for global delivery
- RDS Proxy for secure, scalable DB connections
- CloudFormation IaC for full automation
- Phased deployment for controlled rollout
Live Demo
Try it yourself: SmartFlat AI
The live demo showcases all three agents in action, demonstrating how intelligent routing optimizes both performance and cost.
Key Takeaways
- Phased Development: Incremental deployment reduces risk and enables continuous testing
- Cost Optimization: Intelligent routing can achieve 70% cost savings without sacrificing functionality
- Multi-Agent Architecture: Specialized agents provide better performance than monolithic systems
- Dual Data Sources: Same data, different engines for different use cases
- Amazon Nova Lite: Cost-effective AI model perfect for query routing and orchestration
GitHub Repository: https://github.com/seehiong/smartflat-ai