Building sandx.ai: A Multi-Agent Architecture for Autonomous AI Trading

A Multi-Agent Architecture for Autonomous AI Trading
Introduction
sandx.ai represents a new paradigm in AI trading: autonomous AI agents that collaborate like a professional hedge fund team. This article explores the six foundational technical decisions that make this architecture scalable, observable, and cost-effective, focusing purely on architectural concepts and design rationale.
1. Supervisor Agent and Sub-Agent System Design
The Hierarchical Orchestration Pattern
At the heart of sandx.ai is a supervisor-subordinate agent topology, modeled after real-world investment committee structures. The Chief Investment Officer (CIO) agent acts as the central orchestrator, while seven specialized analyst agents operate as domain experts.
The supervisor receive task and divide the task to be small tasks. It routes tasks to the most relevant sub-agent based on context and the agent skills.
Rather than relying on a single model's output, the CIO synthesizes insights from Technical, Fundamental, and Risk analysts. Voting conflict signals make a better decision.
2. Customized Logging Middleware: LangChain v1.0 for Session Restore and Streaming
Auto-Logging, Streaming, and Session Restore
Langchain V1.0 has a great feature which is middleware. Middle control and customize agent execution at every step. It provides a way to more tightly control what happens inside the agent. Middleware is useful for the following:
Tracking agent behavior with logging, analytics, and debugging.
At sandx.ai, we use the middleware feature to create our customized logging middleware to save logging to the database for Live Chat Streaming and Agent Session Restore

Langraph Logging Middleware For Session Restore and Streaming
3. Context Management with Prisma ORM
Type-Safe, Productive, Query-Powerful
In a multi-agent system, context is everything: market data, investment strategy, historical decisions, portfolio, current position and multi-agent message etc. sandx.ai uses Prisma ORM as the foundation for context management, delivering type safety, developer velocity, and query flexibility.
Prisma ORM make our context Type-Safe, Productive, Query-Powerful.
Type-Safe:
Compile-time validation: Prisma's generated types catch invalid field access, missing relations, or schema mismatches during development—not in production trading.
Safe refactoring: Catch error early before deploy to the production.
Schema as single source of truth: Context model changes automatically propagate type updates, preventing regressions when evolving agent logic
Type Hints: Accelerate Development
IDE autocomplete for context fields and relations reduces lookup time and speeds iteration.
Avoid writing complex SQL: With layer of Prisma ORM, writing better query without digging into complex SQL.
Simple CRUD: Efficient Context Lifecycle
Atomic updates: Trading requires atomic updates when buy or sell stocks.
Nested operations: Prisma handles relational writes seamlessly—persisting complex agent workflows without boilerplate.
4. Real-Time Market Data with Alpaca
Unified, High-Throughput Market Infrastructure
sandx.ai integrates Alpaca API as the single source of truth for market data and execution.
It consolidated all us exchanges with 9+ year historical data and real-time coverage. What makes it better is that high rate limits(10,000 API Calls/Min) support support large scale agent parallel running.
Integrating Alpaca, sandx.ai agents operate on consistent, low-latency, institutional-grade data, turning market complexity into actionable intelligence.
5. Real-Time UI Streaming with Redis
Low-Latency Messaging via Multi-Region Replication
sandx.ai uses Redis as the real-time messaging layer between backend agents and the frontend chat UI, delivering instant updates with minimal infrastructure complexity.

Multi-Region Replication for Global Low Latency

Regional Redis Replicas: Agent messages are stored in Redis instances replicated across multiple geographic regions. Users connect to their nearest replica, minimizing network round-trip time.
Automatic Failover: If a regional endpoint experiences issues, traffic routes to the next closest replica—ensuring consistent UI responsiveness worldwide.
Simple Polling, High Performance
Lightweight Polling: Instead of complex WebSocket management, the frontend polls Redis at short intervals (e.g., 1–2s). Redis's in-memory performance makes this approach efficient.
Stream Buffering: Protecting the Database
Write-Ahead Buffer: Agents write messages to Redis first; a background worker asynchronously persists to Prisma/PostgreSQL. This decouples real-time delivery from durable storage.
Reduced DB Load: High-frequency agent thoughts, tool calls, and status updates hit Redis, not the primary database, preventing query contention during active trading sessions.
6. AWS Batch for Cost-Effective, Scalable Agent Runtime
Right-Sizing Compute for I/O-Bound Workloads
AI agent workflows are fundamentally different from traditional compute tasks. sandx.ai leverages AWS Batch to align infrastructure costs with actual resource consumption.
I/O-Bound vs. CPU-Intensive
Waiting, Not Computing: Agents spend most cycles waiting on LLM responses, third-party API calls (Alpaca, OpenRouter), and database queries—not performing heavy local computation.
Idle Resource Waste: Traditional always-on servers or oversized containers pay for CPU cycles that sit idle during these wait periods.
Operational Efficiency
Event-Driven Scaling: Jobs queue and execute only when triggered (market events, user requests), scaling to zero during idle periods.
Managed Infrastructure: No Kubernetes clusters or EC2 fleets to manage—AWS Batch handles provisioning, scheduling, and termination automatically
Event Driven & Job Queuing
Automatic Queue Management: When triggered, jobs enter a managed queue. AWS Batch holds them there until the allocated compute resources become available.
On-Demand Invocation: The AWS Batch SDK allows the backend to instantly trigger agent jobs with specific parameters (e.g., ticker=AAPL) in response to real-time events like cron schedule, or user interactions.
Choosing agent-favor and event-driven solutions with job queuing solution allows sandx.ai to built a scalable and low cost agent-runtime solution for heavy multi-agent workflows.
Conclusion: Architecture as a Competitive Advantage
sandx.ai's technical architecture demonstrates that autonomous AI trading requires more than just powerful models. Success depends on:
Intelligent orchestration that mirrors human expertise structures
Observable middleware that builds trust through transparency
Robust context management that ensures decision consistency
Real-time communication that delivers interactive user experiences
Reliable market data that grounds AI reasoning in reality
Cost-aware compute that scales with workload demands
By thoughtfully integrating these six pillars, we've built a platform where AI agents don't just trade, they reason, collaborate, and evolve. The result is a system that combines the speed of algorithms with the wisdom of structured decision-making.
.