Multi-Tenancy & Hospital-Agnostic Architecture
The system is architected to serve multiple hospitals from a single deployment. All hospital-specific behaviour is driven by database configuration or per-tenant configuration files, so onboarding a new hospital requires no code changes or redeployment. In this documentation a "tenant" is one hospital.
Tenant-isolation trade-offs
| Decision | Chosen | Alternatives considered | Rejected because |
|---|---|---|---|
| Isolation model | Shared schema where every domain row carries a tenant reference (the "pool" model) | Schema per tenant; database per tenant | Schema-per-tenant complicates database migrations (one migration per tenant per release) and breaks single-query analytics. Database-per-tenant scales the connection pool linearly with the number of tenants and forbids cross-tenant analytics entirely. The pool model with row-level tenant filtering scales to the projected pilot fleet of five to ten hospitals without re-architecture. The three isolation models are the canonical multi-tenant SaaS taxonomy from Bezemer & Zaidman 2010. |
| Tenant resolution | The tenant is read from a signed claim in the identity provider's token and resolved once per request | Tenant ID in the URL; subdomain routing; a tenant request header | A tenant ID in the URL or a header lets a misconfigured client cross tenants by accident. Token claims are signed and verified on every request, so the tenant identity is cryptographically bound to the authentication. Subdomain routing is on the Multi-Tenancy Roadmap for Phase 2 but not in current scope. |
| Per-tenant configuration plane | Two planes: database-driven configuration for the web/RAG channel, and a version-controlled configuration file per tenant for the voice channel | One unified database-driven plane; one unified file | A single database-driven plane forces a schema migration for every voice-FAQ change; a single file forces every web crawl-rule edit to ship as a redeploy. The split keeps slow-moving voice content (FAQ entries, speech-recognition recovery rules) in version control, and fast-moving crawl rules in database rows that platform administrators can edit through the administrative interface. |
Hospital-agnostic refactoring (Phase 1) was completed on 2026-03-30. The system runs in single-tenant pilot mode for ZOL. Full multi-tenant routing (subdomain resolution, per-tenant authentication) is planned for Phase 2. See the Multi-Tenancy Roadmap.
Core design principles
| Principle | Implementation |
|---|---|
| No hardcoded hospital references | All identity is loaded from the database or configuration at runtime |
| Database-driven crawl configuration | A per-hospital configuration record replaces constants in code |
| Database-driven high-value pages | Hub pages for entity extraction are stored per hospital |
| Parameterised prompts | A prompt-context object is injected into every language-model call |
| Generic crawler | One crawler reads its configuration from the database |
| Auto-link on publish | A relationship auto-linker runs after every taxonomy publish to fill relationship gaps |
Configuration architecture
Crawl configuration
Every hospital's crawl behaviour is defined by one active configuration record, which the crawler loads at start-up. It holds:
- the allowed domains and the primary domain used to normalise URLs;
- URL patterns to skip, and high-priority URL prefixes;
- rules for removing boilerplate from pages (CSS selectors), from extracted text and from PDF cover pages;
- URL-to-category patterns used to classify pages;
- a maximum crawl depth and a safety limit on the number of pages;
- an "active" flag, with exactly one active configuration per hospital.
High-value pages
High-value ("golden") pages are URLs that are structurally valuable for entity extraction: hub pages that list doctors, departments or conditions. They are stored per hospital with a page type (hub or detail), the classifier's confidence, a review status (proposed, confirmed or rejected), an approval audit trail (who confirmed, when) and the time of the last entity extraction. They are populated by the hub-detection stage of the taxonomy extraction pipeline.
Parameterised prompts
All language-model prompts are parameterised through a small prompt-context object that carries the hospital's name, full name, location, public telephone number and website. No prompt contains a hardcoded hospital name. At runtime the context is built from the hospital's configuration; if none is available a generic default ("the hospital") is used. Query-decomposition and entity-validation prompts follow the same pattern.
The context flows into the RAG pipeline, the intent classifier, safety messages, disclaimers and blocked-query responses, all surfaces that previously contained hardcoded references to the pilot hospital.
The prompt context parameterises the web/RAG channel. The voice channel uses a separate per-tenant overlay plane described in Voice channel tenant overlays. The planes are separate so that voice-FAQ edits do not require a database migration and crawler-configuration edits do not require a configuration release.
Voice channel tenant overlays
The voice channel uses a parallel per-tenant configuration plane sourced from one version-controlled file per hospital. A loader reads the tenant's file at start-up and a registry returns the active overlay for each request.
What an overlay carries
| Section | Purpose | Example |
|---|---|---|
| FAQ entries | Per-tenant answers that bypass RAG. The voice FAQ tool checks intent and key phrases first; on a match, a renderer produces the spoken answer. | "What are the visiting hours?" is answered by a renderer that reads every campus from the taxonomy database and speaks each address, so no address strings live in code. |
| Speech-recognition recovery | Per-tenant corrections for systematic mishearings, applied to the speech-to-text output (@deepgram_nova3) before intent classification. | A Dutch caller who code-switches mid-sentence to an English compound word that the recogniser mishears as a different Dutch word. |
| Renderers | Database-driven functions that turn structured data (campuses, opening hours) into spoken text. | A future tenant gets correct answers by populating its taxonomy and overlay, with no code change. |
Why a file for this plane (not database rows)
| Trade-off | File overlay | Database-row equivalent |
|---|---|---|
| Edit cadence | Slow (FAQ patterns change quarterly) | Fast (edits without redeploy) |
| Review surface | A pull request, where a reviewer sees every line change | An administrative form, easy to mistype |
| Test coverage | An automated test loads the tenant file and asserts every entry | A per-row contract test, harder to maintain |
| Redeploy cost | Each overlay edit is a code release | None |
The file approach was chosen because voice content is reviewer-gated and changes infrequently. If overlay churn ever exceeds a quarterly cadence, a database table can replace the files without changing the runtime interface.
No hardcoded tenant data
A foundational rule: tenant facts (campus addresses, doctor names, hospital name) never live in source code. They live in the taxonomy database or, for speech-recovery rules, in the overlay. The runtime always asks the taxonomy for facts and the overlay for tenant-scoped voice content. The rule was codified after the May 2026 voice sprint, because an earlier FAQ table held hardcoded strings for the pilot hospital (an address, parking details, a contact number) and a future tenant onboarding would have inherited the wrong content silently. The renderer pattern, which reads from the taxonomy, is the canonical pattern for new tenant-scoped voice answers.
Crawler
The crawler is hospital-agnostic (it was renamed from a hospital-specific crawler). All site-specific constants, such as domains, skip patterns and boilerplate selectors, come from the crawl configuration loaded from the database rather than from class attributes. Its URL filtering and URL categorisation both delegate to that configuration, so crawler behaviour can be changed without a deployment. Default extraction rules handle common content-management patterns (Drupal, WordPress); hospital-specific overrides live in the database configuration.
Auto-linker in the publish pipeline
The relationship auto-linker runs automatically as a step of every taxonomy publish. Its purpose is to make sure newly published entities are connected to at least one department, filling relationship gaps that entity extraction missed.
How it works
- Find orphans: conditions, treatments and examinations that have no relationship to any department.
- Get the department list: load all departments of the hospital.
- Batch classification: send orphans to the language model in batches of 40; the model maps each entity to the most appropriate department from the supplied list.
- Insert relationships: one relationship per assignment, with the relationship type derived from the entity type. Re-running is harmless because duplicates are ignored.
- Commit: if any link was created, the changes are saved.
Relationship type mapping
| Entity type | Relationship created |
|---|---|
| Condition | The department treats the condition |
| Treatment | The department performs the treatment |
| Examination | The department performs the examination |
Failure isolation
The auto-linker runs inside an error guard. Any failure is logged as a warning and never blocks or rolls back the publish. When links were created, the publish result reports the count of automatically linked relationships.
New hospital onboarding flow
Step by step
| Step | Action | Responsible component |
|---|---|---|
| 1 | Create the hospital record | Hospital bootstrap (idempotent create-or-update) |
| 2 | Configure crawl settings | Administrative crawl-configuration function |
| 3 | Run the website crawl | Pipeline wizard, stage 2 (sitemap and content extraction) |
| 4 | Detect hub pages | Pipeline wizard, stage 3 (language-model binary classifier; populates the high-value pages) |
| 5 | Extract entities | Pipeline wizard, stage 4 (per-hub extraction, SNOMED matching, de-duplication) |
| 6 | Operator review | Pipeline wizard review interface (approve, reject, merge entities) |
| 7 | Publish the taxonomy | Publish service |
| 8 | Auto-link orphans | Relationship auto-linker, inside the publish; non-fatal |
After step 8 the in-memory taxonomy registry is invalidated and reloaded, so the new hospital's taxonomy is available to the RAG pipeline within milliseconds. No code changes and no redeployment are required.
Administrative interface for crawl configuration
Two authenticated administrative functions exist, restricted to administrators: reading the active crawl configuration of a hospital, and creating or updating it. The update is an upsert keyed on the hospital and the configuration name, so repeated calls are idempotent. Only the record marked active is used by the crawler at runtime.
Data isolation
All storage layers are scoped per hospital, and there is no shared mutable state between hospitals.
| Layer | Isolation mechanism |
|---|---|
| PostgreSQL taxonomy data | Every entity, relationship and version row references its hospital |
| Crawl configuration | Linked to a hospital; one active configuration per hospital |
| High-value pages | Linked to a hospital; a URL can appear only once per hospital |
| Documents and chunks | Linked to a hospital; every vector query includes the hospital filter |
| Redis | A hospital prefix on all cache, rate-limit and session keys |
| MinIO object storage | A hospital prefix on every object path |
A dependency on every request extracts the tenant from the verified token claims, so each request is scoped to the correct hospital before any query executes.
Related pages
- Multi-Tenancy Roadmap: phased roadmap and tenant-routing plans
- Taxonomy Extraction Pipeline: hub detection, entity extraction and publish flow
- Draft/Publish System: publish versioning and rollback