|
| 1 | +--- |
| 2 | +name: Classifier Deployment Strategy |
| 3 | +description: Exploration of multi-tenant ML model serving strategies — managed training with flexible deployment (self-hosted or managed). |
| 4 | +tags: [classifier, ml, multi-tenant, architecture, deployment] |
| 5 | +author: emil |
| 6 | +date: 2026-04-24 |
| 7 | +status: Proposed |
| 8 | +--- |
| 9 | + |
| 10 | +# 0050. Classifier Deployment Strategy |
| 11 | + |
| 12 | +Date: 2026-04-24 |
| 13 | + |
| 14 | +## Status |
| 15 | + |
| 16 | +Proposed — open for discussion, not yet decided. |
| 17 | + |
| 18 | +## Context |
| 19 | + |
| 20 | +KSeF Hub uses three sidecar services: |
| 21 | + |
| 22 | +| Service | Coupling | Tenant-specific? | |
| 23 | +|---------|----------|-----------------| |
| 24 | +| **pdf-renderer** | Tightly coupled to the app (FA(3) XML → PDF) | No — same logic for all tenants | |
| 25 | +| **invoice-extractor** | Tightly coupled to the app (PDF → structured JSON) | No — generic extraction | |
| 26 | +| **invoice-classifier** | Loosely coupled, depends on tenant data | **Yes** — categories, tags, and classification patterns are per-company | |
| 27 | + |
| 28 | +The classifier is fundamentally different: its accuracy depends on the tenant's own data. A model trained on one company's invoices (e.g., a media agency) will mis-classify for another (e.g., a construction firm). This means multi-tenant SaaS needs per-tenant models. |
| 29 | + |
| 30 | +### Current state |
| 31 | + |
| 32 | +- Single classifier service behind a per-company config (ADR 0049) |
| 33 | +- Each company can override the classifier URL, token, and confidence thresholds |
| 34 | +- One shared model serves all companies — predictions degrade as company profiles diverge |
| 35 | +- Training data export exists (CSV with extended columns from the Services tab) |
| 36 | + |
| 37 | +### Problem |
| 38 | + |
| 39 | +As we scale to more tenants, a shared model won't work. We need a strategy for: |
| 40 | +1. **Training** — how per-tenant models are created and updated |
| 41 | +2. **Serving** — how predictions are served at inference time |
| 42 | +3. **Cost** — who pays for compute (us or the tenant) |
| 43 | +4. **Ops** — how much operational burden falls on us vs. the tenant |
| 44 | + |
| 45 | +## Options Considered |
| 46 | + |
| 47 | +### Option A — Managed multi-tenant model serving |
| 48 | + |
| 49 | +We own both training and serving. A model-management sidecar loads per-tenant models on demand. |
| 50 | + |
| 51 | +**How it works:** |
| 52 | +- Tenant clicks "retrain" in the UI (or we auto-retrain on a schedule) |
| 53 | +- We train the model from their approved invoices using our training pipeline |
| 54 | +- A model-serving sidecar keeps recently-used models in memory, evicts cold ones |
| 55 | +- Prediction requests are routed to the correct tenant model |
| 56 | + |
| 57 | +**Pros:** |
| 58 | +- Best UX — tenant never touches infrastructure |
| 59 | +- We control model quality, versioning, rollback |
| 60 | +- Can optimize hardware (shared GPU/CPU, model caching) |
| 61 | + |
| 62 | +**Cons:** |
| 63 | +- Operational complexity — cache eviction, cold starts, memory pressure, OOM risk |
| 64 | +- We bear infra cost per model (though classification models are small, ~MBs) |
| 65 | +- Single point of failure — our serving layer goes down, all tenants lose classification |
| 66 | + |
| 67 | +### Option B — Dedicated self-hosted service per tenant |
| 68 | + |
| 69 | +We open-source the classifier. Each tenant trains and deploys their own instance. |
| 70 | + |
| 71 | +**How it works:** |
| 72 | +- We provide training tools and a Docker image |
| 73 | +- Tenant deploys to their own cloud (GCP Cloud Run, AWS ECS, etc.) |
| 74 | +- Tenant configures their classifier URL in KSeF Hub (already supported via ADR 0049) |
| 75 | + |
| 76 | +**Pros:** |
| 77 | +- Zero infra cost for us |
| 78 | +- Tenant data never leaves their environment (compliance/GDPR win) |
| 79 | +- Tenant has full control over model, hardware, scaling |
| 80 | + |
| 81 | +**Cons:** |
| 82 | +- Terrible UX — requires cloud knowledge, deployment pipeline, monitoring |
| 83 | +- Support burden when tenant's service breaks |
| 84 | +- Fragmented versions — tenants may run stale images |
| 85 | + |
| 86 | +### Option C — Managed training + flexible deployment (preferred direction) |
| 87 | + |
| 88 | +We own the training pipeline. The tenant chooses where to serve: our managed infrastructure or their own cloud. |
| 89 | + |
| 90 | +**How it works:** |
| 91 | +- **Training**: We provide a managed training pipeline. Tenant's approved invoices feed into it. We produce a model artifact (e.g., ONNX, TensorFlow SavedModel, scikit-learn pickle). |
| 92 | +- **Deployment option 1 — Managed**: We host the model in our serving layer (same as Option A). Included in the subscription or as a paid tier. |
| 93 | +- **Deployment option 2 — Self-hosted on cloud ML platforms**: Tenant deploys the model artifact to a managed ML service: |
| 94 | + - **Google Cloud**: Vertex AI Endpoints (upload model → get prediction API), Cloud Run (containerized) |
| 95 | + - **AWS**: SageMaker Endpoints (upload model → get prediction API), Lambda (lightweight) |
| 96 | + - **Azure**: Azure ML Endpoints |
| 97 | +- We provide clear documentation/tooling for each cloud platform deployment. |
| 98 | +- Tenant points their classifier URL to their deployed endpoint (ADR 0049 already supports this). |
| 99 | + |
| 100 | +**Pros:** |
| 101 | +- We own the hardest part (training pipeline, data prep, feature engineering) |
| 102 | +- Tenant gets a simple choice: "we host it" or "you host it on [GCP/AWS/Azure]" |
| 103 | +- Cloud ML platforms handle scaling, monitoring, versioning natively |
| 104 | +- Self-hosted option satisfies compliance-sensitive tenants |
| 105 | +- Model artifact is portable — not locked to our infrastructure |
| 106 | + |
| 107 | +**Cons:** |
| 108 | +- Need to support multiple model export formats (or standardize on one like ONNX) |
| 109 | +- Documentation/guides needed for each cloud platform |
| 110 | +- Two code paths for model delivery (managed vs. self-hosted) |
| 111 | + |
| 112 | +## Open Questions |
| 113 | + |
| 114 | +1. **Model format**: What format do we standardize on? ONNX is portable but may lose framework-specific optimizations. scikit-learn pickle is simple but Python-only. |
| 115 | +2. **Retraining frequency**: On-demand (tenant clicks button), scheduled (weekly), or continuous (on every N new approved invoices)? |
| 116 | +3. **Minimum training data**: How many approved invoices does a company need before a per-tenant model outperforms the shared default model? |
| 117 | +4. **Cold start**: For new tenants with no data, do we use a shared "bootstrap" model trained on anonymized cross-tenant data? |
| 118 | +5. **Pricing**: Is managed serving included in the base plan, or a paid add-on? |
| 119 | +6. **Model versioning**: How do we handle A/B testing between model versions? Rollback on accuracy regression? |
| 120 | + |
| 121 | +## Decision |
| 122 | + |
| 123 | +Not yet decided. This ADR captures the current thinking to be revisited when we approach multi-tenant scaling. |
| 124 | + |
| 125 | +**Leaning toward Option C** — managed training with flexible deployment. It gives us the best of both worlds: great UX for tenants who want "it just works", and full control for tenants with compliance requirements or existing cloud infrastructure. |
| 126 | + |
| 127 | +## Next Steps |
| 128 | + |
| 129 | +- [ ] Research Vertex AI / SageMaker model deployment APIs — what's the minimal integration? |
| 130 | +- [ ] Prototype: export a trained model artifact, deploy to Vertex AI, verify prediction API compatibility |
| 131 | +- [ ] Define minimum viable training pipeline (how many invoices, what features, what accuracy threshold) |
| 132 | +- [ ] Estimate serving costs per tenant for the managed option |
| 133 | +- [ ] Decide on model format standard |
| 134 | + |
| 135 | +## Consequences |
| 136 | + |
| 137 | +Deferring this decision is acceptable for now — the current shared model + per-company config (ADR 0049) works for early tenants. But this becomes blocking when: |
| 138 | +- A second tenant with a very different business profile onboards |
| 139 | +- Classification accuracy drops below the confidence thresholds for any company |
| 140 | +- A tenant asks "can I train my own model?" |
0 commit comments