The Challenge
When implementing billing for a multi-tenant platform, several requirements emerged: Financial Integrity Requirements:- Every transaction must balance (debits = credits)
- Complete audit trail for compliance
- Support for multiple legal entities
- Fiscal period management for reporting
- Capsule-isolated financial data
- Event-sourced architecture
- DynamoDB single-table design
- Multi-tenant isolation guarantees
- Zero data leakage between capsules
- GAAP/IFRS compatibility
- Financial statement generation
- Period closing and reconciliation
- Multi-currency support foundation
The Solution
We implemented a complete accounting foundation using double-entry bookkeeping principles with four core domain models:1. Legal Entity
Every billing operation happens within a legal entity context:2. Chart of Accounts
The chart of accounts defines the financial structure:- 1000: Cash
- 1100: Accounts Receivable
- 1200: Undeposited Funds
- 2000: Accounts Payable
- 2100: Deferred Revenue
- 2200: Sales Tax Payable
- 4000: Subscription Revenue
- 4100: Usage Revenue
- 4200: Professional Services
- 5000: Cost of Goods Sold
- 5100: Operating Expenses
- 5200: Payment Processing Fees
- 3000: Owner’s Equity
- 3100: Retained Earnings
3. Journal Entries
All financial transactions are recorded as journal entries with balanced debits and credits:4. Accounting Periods
Fiscal periods enable financial reporting and period closing:Example: Recording a Subscription Payment
When a customer pays for a subscription:Event Sourcing Integration
Journal entries emit domain events for auditability:AI Agent Workflow
The accounting foundation required careful planning due to financial domain complexity: Evaluator Phase (Planning):- Researched double-entry bookkeeping principles
- Designed chart of accounts structure for SaaS
- Mapped business requirements to accounting concepts
- Planned journal entry validation rules
- Generated domain models with proper validation
- Implemented account balance calculations
- Created bootstrap logic for standard accounts
- Built repository layer with DynamoDB patterns
- 15+ Level 1 unit tests for balance validation
- 8 Level 2 integration tests for journal workflows
- Test scenarios for period closing
- Multi-entity isolation verification
- Pattern implementation - Double-entry rules are well-documented
- Systematic validation - Clear invariants (debits = credits)
- Repository boilerplate - Standard DynamoDB patterns
- Test generation - Financial scenarios are formulaic
- Account structure - Which accounts for SaaS billing
- Revenue recognition - When to recognize vs defer revenue
- Period closing rules - Fiscal calendar decisions
Implementation Scale
Commit: 1430bb7 Scope: 6,862 lines changed across 95 files Core Domain Models:- LegalEntity (643 lines)
- ChartOfAccounts (783 lines)
- JournalEntry (745 lines)
- AccountingPeriod (705 lines)
- Repository implementations for all models
- DynamoDB schema with GSI for queries
- Event handlers for balance updates
- Bootstrap logic for default accounts
- 15+ unit tests covering validation rules
- 8 integration tests for journal workflows
- Period closing test scenarios
- Multi-tenant isolation tests
Key Learnings
1. Domain Modeling Matters
Proper accounting abstractions made complex requirements simple:- Journal entries enforce balance invariants
- Account types drive financial statement classification
- Periods enable time-based reporting
2. Event Sourcing Fits Accounting
Financial transactions are naturally event-sourced:- Every entry is immutable
- Complete audit trail by default
- Temporal queries for period reporting
- Reconciliation through event replay
3. Validation at Domain Boundary
Financial integrity rules belong in the domain:4. Bootstrap for Consistency
Default chart of accounts ensures consistency:- Every legal entity starts with standard accounts
- Account numbers are predictable
- Financial reports work immediately
- Customization happens through extension
5. AI Handles Systematic Implementation
The AI excelled at implementing well-defined patterns:- Repository boilerplate
- Balance calculation logic
- Test scenario generation
- Validation rule encoding
Results
Development Speed:- Full accounting foundation in single commit
- 95 files with consistent patterns
- Comprehensive test coverage
- Estimated 5-7x faster than manual implementation
- Zero unbalanced entries possible
- Complete audit trail through events
- Period closing prevents backdating
- Multi-tenant isolation enforced
- Foundation supports revenue recognition
- Ready for tax calculation integration
- Multi-currency expansion path
- Financial reporting infrastructure
- GAAP-compatible structure
- Audit trail for all transactions
- Period-based reporting
- Multi-jurisdiction support