# badrinath0526/medical-bill-extraction- architecture

> Architecture diagram of the GitHub repository badrinath0526/medical-bill-extraction-, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/badrinath0526/medical-bill-extraction-
- Repository: https://github.com/badrinath0526/medical-bill-extraction-

## Overview

This platform turns a user-uploaded medical billing PDF into structured billing records and flagged rows. The user signs in, uploads a PDF, follows its processing job, and views the result. The backend API manages authenticated jobs; a worker claims jobs and delegates extraction, whose AI stage returns records and metrics for persistence. PostgreSQL enforces per-user isolation through RLS. The README documents the frontend/backend/auth deployment boundaries; some frontend and job-service wiring is not visible in excerpts, so uncertain internal links are omitted.

Main entry points: [`backend/app/service/extraction_service.py`](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/service/extraction_service.py) (Job extraction), [`backend/app/api/main.py`](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/api/main.py) (FastAPI application), [`backend/app/core/context_manager.py`](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/core/context_manager.py) (RLS session manager), [`frontend/lib/api.ts`](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/lib/api.ts) (API client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Web application"]
  node_web_auth["Sign-in and sign-up<br/>[page.tsx]"]
  node_web_upload["PDF upload<br/>[page.tsx]"]
  node_web_jobs["Job list<br/>[page.tsx]"]
  node_web_detail["Job results<br/>[page.tsx]"]
  node_web_api["API client<br/>[api.ts]"]
  node_auth_service["Better Auth<br/>[auth.ts]"]
end

subgraph group_api["Job API"]
  node_backend_api["FastAPI application<br/>[main.py]"]
  node_job_routes["Job routes<br/>[jobs.py]"]
  node_session_validation["Session validation<br/>[current_user.py]"]
end

subgraph group_processing["Worker and extraction"]
  node_worker["Job worker loop<br/>[loop.py]"]
  node_job_service["Job management<br/>[job_service.py]"]
  node_extraction_service["Job extraction"]
end

subgraph group_ai["AI pipeline"]
  node_pdf_loader["PDF loading<br/>[pdf_loader.py]"]
  node_run_context["Document run context<br/>[context.py]"]
  node_orchestrator["Extraction orchestrator<br/>[orchestrator.py]"]
  node_billing_agent["Billing extraction agent<br/>[executor.py]"]
  node_extraction_models["Records and flags<br/>[extraction.py]"]
end

subgraph group_persistence["Persistence and isolation"]
  node_dao["Job data access<br/>[job_dao.py]"]
  node_context_manager["RLS session manager<br/>[context_manager.py]"]
  node_postgres_provider["PostgreSQL provider<br/>[postgres.py]"]
  node_postgres[("PostgreSQL with RLS")]
end

node_user(("User"))
node_openai["OpenAI"]

node_user -->|"signs in"| node_web_auth
node_user -->|"uploads PDF"| node_web_upload
node_user -->|"views jobs"| node_web_jobs
node_user -->|"views results"| node_web_detail
node_web_auth -.->|"authenticates with"| node_auth_service
node_web_upload -.->|"submits job"| node_web_api
node_web_jobs -.->|"requests jobs"| node_web_api
node_web_detail -.->|"requests result"| node_web_api
node_web_api -->|"calls API"| node_backend_api
node_auth_service -.->|"provides session"| node_backend_api
node_backend_api -->|"registers routes"| node_job_routes
node_job_routes -->|"uses dependency"| node_session_validation
node_backend_api -->|"initializes"| node_context_manager
node_worker -->|"claims and recovers"| node_job_service
node_worker -->|"processes job"| node_extraction_service
node_job_routes -.->|"delegates job actions"| node_job_service
node_extraction_service -->|"loads and updates job"| node_dao
node_extraction_service -->|"loads PDF"| node_pdf_loader
node_extraction_service -->|"builds context"| node_run_context
node_extraction_service -->|"runs extraction"| node_orchestrator
node_orchestrator -->|"runs billing stage"| node_billing_agent
node_orchestrator -->|"returns records"| node_extraction_models
node_billing_agent -.->|"calls model"| node_openai
node_dao -->|"opens sessions"| node_context_manager
node_context_manager -->|"creates sessions"| node_postgres_provider
node_postgres_provider -.->|"connects"| node_postgres
node_context_manager -->|"sets RLS identity"| node_postgres

click node_web_auth "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/app/sign-in/page.tsx"
click node_web_upload "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/app/upload/page.tsx"
click node_web_jobs "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/app/jobs/page.tsx"
click node_web_detail "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/app/jobs/%5Bid%5D/page.tsx"
click node_web_api "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/lib/api.ts"
click node_auth_service "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/lib/auth.ts"
click node_backend_api "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/api/main.py"
click node_job_routes "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/api/routes/jobs.py"
click node_session_validation "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/api/dependencies/current_user.py"
click node_worker "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/worker/loop.py"
click node_job_service "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/service/job_service.py"
click node_extraction_service "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/service/extraction_service.py"
click node_pdf_loader "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/ai/pdf_loader.py"
click node_run_context "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/ai/context.py"
click node_orchestrator "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/ai/orchestrator.py"
click node_billing_agent "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/ai/agents/billing/executor.py"
click node_extraction_models "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/models/extraction.py"
click node_dao "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/dao/pg/job_dao.py"
click node_context_manager "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/core/context_manager.py"
click node_postgres_provider "https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/core/database/postgres.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_web_auth,node_web_upload,node_web_jobs,node_web_detail,node_web_api,node_auth_service,node_user toneBlue
class node_backend_api,node_job_routes,node_session_validation toneAmber
class node_worker,node_job_service,node_extraction_service toneMint
class node_pdf_loader,node_run_context,node_orchestrator,node_billing_agent,node_extraction_models toneRose
class node_dao,node_context_manager,node_postgres_provider,node_postgres,node_openai toneIndigo
```

## Components

### Web application

- [Sign-in and sign-up](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/app/sign-in/page.tsx): `frontend/app/sign-in/page.tsx`
- [PDF upload](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/app/upload/page.tsx): `frontend/app/upload/page.tsx`
- [Job list](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/app/jobs/page.tsx): `frontend/app/jobs/page.tsx`
- [Job results](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/app/jobs/%5Bid%5D/page.tsx): `frontend/app/jobs/[id]/page.tsx`
- [API client](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/lib/api.ts): `frontend/lib/api.ts`
- [Better Auth](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/frontend/lib/auth.ts): `frontend/lib/auth.ts`

### Job API

- [FastAPI application](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/api/main.py): `backend/app/api/main.py`
- [Job routes](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/api/routes/jobs.py): `backend/app/api/routes/jobs.py`
- [Session validation](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/api/dependencies/current_user.py): `backend/app/api/dependencies/current_user.py`

### Worker and extraction

- [Job worker loop](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/worker/loop.py): `backend/app/worker/loop.py`
- [Job management](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/service/job_service.py): `backend/app/service/job_service.py`
- [Job extraction](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/service/extraction_service.py): `backend/app/service/extraction_service.py`

### AI pipeline

- [PDF loading](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/ai/pdf_loader.py): `backend/app/ai/pdf_loader.py`
- [Document run context](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/ai/context.py): `backend/app/ai/context.py`
- [Extraction orchestrator](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/ai/orchestrator.py): `backend/app/ai/orchestrator.py`
- [Billing extraction agent](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/ai/agents/billing/executor.py): `backend/app/ai/agents/billing/executor.py`
- [Records and flags](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/models/extraction.py): `backend/app/models/extraction.py`

### Persistence and isolation

- [Job data access](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/dao/pg/job_dao.py): `backend/app/dao/pg/job_dao.py`
- [RLS session manager](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/core/context_manager.py): `backend/app/core/context_manager.py`
- [PostgreSQL provider](https://github.com/badrinath0526/medical-bill-extraction-/blob/main/backend/app/core/database/postgres.py): `backend/app/core/database/postgres.py`
- **PostgreSQL with RLS**

### Other components

- **User**
- **OpenAI**

## Connections

- User → Sign-in and sign-up: signs in (evidence: `README.md`)
- User → PDF upload: uploads PDF (evidence: `README.md`)
- User → Job list: views jobs (evidence: `README.md`)
- User → Job results: views results (evidence: `README.md`)
- Sign-in and sign-up → Better Auth: authenticates with
- PDF upload → API client: submits job
- Job list → API client: requests jobs
- Job results → API client: requests result
- API client → FastAPI application: calls API (evidence: `README.md`)
- Better Auth → FastAPI application: provides session
- FastAPI application → Job routes: registers routes (evidence: `backend/app/api/main.py`)
- Job routes → Session validation: uses dependency (evidence: `backend/app/api/routes/jobs.py`)
- FastAPI application → RLS session manager: initializes (evidence: `backend/app/api/main.py`)
- Job worker loop → Job management: claims and recovers (evidence: `backend/app/worker/loop.py`)
- Job worker loop → Job extraction: processes job (evidence: `backend/app/worker/loop.py`)
- Job routes → Job management: delegates job actions
- Job extraction → Job data access: loads and updates job (evidence: `backend/app/service/extraction_service.py`)
- Job extraction → PDF loading: loads PDF (evidence: `backend/app/service/extraction_service.py`)
- Job extraction → Document run context: builds context (evidence: `backend/app/service/extraction_service.py`)
- Job extraction → Extraction orchestrator: runs extraction (evidence: `backend/app/service/extraction_service.py`)
- Extraction orchestrator → Billing extraction agent: runs billing stage (evidence: `backend/app/ai/orchestrator.py`)
- Extraction orchestrator → Records and flags: returns records (evidence: `backend/app/ai/orchestrator.py`)
- Billing extraction agent → OpenAI: calls model
- Job data access → RLS session manager: opens sessions (evidence: `backend/app/dao/pg/job_dao.py`)
- RLS session manager → PostgreSQL provider: creates sessions (evidence: `backend/app/core/context_manager.py`)
- PostgreSQL provider → PostgreSQL with RLS: connects
- RLS session manager → PostgreSQL with RLS: sets RLS identity (evidence: `backend/app/core/context_manager.py`)
