# ritam-05/finmate-ai-powered-financial-advisor architecture

> Architecture diagram of the GitHub repository ritam-05/finmate-ai-powered-financial-advisor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ritam-05/finmate-ai-powered-financial-advisor
- Repository: https://github.com/ritam-05/finmate-ai-powered-financial-advisor

## Overview

1. **High-level shape**
   This repo is a two-tier product: a **Next.js frontend** deployed on Vercel and a **FastAPI backend** deployed on Hugging Face Spaces. The frontend owns all user interaction and local persistence; the backend owns LLM calls and response normalization.

2. **Frontend application shell**
   The frontend uses the **Next.js App Router** under `src/app`. There are two route groups:
   - `src/app/(marketing)` for the public landing page
   - `src/app/(app)` for the authenticated-like product flow: questionnaire and results
   `src/app/layout.tsx` and `src/app/globals.css` provide the shared application shell and global styling.

3. **Primary user flow**
   The core experience is a linear flow:
   - user starts on the marketing page
   - completes the questionnaire
   - frontend submits answers to the backend API
   - backend returns a structured financial plan
   - results page renders the plan and educational explanations
   The important architectural boundary is that **planning happens server-side**, while **presentation and state orchestration happen client-side**.

4. **Feature-based FinMate module**
   Most product logic lives under `src/features/finmate`. This is where the domain-specific UI, hooks, and utilities are grouped together:
   - questionnaire components and hook
   - results components and hook
   - term-detection and explanation utilities
   - questionnaire/insights/terms helpers
   This makes FinMate a self-contained feature slice rather than spreading domain logic across the app.

5. **Questionnaire subsystem**
   `src/features/finmate/components/questionnaire/questionnaire-form.tsx` is the main data-entry UI. The questionnaire is described in the README as collecting structured answers plus a free-text occupation field. `use-questionnaire.ts` and `utils/questionnaire.ts` likely coordinate form state, validation, and normalization before submission.

6. **Results subsystem**
   The results experience is split between `results-client.tsx` and `terms-section.tsx`, with `use-results.ts` supporting data loading/state and `utils/insights.ts` / `utils/terms.ts` shaping the output for display. The results page is not just a static summary: it also expands financial terms into a learning layer, so the UI includes both advice and education.

7. **API boundary and backend responsibility**
   The frontend talks to the backend through `src/lib/api/finmate.ts`, using the public URL configured via `NEXT_PUBLIC_API_URL` from `src/lib/config/finmate.ts` and `src/lib/config/frontend.ts`. The backend is a single FastAPI entrypoint at `backend/app.py`, which exposes:
   - `GET /` for health
   - `POST /generate` for personalized financial advice
   - `POST /terms/explain` for term explanations
   This boundary is important for the architecture diagram: the browser never calls the LLM directly.

8. **LLM and provider integration**
   The backend is the only place that integrates with Groq’s OpenAI-compatible API. The README indicates a primary/fallback model strategy and request timeout handling. From an architecture perspective, the backend is acting as an **LLM orchestration and sanitization layer**, returning strict JSON to protect the UI from malformed model output.

9. **State and persistence**
   The app uses **browser-local persistence** for questionnaire answers and generated results, implemented via `src/lib/storage.ts`. This means refresh safety is handled on the client, not through a database or user account system. There is no evidence of server-side persistence in the repo.

10. **Shared UI and design system**
   Reusable presentation components live in `src/components`, including layout primitives (`container`, `navbar`, `theme-toggle`) and generic UI controls (`button`, `card`, `input`, `progress`, etc.). Styling is a mix of Tailwind config, `src/styles/theme.css`, and global CSS, so the visual system is centralized and reusable across both marketing and app routes.

11. **Runtime/configuration boundaries**
   Runtime configuration is intentionally split:
   - frontend: `NEXT_PUBLIC_API_URL`
   - backend: `GROQ_API_KEY`, model names, request timeout
   The deployment model depends on these environment variables being set correctly in Vercel and Hugging Face Spaces. This separation is part of the security model: secrets stay on the backend.

12. **What to show in an architecture diagram**
   A correct diagram should show:
   - browser user interacting with Next.js UI
   - Next.js pages/components and feature hooks managing questionnaire/results state
   - browser-local storage for persistence
   - outbound HTTP calls from frontend to FastAPI backend
   - backend calling Groq
   - backend returning normalized JSON and term explanations
   The main trust boundary is between browser/frontend and backend; the main computation boundary is between backend and external LLM provider.

Main entry points: [`src/lib/api/finmate.ts`](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/lib/api/finmate.ts) (API client), [`backend/app.py`](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/backend/app.py) (FastAPI app), [`src/features/finmate/hooks/use-questionnaire.ts`](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/features/finmate/hooks/use-questionnaire.ts) (Questionnaire state), [`src/features/finmate/components/results/results-client.tsx`](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/features/finmate/components/results/results-client.tsx) (Results client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Next.js frontend"]
  node_browser(("Browser<br/>runtime"))
  node_app_shell["App shell<br/>nextjs shell<br/>[layout.tsx]"]
  node_marketing_page["Marketing page<br/>route<br/>[page.tsx]"]
  node_questionnaire_page["Questionnaire page<br/>route<br/>[page.tsx]"]
  node_results_page["Results page<br/>route<br/>[page.tsx]"]
  node_finmate_feature{{"FinMate slice<br/>feature module"}}
  node_questionnaire_form["Questionnaire form<br/>ui form"]
  node_questionnaire_hook["Questionnaire state<br/>hook"]
  node_results_client["Results client<br/>ui controller<br/>[results-client.tsx]"]
  node_terms_section["Terms section<br/>education ui<br/>[terms-section.tsx]"]
  node_results_hook["Results state<br/>hook<br/>[use-results.ts]"]
  node_domain_utils["Domain utils<br/>transform helpers"]
end

subgraph group_backend["FastAPI backend"]
  node_backend_app["FastAPI app<br/>api service<br/>[app.py]"]
  node_groq_api(("Groq API<br/>external LLM"))
end

subgraph group_shared["Shared UI and lib"]
  node_api_client["API client<br/>http client<br/>[finmate.ts]"]
  node_runtime_config["Runtime config<br/>env config<br/>[finmate.ts]"]
  node_storage[("Local storage<br/>browser persistence<br/>[storage.ts]")]
  node_shared_ui["Shared UI<br/>component library"]
end

node_user(("User<br/>actor"))

node_user -->|"uses"| node_browser
node_browser -->|"opens"| node_marketing_page
node_marketing_page -->|"navigates"| node_questionnaire_page
node_questionnaire_page -->|"renders"| node_questionnaire_form
node_questionnaire_form -->|"uses"| node_questionnaire_hook
node_questionnaire_hook -->|"persists"| node_storage
node_questionnaire_hook -->|"submits"| node_api_client
node_api_client -->|"reads"| node_runtime_config
node_api_client -->|"POST /generate"| node_backend_app
node_backend_app -->|"calls"| node_groq_api
node_backend_app -->|"returns JSON"| node_api_client
node_questionnaire_hook -->|"routes"| node_results_page
node_results_page -->|"renders"| node_results_client
node_results_client -->|"uses"| node_results_hook
node_results_client -->|"includes"| node_terms_section
node_results_hook -->|"reads/writes"| node_storage
node_results_hook -->|"fetches"| node_api_client
node_api_client -->|"POST /terms/explain"| node_backend_app
node_backend_app -.->|"formats for UI"| node_shared_ui
node_app_shell -->|"wraps"| node_shared_ui
node_finmate_feature -->|"contains"| node_questionnaire_form
node_finmate_feature -->|"contains"| node_results_client
node_finmate_feature -->|"contains"| node_domain_utils
node_browser -->|"loads"| node_app_shell

click node_app_shell "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/app/layout.tsx"
click node_marketing_page "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/app/(marketing)/page.tsx"
click node_questionnaire_page "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/app/(app)/questionnaire/page.tsx"
click node_results_page "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/app/(app)/results/page.tsx"
click node_finmate_feature "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/tree/main/src/features/finmate"
click node_questionnaire_form "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/features/finmate/components/questionnaire/questionnaire-form.tsx"
click node_questionnaire_hook "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/features/finmate/hooks/use-questionnaire.ts"
click node_results_client "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/features/finmate/components/results/results-client.tsx"
click node_terms_section "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/features/finmate/components/results/terms-section.tsx"
click node_results_hook "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/features/finmate/hooks/use-results.ts"
click node_domain_utils "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/tree/main/src/features/finmate/utils"
click node_api_client "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/lib/api/finmate.ts"
click node_runtime_config "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/lib/config/finmate.ts"
click node_storage "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/lib/storage.ts"
click node_shared_ui "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/tree/main/src/components"
click node_backend_app "https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/backend/app.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser,node_app_shell,node_marketing_page,node_questionnaire_page,node_results_page,node_finmate_feature,node_questionnaire_form,node_questionnaire_hook,node_results_client,node_terms_section,node_results_hook,node_domain_utils toneBlue
class node_backend_app,node_groq_api toneAmber
class node_api_client,node_runtime_config,node_storage,node_shared_ui toneMint
class node_user toneNeutral
```

## Components

### Next.js frontend

Browser-facing app, feature UI, client state, and local persistence.

- **Browser**: runtime · Runs the Next.js UI and stores local state.
- [App shell](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/app/layout.tsx): nextjs shell · Shared layout and global styling for the app. · `src/app/layout.tsx`
- [Marketing page](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/app/(marketing)/page.tsx): route · Public landing page and entry point. · `src/app/(marketing)/page.tsx`
- [Questionnaire page](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/app/(app)/questionnaire/page.tsx): route · Product flow entry for collecting user answers. · `src/app/(app)/questionnaire/page.tsx`
- [Results page](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/app/(app)/results/page.tsx): route · Displays the generated plan and explanations. · `src/app/(app)/results/page.tsx`
- [FinMate slice](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/tree/main/src/features/finmate): feature module · Domain-specific questionnaire, results, and explanation logic. · `src/features/finmate`
- [Questionnaire form](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/features/finmate/components/questionnaire/questionnaire-form.tsx): ui form · Main data-entry interface for structured answers and occupation text. · `src/features/finmate/components/questionnaire/questionnaire-form.tsx`
- [Questionnaire state](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/features/finmate/hooks/use-questionnaire.ts): hook · Manages form state, validation, and submission prep. · `src/features/finmate/hooks/use-questionnaire.ts`
- [Results client](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/features/finmate/components/results/results-client.tsx): ui controller · Orchestrates result loading, rendering, and local state. · `src/features/finmate/components/results/results-client.tsx`
- [Terms section](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/features/finmate/components/results/terms-section.tsx): education ui · Expands financial terms into a learning layer. · `src/features/finmate/components/results/terms-section.tsx`
- [Results state](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/features/finmate/hooks/use-results.ts): hook · Supports results fetching, normalization, and display state. · `src/features/finmate/hooks/use-results.ts`
- [Domain utils](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/tree/main/src/features/finmate/utils): transform helpers · Shapes questionnaire, insights, and term data for the UI. · `src/features/finmate/utils`

### FastAPI backend

Server-side planning and term explanation API.

- [FastAPI app](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/backend/app.py): api service · Server entrypoint for generation and term explanations. · `backend/app.py`
- **Groq API**: external LLM · Model provider used for advice generation and explanation.

### Shared UI and lib

Reusable components, client API, config, and storage.

- [API client](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/lib/api/finmate.ts): http client · Frontend bridge to backend endpoints. · `src/lib/api/finmate.ts`
- [Runtime config](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/lib/config/finmate.ts): env config · Public API base URL for the frontend. · `src/lib/config/finmate.ts`
- [Local storage](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/blob/main/src/lib/storage.ts): browser persistence · Persists questionnaire answers and generated results in the browser. · `src/lib/storage.ts`
- [Shared UI](https://github.com/ritam-05/finmate-ai-powered-financial-advisor/tree/main/src/components): component library · Reusable layout and form primitives used across routes. · `src/components`

### Other components

- **User**: actor · Starts on marketing, answers questionnaire, reviews results.

## Connections

- User → Browser: uses · Interacts with the web app in the browser.
- Browser → Marketing page: opens · Starts from the public landing page.
- Marketing page → Questionnaire page: navigates · Moves into the main product flow.
- Questionnaire page → Questionnaire form: renders · Mounts the FinMate questionnaire UI.
- Questionnaire form → Questionnaire state: uses · Delegates form state and submission prep.
- Questionnaire state → Local storage: persists · Saves answers for refresh safety.
- Questionnaire state → API client: submits · Sends normalized answers to the backend.
- API client → Runtime config: reads · Uses the public backend base URL.
- API client → FastAPI app: POST /generate · Requests the personalized financial plan.
- FastAPI app → Groq API: calls · Orchestrates model requests and fallback handling.
- FastAPI app → API client: returns JSON · Responds with normalized plan data.
- Questionnaire state → Results page: routes · Advances into the results experience after submission.
- Results page → Results client: renders · Mounts the results controller UI.
- Results client → Results state: uses · Coordinates results loading and state.
- Results client → Terms section: includes · Shows educational term explanations alongside advice.
- Results state → Local storage: reads/writes · Restores and caches generated results locally.
- Results state → API client: fetches · Requests plan or explanation data when needed.
- API client → FastAPI app: POST /terms/explain · Requests term explanations from the server.
- FastAPI app → Shared UI: formats for UI · Returns responses intended for the frontend renderer.
- App shell → Shared UI: wraps · Provides global layout and styling around reusable UI.
- FinMate slice → Questionnaire form: contains · Groups the questionnaire slice.
- FinMate slice → Results client: contains · Groups the results slice.
- FinMate slice → Domain utils: contains · Holds domain-specific transforms and explainers.
- Browser → App shell: loads · Bootstraps the shared app shell.
