Domain Model
Chat Aggregates
«Aggregate Root» ChatSession
- id : SessionId PK
- userId : UserId
- mode : MIAMode {WEALTH, MARKETS}
- messages : List<Message>
- context : SessionContext
- tokenCount : int
- model : ModelTier
- createdAt : Instant
+ addMessage(msg) : void
+ routeModel() : ModelTier
+ summarize() : String
+ exceedsContextWindow() : bool
«Abstract» Message
- id : MessageId
- role : Role {USER, ASSISTANT, SYSTEM, TOOL}
- content : String
- timestamp : Instant
- tokens : int
UserMessage
- intent : Intent
- chips : List<Chip>
AssistantMsg
- citations : List<Cite>
- toolCalls : List<Tool>
ToolResult
- toolId : String
- output : JSON
«Value Object» SessionContext
- userProfile : UserSnapshot
- portfolio : PortfolioSnapshot
- ipsProfile : IPSSnapshot
- activeGoals : List<GoalSnapshot>
- tier : Tier
WEALTH
Warm gold avatar
Advisory, goals, KYC
MARKETS
Sharp green avatar
Stocks, news, trades
RAG Pipeline
Retrieval-Augmented Generation Flow
User Query
"How is my SIP performing?"
Intent Classifier
Rule + LLM hybrid
Embed Query
text-embedding-3-small
Vector Search
Pinecone | top-k=5
Context Assembly
System prompt + user context
+ RAG chunks + tool defs
Claude API
tool_use | streaming SSE
Sonnet 4 (default)
Safety Pipeline
4-stage chain
(see bottom-left)
Stream Response
SSE → Kotlin CMP client
Token-by-token
User Input
Domain/Business
Data/Embedding
External Service
Security/Safety
Infrastructure
Events/Async
Tool Execution Flow (tool_use)
1
Claude tool_use
Model outputs JSON tool call:
get_portfolio_summary
create_sip
calculate_goal
2
Confirmation Gate
Read-only tools: auto-approve
Write tools: user confirmation
Financial ops: biometric verify
Biometric required for > ₹50,000
3
Spring Command
Dispatched as domain command
to target microservice via
gRPC / REST internal API
4
Kafka Event
byld.mia.events
byld.sip.events
byld.order.events
Tools are defined as JSON Schema in system prompt. Claude decides when and which tool to invoke. Human-in-the-loop for all write operations.
Model Routing
Multi-Model Strategy
ModelUse CaseLatencyCost/Query
Haiku 3.5 FAQ, navigation, simple lookups, chip generation ~200ms ~$0.001
Sonnet 4 Portfolio analysis, goal planning, tax Q&A, rebalance explain ~1.2s ~$0.012
Opus 4 Complex multi-step advice, estate planning, tax optimization ~3s ~$0.10
Routing Decision Tree
if intent in [FAQ, NAV_CHECK, NAVIGATE]
  → Haiku
elif intent in [ANALYSIS, GOAL, REBALANCE]
  → Sonnet
elif intent in [ESTATE, TAX_OPT, COMPLEX]
  → Opus
elif tier == LEGACY
  → Opus (always premium)
$0.03
Avg cost / conversation
1.8s
Avg TTFT (time to first token)
94%
Queries on Haiku/Sonnet
Safety Pipeline
4-Stage Response Processing Chain
PII Redaction
Detect & mask Aadhaar, PAN,
phone, account numbers
in both input & output
Compliance Filter
Block guaranteed returns,
ensure SEBI disclaimer,
past performance caveat
Hallucination Guard
Verify NAV / return claims
against ground truth data.
Flag unverifiable assertions
Citation Extractor
Extract data sources,
attach fund factsheets,
link to Morningstar pages
Blocked Patterns
"guaranteed returns" | "risk-free" | "assured profit"
"you should buy" (without disclaimer) | personal tax advice
Aadhaar regex: \d{4}\s?\d{4}\s?\d{4}
Auto-Appended Disclaimers
"Mutual fund investments are subject to market risks."
"Past performance does not guarantee future results."
"This is not personal tax/legal advice."
byld.mia.events/MessageSent byld.mia.events/PIIDetected byld.mia.events/ComplianceBlocked byld.mia.events/HallucinationFlagged byld.mia.events/ToolInvoked byld.mia.events/FeedbackReceived
REST + WebSocket API
POST /api/v1/mia/sessions
Create new chat session
POST /api/v1/mia/sessions/{id}/messages
Send message (returns SSE stream)
GET /api/v1/mia/sessions/{id}
Get session history
POST /api/v1/mia/tools/{id}/confirm
Confirm tool execution
POST /api/v1/mia/feedback
Thumbs up/down + reason
SSE Events:
event: token | data: {"t":"Hello"}
event: tool_use | data: {"name":"..."}
event: done | data: {"tokens":142}
event: error | data: {"code":"SAFETY"}
Architect's Notes
MIA is the crown jewel. It is the primary interaction layer for the entire platform. Every other service is, in a sense, a "tool" that MIA can invoke on the user's behalf.

RAG over Pinecone ensures MIA's answers are grounded in real portfolio data, fund factsheets, and regulatory docs -- not hallucinated.

Safety Pipeline is non-negotiable for SEBI compliance. No financial AI can make "guaranteed return" claims.

Confirmation Gate is the ethical boundary. MIA suggests; the human decides. Biometric for >50K ensures no accidental large transactions.
"The AI is the adapter, not the domain.
The domain rules stand with or without it."