Security advisory — Malicious litellm versions 1.82.7 and 1.82.8 were removed from PyPI (potential API key exfiltration). Uninstall them, rotate exposed credentials, and upgrade to a safe release (e.g. 1.82.9+). Run pip show litellm to verify. PyPI · README

Database Schema

ORM: Drizzle ORM + PostgreSQL

Database Schema

Last updated: 2026-05-21 · Corresponding code: packages/db-schema/src/schema/*

ORM: Drizzle ORM + PostgreSQL

Package: @agenticx/db-schema (packages/db-schema/)

Migrations: packages/db-schema/drizzle/ (0000 → 0011_gateway_channels.sql)


1. Migrations and Seed

bash
1# bootstrap.sh runs this automatically
2pnpm --filter @agenticx/db-schema db:migrate
3pnpm --filter @agenticx/db-schema db:seed
Seed scriptWhat it writes
scripts/db-seed.mjsDefault tenant, owner user, owner role (including * scope)
scripts/iam-demo-seed.mjs (optional)Multi-level departments, 4 roles, 10 demo users; triggered via reset-dev-data.sh --with-iam-seed

2. Table inventory (22 tables)

2.1 IAM core (7)

TableKey columnsDescription
tenantsid, code, name, planTenant
organizationsid, tenant_id, nameOrganization
departmentsid, tenant_id, org_id, parent_id, pathDepartment tree (with path)
usersid, tenant_id, dept_id, email, display_name, password_hash, status, phone, employee_no, job_title, failed_login_count, locked_until, is_deleted, deleted_atUsers (soft delete + login lockout)
rolesid, tenant_id, code, name, scopes jsonb, immutableimmutable=true marks a system role that cannot be deleted
user_rolesuser_id, role_id, scope_org_id, scope_dept_idMultiple roles; supports scoped assignment
sso_providersid, tenant_id, provider_type(oidc/saml), client_secret_cipherClient secret encrypted with AES-GCM

2.2 Chat (2)

TableKey columnsDescription
chat_sessionsid, tenant_id, user_id, title, active_model, message_count, last_message_at, deleted_atSoft delete; active_model remembers the current model
chat_messagesid, session_id, role, content, model, metadata jsonbFull message history

2.3 Metering (1)

TableKey columnsDescription
usage_recordstenant_id, dept_id, user_id, provider, model, route, time_bucket, input_tokens/output_tokens/total_tokens numeric(20,0), cost_usd numeric(18,8)Five-dimension aggregation

2.4 Audit (two tables)

TableKey columnsDescription
audit_eventsactor, target_kind, detailAudit of IAM admin operations (admin CRUD)
gateway_audit_eventsevent_time, event_type, user_id, user_email, department_id, session_id, client_type, client_ip, provider, model, route, input/output/total_tokens, latency_ms, digest jsonb, policies_hit jsonb (GIN index), tools_called jsonb, prev_checksum, checksum, signatureAudit of LLM calls; Blake2b chain

⚠️ The two tables have different uses: the admin /audit page queries gateway_audit_events; IAM operation logs go to audit_events.

2.5 Policy (4)

TableKey columnsDescription
policy_rule_packscode, name, source, enabled, applies_to jsonbRule pack
policy_rulespack_id, code, kind, action, severity, message, payload jsonb, applies_to jsonb, status(draft/active), updated_byIndividual rule
policy_rule_versionsrule_id, version, snapshot jsonb, authorA version is kept on each save
policy_publish_eventssnapshot jsonbPublish history

applies_to field (PolicyAppliesTo type):

ts
1{
2 version?: number;
3 departmentIds?: string[];
4 departmentRecursive?: boolean;
5 roleCodes?: string[];
6 userIds?: string[];
7 userExcludeIds?: string[];
8 clientTypes?: string[];
9 stages?: string[];
10}

2.6 Runtime configuration (6, migrated from JSON to PG)

TableFormer JSONKey columns
enterprise_runtime_model_providersproviders.jsontenant_id, provider_id, display_name, base_url, api_key_cipher, enabled, is_default, route, env_key, models jsonb
enterprise_runtime_user_visible_modelsuser-models.jsontenant_id, assignment_key(user ulid or email:xxx), model_id — composite primary key
enterprise_runtime_token_quotasquotas.jsontenant_id primary key, config jsonb
enterprise_runtime_policy_snapshotspolicy-snapshot.jsontenant_id primary key, snapshot jsonb
auth_refresh_sessions—session_id, user_id, tenant_id, dept_id, email, scopes_json, expires_at (serverless multi-replica refresh)
gateway_channels—id, tenant_id, name, provider_type(default openai), base_url, api_key_cipher, weight, priority, status(active/disabled), supported_models jsonb, metadata jsonb

Migration CLI:

bash
1pnpm -C enterprise migrate:legacy-runtime

Triggered automatically by bootstrap.sh and start-dev.sh (local DB only); idempotent.


3. ER relationships

Diagram: tenants own IAM, chat, policy, runtime, audit, and Channel; departments can nest; users own sessions.

4. Shared column conventions (_shared.ts)

HelperColumns
ulid(name)varchar(26) — used for all primary keys / FKs
auditColumnscreated_at, updated_at (withTimezone, defaultNow)
softDeleteColumnsis_deleted boolean, deleted_at timestamptz

5. Tenant isolation rules

  • Business tables all carry a tenant_id FK → tenants.id; delete policy is usually RESTRICT (to avoid accidental wipes)
  • Composite unique indexes often include tenant_id (e.g. users_tenant_email_uq)
  • The API layer injects tenant from the JWT; the server must not read or write across tenants
  • Multi-tenant expansion: the Internal API currently returns the whole tenant in a single-tenant deployment; for multi-tenant fan-out, the caller passes tenant_id to filter (as implemented)

6. Encrypted fields

FieldAlgorithmSecret env var
enterprise_runtime_model_providers.api_key_cipherAES-256-GCMAGX_PROVIDER_SECRET_KEY
gateway_channels.api_key_cipherAES-256-GCMAGX_PROVIDER_SECRET_KEY
sso_providers.client_secret_cipherAES-256-GCMSSO_PROVIDER_SECRET_KEY

Key rotation: all existing rows must be re-encrypted; batch via an ops script.


7. Source index

FileTables
schema/tenants.tstenants
schema/organizations.tsorganizations
schema/departments.tsdepartments
schema/users.tsusers
schema/roles.tsroles
schema/user-roles.tsuser_roles
schema/sso-providers.tssso_providers
schema/chat-sessions.tschat_sessions
schema/chat-messages.tschat_messages
schema/usage-records.tsusage_records
schema/audit-events.tsaudit_events
schema/gateway-audit-events.tsgateway_audit_events
schema/policy.tspolicy_rule_packs, policy_rules, policy_rule_versions, policy_publish_events
schema/runtime-config.tsenterprise_runtime_*, auth_refresh_sessions
schema/gateway-channels.tsgateway_channels

Export index: schema/index.ts


8. Related docs

Made-with: Damon Li