System Architecture
Last updated
Last updated
MAFIA (Modular Autonomous Financial Intelligence Agent) is a multi-agent system orchestrated by the AgentController, which delegates tasks to specialized subagents.
Agent Controller (Orchestrator): Classifies intent and routes control of flow to to specific subagent for processing.
SubAgents: Each subagent is a specialist in a a given domain, typically a DeFi protocol, web search or knowledge base lookup using RAG. Subagents are equipped with tools that enable them to execute actions, like creating transaction call data, creating user-operations to send to the user's smart wallet, and executing transactions.
Agent Service: This serves as the quality control mechanism. Multiple decisions are made by the system between user input and system output. The Agent Service ensures the output aligns with the user's intention.
Maintains context across multiple user messages
Persistent transaction state in Session Manager
Conversation history tracking
Confidence scoring for routing decisions
Multiple classification methods (keyword, LLM, fallback)
Context-aware transaction state detection
Always requires user confirmation before generating transactions
Clear transaction summaries before confirmation
Cancel/abort capabilities at any step
Modular node design for easy extension
Tool integration (Tavily, ChromaDB, web3.py)
Routing confidence scores
Processing time tracking
Error handling and reporting
LangSmith integration support
Internally, MAFIA is powered by a sophisticated LangGraph workflow that provides intent classification, state management and action execution and multi-step transaction creation. The graph orchestrates multiple nodes to handle web search, knowledge base queries, and multi-step transaction creation.
Entry Point - Sophisticated intent classification with confidence scoring
Features:
Keyword-based routing (high confidence ≥ 0.8)
LLM-assisted routing (medium confidence 0.4-0.8)
Fallback routing (low confidence < 0.4)
Transaction state awareness - Detects ongoing transactions
Multi-turn conversation support
Intent Classifications:
web_search
- Market data, DeFi news, current prices
knowledge_base
- Protocol documentation queries
transaction_inquiry
- "What can I do?" type questions
transaction_request
- Incomplete transaction requests
transaction_confirmation
- Yes/No confirmation responses
transaction_complete
- Complete transaction ready for execution
general
- DeFi education and general conversation
Purpose: Real-time DeFi market data and news retrieval
Tool: Tavily Web Search API Use Cases:
Current token prices
DeFi protocol news
Market trends and analysis
Yield farming opportunities
Purpose: RAG-powered queries about Chedda Finance & MAFIA AI
Tool: File Knowledge Base (ChromaDB) Content Sources:
Gitbook documentation
Protocol specifications
FAQ documents
User guides
Purpose: List available DeFi operations and capabilities
Responses:
Available transaction types (supply, withdraw, borrow, repay)
Supported assets (USDC, ETH, etc.)
Pool information (CB Assets, Memecoin pools)
Example commands
Purpose: Parse transaction details and gather missing information
Features:
Component extraction from natural language
Missing information detection
Session state persistence via Session Manager
Multi-turn information gathering
Parsed Components:
Action: supply, withdraw, borrow, repay, balance check
Amount: numerical value, "max", "all"
Currency: USDC, ETH, other supported tokens
Pool: cbassets, memecoin (auto-detected)
Purpose: Process user confirmation responses
Features:
Confirmation pattern recognition (yes, no, confirm, cancel)
Transaction state transition (confirming → confirmed)
Security confirmation - Always requires explicit user approval
Purpose: Generate blockchain transaction calldata
Tool: Blockchain Tool (web3.py) Features:
Smart contract interaction with DeFi protocols
Multi-step transaction sequencing (approve → supply)
Base mainnet calldata generation
ABI encoding for contract methods
Purpose: DeFi education and general assistance
Features:
LLM-powered responses for DeFi concepts
Educational content about lending, borrowing, yield farming
Fallback responses when LLM unavailable
Final Step - Formats all responses for client consumption
Output Format:
Persistent state across conversation turns
Transaction state storage in memory
Session cleanup for inactive users
Multi-user support with isolated sessions
From the user's standpoint, the flow of execution is fairly simple and follows this process:
User
External app user interacts with the application through a chat interface on the MAFIA web app.
MAFIA exposes API endpoints, thus, could be called by other applications or AI agents.
Smart Contract Wallet
ERC-4337 compatible account abstraction wallet
Stores user assets and executes validated UserOperations
Implements signature validation for:
ECDSA signatures (user-approved)
Session keys (AI-delegated)
Multi-sig requirements (high-value tx)
MAFIA
Brains behnd the operation.
Receives user intent from a chat messages.
Classifies this intent and creates an action that is returned to the users.
Can submit user operations to user's smart wallet for execution.
External Vaults
User operations created by MAFIA are submitted on chain to the respective DeFi protocol smart contracts for execution.
The end-user interacts with MAFIA through a chat interface and an smart contract wallet.