DEV Community

Cover image for Building CardOS: An AI-Powered Credit Pre-Approval System on Google Kubernetes Engine
Anh Lam
Anh Lam

Posted on

Building CardOS: An AI-Powered Credit Pre-Approval System on Google Kubernetes Engine

This content was created for the purposes of entering the GKE Turns 10 Hackathon

GKEHackathon #GKETurns10


Vision: Revolutionizing Credit Decisions with AI

Traditional credit card applications are painfully slow, opaque, and often miss the mark on what customers actually need. What if I could create an intelligent system that analyzes your real spending patterns, provides instant personalized credit offers, and ensures both customer satisfaction and bank profitability?

That's exactly what I built CardOS - an AI-powered credit pre-approval system deployed entirely on Google Kubernetes Engine (GKE).

🚀 Try the live demo | 📚 View source code

What Makes CardOS Special?

Real-Time Intelligence

Instead of relying solely on credit scores, CardOS analyzes actual spending patterns from banking transactions. It understands that someone who regularly pays for groceries, gas, and utilities is fundamentally different from someone making luxury purchases - and tailors credit offers accordingly.

Multi-Agent AI Orchestra

CardOS orchestrates 6 specialized AI agents working together:

  • Risk Agent: Evaluates creditworthiness with Gemini-powered reasoning
  • Terms Agent: Generates competitive APR and credit limits with intelligent guardrails
  • Perks Agent: Creates personalized cashback offers based on spending categories
  • Challenger Agent: Stress-tests proposals for bank profitability
  • Arbiter Agent: Makes final decisions balancing customer value with bank economics
  • Policy Agent: Generates comprehensive legal documents
  • MCP Server: Provides banking policies and compliance frameworks

Production-Ready Architecture

Built from day one for enterprise scale with comprehensive error handling, intelligent caching, retry logic, and 99.9% uptime reliability.

Building on GKE

Why Google Kubernetes Engine?

When you're orchestrating 6 different AI agents, you need a platform that can scale intelligently. GKE provided exactly what I needed:

Service Discovery: With 6+ microservices communicating, GKE's built-in service discovery made inter-service communication seamless.

Load Balancing: GKE's intelligent load balancing ensures our AI agents never get overwhelmed, even under heavy load.

Zero-Downtime Deployments: Rolling updates mean we can deploy new AI models without service interruption.

Architecture Deep Dive

# My GKE deployment structure
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-service
spec:
  replicas: 3
  selector:
    matchLabels:
      app: backend-service
  template:
    metadata:
      labels:
        app: backend-service
    spec:
      containers:
      - name: backend
        image: python:3.9-slim
        ports:
        - containerPort: 8080
        env:
        - name: GEMINI_API_KEY
          valueFrom:
            secretKeyRef:
              name: gemini-secret
              key: api-key
Enter fullscreen mode Exit fullscreen mode

The AI Agent Pipeline

Here's how our agents work together on GKE:

async def orchestrate_credit_decision(username):
    """
    Sophisticated AI agent orchestration running on GKE
    """
    # Step 1: Health checks across all agents
    agent_health = await check_all_agents_health()

    # Step 2: Risk assessment with early rejection capability
    risk_decision = await call_agent('risk', 'approve', user_data)
    if risk_decision.get('decision') == 'REJECTED':
        return early_rejection_response()

    # Step 3: Parallel execution of core agents
    tasks = [
        call_agent('terms', 'generate', risk_data),
        call_agent('perks', 'personalize', spending_data),
    ]
    terms_data, perks_data = await asyncio.gather(*tasks)

    # Step 4: Challenger optimization
    challenger_analysis = await call_agent('challenger', 'optimize', {
        'terms': terms_data,
        'risk': risk_decision,
        'spending': spending_data
    })

    # Step 5: Arbiter final decision
    final_decision = make_arbiter_decision(
        original_terms=terms_data,
        challenger_offer=challenger_analysis,
        bank_profitability_weight=0.8,
        customer_value_weight=0.2
    )

    # Step 6: Legal document generation
    if final_decision.approved:
        policy_docs = await call_agent('policy', 'generate', final_decision)

    return comprehensive_credit_response()
Enter fullscreen mode Exit fullscreen mode

Deployment Strategy

ConfigMap-Driven Architecture

One of our key innovations was embedding all AI agent code directly in Kubernetes ConfigMaps. This approach provided several advantages:

apiVersion: v1
kind: ConfigMap
metadata:
  name: risk-agent-code
data:
  app.py: |
    import google.generativeai as genai
    from flask import Flask, request, jsonify

    app = Flask(__name__)
    genai.configure(api_key=os.getenv('GEMINI_API_KEY'))

    @app.route('/assess', methods=['POST'])
    def assess_risk():
        # Sophisticated risk assessment using Gemini AI
        # Real implementation with spending pattern analysis
        return jsonify(risk_assessment)
Enter fullscreen mode Exit fullscreen mode

Benefits:

  • Version Control: All agent code is versioned with Kubernetes manifests
  • Easy Updates: Update agent logic without rebuilding Docker images
  • Configuration Management: Centralized configuration across all agents
  • Rapid Deployment: Changes deploy in seconds, not minutes

Production Deployment Pipeline

Our deployment process leverages GKE's powerful features:

# 1. Deploy core infrastructure
kubectl apply -f deployments/backend/
kubectl apply -f deployments/frontend/

# 2. Deploy AI agents with health checks
kubectl apply -f deployments/agents/
kubectl wait --for=condition=available --timeout=300s deployment/risk-agent-simple

# 3. Deploy advanced agents
kubectl apply -f deployments/infrastructure/
kubectl wait --for=condition=available --timeout=300s deployment/challenger-agent

# 4. Configure public access
kubectl apply -f deployments/ingress/
Enter fullscreen mode Exit fullscreen mode

Intelligent Load Balancing

GKE's load balancing proved crucial for our AI workloads:

apiVersion: v1
kind: Service
metadata:
  name: backend-service
spec:
  type: LoadBalancer
  selector:
    app: backend-service
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
  sessionAffinity: ClientIP  # Sticky sessions for AI context
Enter fullscreen mode Exit fullscreen mode

Orchestrating Intelligence at Scale

Gemini Integration Strategy

Integrating Google's Gemini AI across 7 different agents presented unique challenges:

Rate Limiting: We implemented intelligent queuing to respect API limits
Cost Optimization: Strategic prompt engineering reduced token usage by 40%
Reliability: Comprehensive fallback mechanisms ensure system availability

class GeminiManager:
    def __init__(self):
        self.model = genai.GenerativeModel('gemini-1.5-flash')
        self.rate_limiter = RateLimiter(requests_per_minute=60)

    async def generate_with_fallback(self, prompt, fallback_func):
        try:
            async with self.rate_limiter:
                response = await self.model.generate_content_async(prompt)
                return self.parse_response(response)
        except Exception as e:
            logger.warning(f"Gemini API failed: {e}, using fallback")
            return fallback_func()
Enter fullscreen mode Exit fullscreen mode

Financial Modeling Complexity

Building realistic financial models that work in production required sophisticated mathematics:

def calculate_unit_economics(terms, spending_data, risk_assessment):
    """
    Real-world unit economics for credit card profitability
    """
    # Revenue streams
    interchange_revenue = 0.015 * expected_monthly_spend  # 1.5% interchange
    interest_revenue = (terms.apr / 12) * revolving_balance
    annual_fee_revenue = terms.annual_fee

    # Cost components
    perk_costs = sum(category.rate * category.spend for category in terms.cashback)
    expected_loss = risk_assessment.pd * risk_assessment.lgd * terms.credit_limit
    funding_cost = 0.05 * revolving_balance  # 5% cost of funds
    operational_cost = 15  # Monthly operational cost per account

    # Profitability calculation
    monthly_profit = (interchange_revenue + interest_revenue + 
                     annual_fee_revenue/12 - perk_costs - expected_loss - 
                     funding_cost - operational_cost)

    roe = monthly_profit * 12 / (terms.credit_limit * 0.1)  # 10% capital allocation

    return {
        'monthly_profit': monthly_profit,
        'annual_roe': roe,
        'meets_bank_constraints': roe >= 0.15  # 15% minimum ROE
    }
Enter fullscreen mode Exit fullscreen mode

Key Innovations and Lessons Learned

1. Agent Orchestration at Scale

Challenge: Coordinating 7 AI agents with complex dependencies and varying response times.

Solution: Built a sophisticated orchestrator with health checks, timeout management, and graceful degradation.

GKE Advantage: Service mesh capabilities made inter-agent communication reliable and observable.

2. Real-Time Financial Data Processing

Challenge: Processing live banking transactions while maintaining sub-10-second response times.

Solution: Implemented intelligent caching, direct database access, and parallel processing.

GKE Advantage: Auto-scaling ensured we could handle transaction spikes without manual intervention.

3. Regulatory Compliance Automation

Challenge: Generating legally compliant credit documents automatically.

Solution: Policy Agent with comprehensive legal templates and Gemini-powered customization.

GKE Advantage: Secure secret management for API keys and sensitive configuration.

Building CardOS for the GKE Turns 10 Hackathon taught me that with the right platform, you can build production-ready AI systems in record time. GKE provided the foundation that let me focus on AI innovation rather than infrastructure management.

Top comments (0)