Documentation Index
Fetch the complete documentation index at: https://tonic-ai.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Design Patterns Overview
CreditNexus follows established design patterns to ensure maintainability, testability, and scalability. This document outlines the key patterns used throughout the codebase. Code Reference:.cursor/rules/architecture.mdc, .cursor/rules/service-layer.mdc
Dependency Injection Pattern
Overview
FastAPI’s dependency injection system is used throughout the application to manage dependencies and ensure testability. Code Reference:app/db/__init__.py, app/auth/jwt_auth.py, app/services/policy_service.py
Implementation
Key Principles
- ALWAYS use FastAPI’s
Depends()for dependency injection - ALWAYS create dependency functions in appropriate modules
- NEVER create database sessions directly - always use
get_db()dependency - NEVER access global state directly - inject dependencies
Service Layer Pattern
Overview
Business logic is encapsulated in service classes, separating concerns from API routes and database models. Code Reference:app/services/ directory
Implementation
Key Principles
- ALWAYS create service classes for business logic
- ALWAYS keep services stateless (no instance variables)
- ALWAYS inject dependencies into service constructors
- ALWAYS use service layer between API routes and database models
Repository Pattern (Implicit)
Overview
SQLAlchemy models serve as repositories, providing query methods and data access. Code Reference:app/db/models.py
Implementation
Key Principles
- Use SQLAlchemy models directly as repositories
- ALWAYS use
joinedload()for eager loading relationships - ALWAYS use query methods on models, not raw SQL
- ALWAYS handle transactions explicitly with
db.commit()/db.rollback()
Factory Pattern
Overview
Factory functions centralize object creation and configuration. Code Reference:app/core/llm_client.py, app/services/policy_engine_factory.py
Implementation
Key Principles
- ALWAYS use factory functions for creating complex objects
- ALWAYS centralize configuration in factory functions
- Examples:
get_chat_model(),get_policy_engine(),load_ssl_context()
Lifespan Management Pattern
Overview
FastAPI’s lifespan context manager handles application startup and shutdown. Code Reference:server.py (lifespan function)
Implementation
Key Principles
- ALWAYS use FastAPI’s
lifespancontext manager - ALWAYS initialize global resources in
lifespanfunction - ALWAYS clean up resources in shutdown phase
- ALWAYS handle initialization errors gracefully
CDM Event Pattern
Overview
All state changes and policy decisions are stored as CDM-compliant events. Code Reference:app/models/cdm_events.py
Implementation
Key Principles
- ALWAYS generate CDM events for state changes
- ALWAYS include required CDM fields:
eventType,eventDate,meta.globalKey - ALWAYS link events to transactions via
transaction_id - ALWAYS store events in database
Policy-as-Code Pattern
Overview
Policy rules are defined as YAML files and evaluated deterministically. Code Reference:app/policies/, app/services/policy_service.py
Implementation
Key Principles
- ALWAYS define policies as YAML files
- ALWAYS evaluate policies deterministically
- ALWAYS store policy decisions as CDM events
- ALWAYS handle three decision types:
ALLOW,BLOCK,FLAG
LLM Abstraction Pattern
Overview
LLM clients are abstracted to support multiple providers (OpenAI, vLLM, HuggingFace). Code Reference:app/core/llm_client.py
Implementation
Key Principles
- NEVER directly instantiate
ChatOpenAIorOpenAIEmbeddings - ALWAYS use
get_chat_model()andget_embeddings_model() - ALWAYS support multiple providers via configuration
- ALWAYS use provider-agnostic interfaces
Audit Trail Pattern
Overview
All state-changing operations are logged with audit trails. Code Reference:app/utils/audit.py
Implementation
Key Principles
- ALWAYS log audit actions for state changes
- ALWAYS include user ID and timestamp
- ALWAYS include relevant metadata
- ALWAYS link audit logs to entities
Error Handling Pattern
Overview
Consistent error handling with appropriate HTTP status codes and error messages. Code Reference:app/api/routes.py (error handling)
Implementation
Key Principles
- ALWAYS use
HTTPExceptionfor API errors - ALWAYS log errors before raising
- ALWAYS provide clear error messages
- ALWAYS use appropriate status codes
Additional Resources
Last Updated: 2026-01-14
Code Reference:
.cursor/rules/architecture.mdc, app/services/, app/core/llm_client.py