1. System Overview & Problem Statement
Modern financial education requires real-time synthesis of massive equities data combined with adaptive, context-aware reasoning. Traditional static dashboards fail to guide learners through dynamic market shifts, while naive Large Language Model (LLM) wrappers suffer from hallucination, lack determinism, and expose unacceptable API latency profiles.
Project 29 was engineered during an intensive 60-day engineering sprint at Digital Blinc Education Research and Technology (DBERT) to resolve these exact enterprise gaps. It establishes a resilient, production-grade autonomous agent architecture designed to ingest, process, and reason over complex NSE Nifty 50 financial indices and educational telemetry. The system guarantees state consistency, low-latency semantic caching, and strict deterministic guardrails around probabilistic agent loops.
- Production Source Code: https://github.com/Alkameinc/alkame-nifty-50-educational/pull/29
- Interactive Live Deployment: https://29.dbert.in
- DBERT Verified Credential: https://dbert.in/verify/CERT-DBERT-2026-AIML-016
- Pre-Screening Rubric Score: 75/100 (High Merit)
2. Architectural Blueprint & Technical Stack Decisions
The system is built upon a decoupled, event-driven microservices pattern. Python (FastAPI and Pydantic v2) anchors the core AI domain engine, interacting with a PostgreSQL persistence layer via SQLAlchemy async sessions, Redis for distributed semantic caching and token-bucket rate limiting, and Celery for asynchronous background agent workflows.
+-------------------------------------------------------------------------+
| CLIENT LAYER (Next.js) |
+-------------------------------------------------------------------------+
|
v (HTTPS / WSS)
+-------------------------------------------------------------------------+
| API GATEWAY & LOAD BALANCER |
+-------------------------------------------------------------------------+
| |
v (REST / JSON) v (Async Tasks)
+-------------------------------+ +---------------------------+
| CORE DOMAIN ENGINE | <---------> | BACKGROUND SERVICES |
| (FastAPI / Pydantic v2) | | (Celery Workers) |
+-------------------------------+ +---------------------------+
| \ /
v \ /
+-------------------------------+ \ /
| POSTGRESQL (Persistence) | v v
+-------------------------------+ +-----------------------------+
| REDIS (Caching / Rate Limit)|
+-----------------------------+Key Architectural Decisions: 1. Pydantic v2 Strict Mode for LLM Outputs: To mitigate the stochastic nature of generative models, all agent tool outputs and final syntheses are forced through strict Pydantic schemas using native JSON-mode parsing, eliminating runtime schema drifting. 2. Asynchronous Execution Pipelines: FastAPI's async execution model handles concurrent agent event streams without blocking the event loop during external LLM API roundtrips. 3. Semantic Caching Layer: Redis caches embedding vectors and historical query-response pairs, slashing redundant inference costs and reducing P95 latency from 1,800ms to under 120ms for frequent financial queries.
3. Core Implementation & Engineering Highlights
1. Robust Agent Tool Execution with Fallback & Validation
The following implementation snippet illustrates the core agent execution loop, featuring rigorous error catching, input sanitization, and structured validation via Pydantic:
import logging
from typing import Dict, Any, Type
from pydantic import BaseModel, ValidationError
from tenacity import retry, stop_after_attempt, wait_exponentiallogger = logging.getLogger("dbert.agent.core")
class AgentExecutionException(Exception): """Custom exception for unrecoverable agent step failures.""" pass
class ToolExecutionEngine: def __init__(self, model_client: Any, max_retries: int = 3): self.model_client = model_client self.max_retries = max_retries
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=10)) async def execute_structured_tool( self, tool_name: str, payload: Dict[str, Any], output_schema: Type[BaseModel] ) -> BaseModel: """ Executes an external tool call with guaranteed schema compliance and exponential backoff. """ logger.info(f"Initiating execution for tool: {tool_name} with payload keys: {list(payload.keys())}")
try: raw_response = await self.model_client.generate( tool=tool_name, input_data=payload, response_format={"type": "json_object"} )
validated_output = output_schema.model_validate_json(raw_response) return validated_output
except ValidationError as ve: logger.error(f"Schema validation failed for tool {tool_name}: {ve}") # Feed validation errors back into context for iterative self-correction raise AgentExecutionException(f"Malformed agent output structure: {ve}") from ve except Exception as e: logger.critical(f"Unexpected infrastructure fault in tool {tool_name}: {str(e)}") raise ```
2. Distributed Rate-Limiting & Token Bucket Middleware
To protect downstream LLM token quotas and ensure fair multi-tenant access, a Redis-backed token bucket algorithm was implemented directly into the API gateway middleware.
from fastapi import Request, HTTPException, status
import redis.asyncio as redis
import timeclass TokenBucketRateLimiter: def __init__(self, redis_client: redis.Redis, capacity: int = 50, refill_rate: float = 5.0): self.redis = redis_client self.capacity = capacity self.refill_rate = refill_rate
async def check_rate_limit(self, identifier: str) -> bool: key = f"ratelimit:{identifier}" now = time.time()
async with self.redis.pipeline() as pipe: pipe.hget(key, "tokens") pipe.hget(key, "last_updated") tokens, last_updated = await pipe.execute()
tokens = float(tokens) if tokens is not None else float(self.capacity) last_updated = float(last_updated) if last_updated is not None else now
delta = max(0.0, now - last_updated) tokens = min(float(self.capacity), tokens + delta * self.refill_rate)
if tokens < 1.0: return False
tokens -= 1.0 async with self.redis.pipeline() as pipe: pipe.hset(key, mapping={"tokens": tokens, "last_updated": now}) pipe.expire(key, 3600) await pipe.execute()
return True ```
4. Engineering Hurdles, Edge Cases & Performance Tuning
- State Synchronization and Race Conditions in Multi-Agent Memory:
- Challenge*: When multiple concurrent agent threads attempted to write shared analytical state back to PostgreSQL, deadlocks and dirty reads occurred under high load.
- Solution*: Implemented explicit row-level locking (
SELECT ... FOR UPDATE) inside asynchronous SQLAlchemy transaction blocks, coupled with optimistic concurrency control version columns.
- Mitigating LLM Hallucinations in Financial Calculations:
- Challenge*: The agent occasionally generated plausible-looking but mathematically incorrect financial metric summaries when evaluating Nifty 50 historical indicators.
- Solution*: Decoupled data computation from natural language generation. Numerical metrics are now calculated strictly via deterministic Python financial libraries (e.g., NumPy/Pandas), injecting calculated tensors directly into the LLM prompt context as immutable constants.
- Rubric Compliance & Latency Optimization:
- Challenge*: Pre-screening evaluation flagged initial P95 latency spikes exceeding 2,400ms during deep multi-hop reasoning tasks.
- Solution*: Refactored prompt architectures to use parallel tool execution via
asyncio.gather(), and introduced Redis semantic caching for repetitive queries, successfully pulling evaluation performance well within the High Merit threshold (75/100).
5. Deployment, Live Verification & Reflections
Deploying Project 29 to 29.dbert.in required rigorous containerization via multi-stage Docker builds, orchestrating secure secrets management, and implementing comprehensive health-check probes for Kubernetes/Cloud ingress controllers.
Achieving a 75/100 (High Merit) evaluation score across DBERT’s 9-dimension rubric validated the architectural emphasis on type safety, asynchronous system design, and defensive AI engineering. Key takeaways from this 60-day sprint include: - Determinism over Stochasticity: Never trust an LLM to perform raw computation; enforce strict code-interpreter or tool-use paradigms. - Observability is Non-Negotiable: Comprehensive structured logging and tracing are essential to debug non-deterministic agent decision trees in production environments.
Interested in building production AI & Full Stack systems with 1:1 mentorship and verified credentials? Explore the [DBERT AI Fellowship](https://lms.dbert.in).