# srungavruksham/bank-statement-extractor architecture

> Architecture diagram of the GitHub repository srungavruksham/bank-statement-extractor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/srungavruksham/bank-statement-extractor
- Repository: https://github.com/srungavruksham/bank-statement-extractor

## Overview

#### Purpose and Entry Points
This is a Python/Streamlit bank-statement extraction system. It converts PDFs from `data/statements/` into structured customer, account, and transaction data; validates the result; captures lessons from failures; and supports human approval. The interactive entry point is `app.py`; orchestration is centered in `pipeline.py`.

#### UI and Human Review Boundary
`app.py` provides the Streamlit workflow: select an allowed statement, provide the bank name, run processing, inspect live stage results, and approve or reject output. Human decisions cross into durable workflow state through `review.py`, which writes approved records under `data/approved/`. Input access is intentionally bounded to `data/statements/`.

Paths: `app.py`, `review.py`, `data/approved/ef66dfd6-3848-4339-8f4d-770cbbd2e79f.json`

#### Pipeline Orchestration
`pipeline.py` coordinates stages and returns their combined result to the UI. Its principal flow is: safely read PDF text, retrieve bank-specific learned rules, run parallel extraction, merge fields, validate deterministic rules, and create/refine a reflection lesson on failure. This module is the control-plane boundary between UI, LLM agents, Python validation, memory, and telemetry.

Paths: `pipeline.py`, `utils.py`, `validation.py`

#### LLM Extraction and Merge
`crewai_extraction.py` implements the CrewAI/Gemini agent layer. Three parallel agents extract identity, account, and transaction data from statement text. A separate merge agent combines only smaller identity/account outputs; transaction rows are restored in Python rather than regenerated by the merge model, reducing truncation and transcription risk.

Prompt contracts are versioned and separated per role, with a registry resolving their active definitions.

Paths: `crewai_extraction.py`, `prompts/identity.yaml`, `prompts/account.yaml`, `prompts/transactions.yaml`, `prompts/merge.yaml`, `prompts/registry.py`

#### Deterministic Validation and Reflection
`validation.py` applies non-LLM checks such as required-field presence, balance arithmetic, and date consistency. This is a hard correctness boundary: model-produced structured values are checked by repeatable Python logic rather than another model call. Failures feed a reflection prompt that records the issue and avoidance guidance; known numeric strings such as `"$1,234.56"` remain a documented validation limitation.

Paths: `validation.py`, `prompts/reflection.yaml`, `utils.py`

#### Memory and Learning Loop
`memory/store.py` persists bank-scoped lessons across runs. Validation-originated lessons and human rejection feedback remain distinguishable by source. Repeated mistakes for a bank are promoted after two occurrences into trusted semantic rules, which are injected into future extraction/reflection context.

Paths: `memory/store.py`, `pipeline.py`, `review.py`

#### Observability and Runtime Dependencies
`telemetry/tracker.py` records per-agent LLM token use and cost, enabling the UI to expose execution evidence and spend. Runtime infrastructure is local Python storage plus filesystem directories; there is no indicated database or remote backend beyond Gemini access through CrewAI. Required external services/libraries include Streamlit, CrewAI with Google GenAI/Gemini, PyPDF2, dotenv-based environment configuration, and YAML prompt loading.

Paths: `telemetry/tracker.py`, `requirements.txt`, `app.py`

Main entry points: [`crewai_extraction.py`](https://github.com/srungavruksham/bank-statement-extractor/blob/main/crewai_extraction.py) (CrewAI extraction), [`pipeline.py`](https://github.com/srungavruksham/bank-statement-extractor/blob/main/pipeline.py) (Pipeline orchestrator), [`app.py`](https://github.com/srungavruksham/bank-statement-extractor/blob/main/app.py) (Streamlit app), [`memory/store.py`](https://github.com/srungavruksham/bank-statement-extractor/blob/main/memory/store.py) (Bank lesson store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["UI &amp; Review"]
  node_app["Streamlit app<br/>UI entry point<br/>[app.py]"]
  node_statements["Statement PDFs<br/>bounded input directory"]
  node_review["Review workflow<br/>human approval<br/>[review.py]"]
  node_approved["Approved records<br/>durable JSON output"]
end

subgraph group_pipeline["Extraction Pipeline"]
  node_pipeline_orchestrator["Pipeline orchestrator<br/>control plane<br/>[pipeline.py]"]
  node_pdf_reader["Safe PDF text reader<br/>PDF ingestion<br/>[utils.py]"]
  node_crew_extraction["CrewAI extraction<br/>LLM agent layer"]
  node_parallel_agents["Role extractors<br/>parallel LLM agents"]
  node_merge["Merge and restore rows<br/>Python-backed merge"]
  node_prompt_contracts["Prompt contracts<br/>versioned YAML prompts<br/>[identity.yaml]"]
  node_prompt_registry["Prompt registry<br/>prompt resolver<br/>[registry.py]"]
  node_validation["Deterministic validation<br/>Python correctness boundary<br/>[validation.py]"]
  node_gemini{{"Gemini via CrewAI<br/>external LLM runtime<br/>[requirements.txt]"}}
end

subgraph group_learning["Learning &amp; Evidence"]
  node_memory_store[("Bank lesson store<br/>local learning store<br/>[store.py]")]
  node_reflection["Reflection lesson<br/>failure guidance<br/>[reflection.yaml]"]
  node_telemetry["LLM telemetry<br/>usage tracker<br/>[tracker.py]"]
end

node_statements -->|"select allowed PDF"| node_app
node_app -->|"run processing"| node_pipeline_orchestrator
node_pipeline_orchestrator -->|"read safely"| node_pdf_reader
node_pdf_reader -->|"statement text"| node_crew_extraction
node_pipeline_orchestrator -->|"retrieve bank rules"| node_memory_store
node_memory_store -->|"learned context"| node_crew_extraction
node_prompt_registry -->|"resolve active prompts"| node_prompt_contracts
node_prompt_contracts -->|"agent contracts"| node_crew_extraction
node_crew_extraction -->|"parallel extraction"| node_parallel_agents
node_parallel_agents -->|"structured outputs"| node_merge
node_gemini -->|"LLM inference"| node_crew_extraction
node_crew_extraction -->|"token use and cost"| node_telemetry
node_merge -->|"combined record"| node_validation
node_validation -->|"validated result"| node_pipeline_orchestrator
node_validation -.->|"on failure"| node_reflection
node_reflection -->|"validation lesson"| node_memory_store
node_pipeline_orchestrator -->|"live stage results"| node_app
node_app -->|"approve or reject"| node_review
node_review -->|"approved record"| node_approved
node_review -->|"rejection feedback"| node_memory_store
node_telemetry -->|"execution evidence"| node_app

click node_app "https://github.com/srungavruksham/bank-statement-extractor/blob/main/app.py"
click node_review "https://github.com/srungavruksham/bank-statement-extractor/blob/main/review.py"
click node_approved "https://github.com/srungavruksham/bank-statement-extractor/blob/main/data/approved/ef66dfd6-3848-4339-8f4d-770cbbd2e79f.json"
click node_pipeline_orchestrator "https://github.com/srungavruksham/bank-statement-extractor/blob/main/pipeline.py"
click node_pdf_reader "https://github.com/srungavruksham/bank-statement-extractor/blob/main/utils.py"
click node_crew_extraction "https://github.com/srungavruksham/bank-statement-extractor/blob/main/crewai_extraction.py"
click node_parallel_agents "https://github.com/srungavruksham/bank-statement-extractor/blob/main/crewai_extraction.py"
click node_merge "https://github.com/srungavruksham/bank-statement-extractor/blob/main/crewai_extraction.py"
click node_prompt_contracts "https://github.com/srungavruksham/bank-statement-extractor/blob/main/prompts/identity.yaml"
click node_prompt_registry "https://github.com/srungavruksham/bank-statement-extractor/blob/main/prompts/registry.py"
click node_validation "https://github.com/srungavruksham/bank-statement-extractor/blob/main/validation.py"
click node_memory_store "https://github.com/srungavruksham/bank-statement-extractor/blob/main/memory/store.py"
click node_reflection "https://github.com/srungavruksham/bank-statement-extractor/blob/main/prompts/reflection.yaml"
click node_telemetry "https://github.com/srungavruksham/bank-statement-extractor/blob/main/telemetry/tracker.py"
click node_gemini "https://github.com/srungavruksham/bank-statement-extractor/blob/main/requirements.txt"

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_app,node_statements,node_review,node_approved toneBlue
class node_pipeline_orchestrator,node_pdf_reader,node_crew_extraction,node_parallel_agents,node_merge,node_prompt_contracts,node_prompt_registry,node_validation,node_gemini toneAmber
class node_memory_store,node_reflection,node_telemetry toneMint
```

## Components

### UI & Review

- [Streamlit app](https://github.com/srungavruksham/bank-statement-extractor/blob/main/app.py): UI entry point · Selects statements, runs processing, shows results, and collects decisions. · `app.py`
- **Statement PDFs**: bounded input directory · Allowed source documents.
- [Review workflow](https://github.com/srungavruksham/bank-statement-extractor/blob/main/review.py): human approval · Persists approvals and records rejection feedback. · `review.py`
- [Approved records](https://github.com/srungavruksham/bank-statement-extractor/blob/main/data/approved/ef66dfd6-3848-4339-8f4d-770cbbd2e79f.json): durable JSON output · `data/approved/ef66dfd6-3848-4339-8f4d-770cbbd2e79f.json`

### Extraction Pipeline

- [Pipeline orchestrator](https://github.com/srungavruksham/bank-statement-extractor/blob/main/pipeline.py): control plane · Coordinates extraction, validation, learning, and returned stage results. · `pipeline.py`
- [Safe PDF text reader](https://github.com/srungavruksham/bank-statement-extractor/blob/main/utils.py): PDF ingestion · `utils.py`
- [CrewAI extraction](https://github.com/srungavruksham/bank-statement-extractor/blob/main/crewai_extraction.py): LLM agent layer · Runs parallel identity, account, and transaction extraction plus merge. · `crewai_extraction.py`
- [Role extractors](https://github.com/srungavruksham/bank-statement-extractor/blob/main/crewai_extraction.py): parallel LLM agents · Extract identity, account, and transaction data independently. · `crewai_extraction.py`
- [Merge and restore rows](https://github.com/srungavruksham/bank-statement-extractor/blob/main/crewai_extraction.py): Python-backed merge · Merges small identity/account outputs and restores transaction rows in Python. · `crewai_extraction.py`
- [Prompt contracts](https://github.com/srungavruksham/bank-statement-extractor/blob/main/prompts/identity.yaml): versioned YAML prompts · Role-specific extraction, merge, and reflection contracts. · `prompts/identity.yaml`
- [Prompt registry](https://github.com/srungavruksham/bank-statement-extractor/blob/main/prompts/registry.py): prompt resolver · `prompts/registry.py`
- [Deterministic validation](https://github.com/srungavruksham/bank-statement-extractor/blob/main/validation.py): Python correctness boundary · Checks required fields, balances, and date consistency. · `validation.py`
- [Gemini via CrewAI](https://github.com/srungavruksham/bank-statement-extractor/blob/main/requirements.txt): external LLM runtime · `requirements.txt`

### Learning & Evidence

- [Bank lesson store](https://github.com/srungavruksham/bank-statement-extractor/blob/main/memory/store.py): local learning store · Promotes repeated bank-specific mistakes into trusted rules after two occurrences. · `memory/store.py`
- [Reflection lesson](https://github.com/srungavruksham/bank-statement-extractor/blob/main/prompts/reflection.yaml): failure guidance · Captures validation issues and avoidance guidance. · `prompts/reflection.yaml`
- [LLM telemetry](https://github.com/srungavruksham/bank-statement-extractor/blob/main/telemetry/tracker.py): usage tracker · Records per-agent token use and cost. · `telemetry/tracker.py`

## Connections

- Statement PDFs → Streamlit app: select allowed PDF
- Streamlit app → Pipeline orchestrator: run processing
- Pipeline orchestrator → Safe PDF text reader: read safely
- Safe PDF text reader → CrewAI extraction: statement text
- Pipeline orchestrator → Bank lesson store: retrieve bank rules
- Bank lesson store → CrewAI extraction: learned context
- Prompt registry → Prompt contracts: resolve active prompts
- Prompt contracts → CrewAI extraction: agent contracts
- CrewAI extraction → Role extractors: parallel extraction
- Role extractors → Merge and restore rows: structured outputs
- Gemini via CrewAI → CrewAI extraction: LLM inference
- CrewAI extraction → LLM telemetry: token use and cost
- Merge and restore rows → Deterministic validation: combined record
- Deterministic validation → Pipeline orchestrator: validated result
- Deterministic validation → Reflection lesson: on failure
- Reflection lesson → Bank lesson store: validation lesson
- Pipeline orchestrator → Streamlit app: live stage results
- Streamlit app → Review workflow: approve or reject
- Review workflow → Approved records: approved record
- Review workflow → Bank lesson store: rejection feedback
- LLM telemetry → Streamlit app: execution evidence
