Self-hosting

Self-hosting

Run the Kaimei API on your own infrastructure, against SQLite on a laptop or Postgres for a team.

The API is a FastAPI service and runs anywhere Python 3.9 does, against SQLite for a laptop or Postgres (Supabase works) for a team. Enterprise plans include a private deployment; the configuration below applies to any installation.

Service 1.3.1 serves the /v1 contract: new fields may appear, and existing fields keep their meaning, apart from four changes in 1.2.0, three of them for safety. See API compatibility. Rate limits are stored in the database, so every process of one deployment shares them. An analysis that was still running when the process stopped is resumed; after three interrupted attempts it fails and can be started again. Responses carry X-Request-Id.

Configuration is through KAIMEI_* environment variables or a .env file in the working directory. The ones that matter most:

VariableDefaultMeaning
KAIMEI_DATABASE_URLsqlite:///./kaimei.dbSQLAlchemy URL. Postgres in production.
KAIMEI_SECRET_KEY(dev value)HMAC key for session tokens. At least 32 random bytes; always set it.
KAIMEI_ENVdevelopmentproduction turns email verification on by default; test turns rate limits and quotas off.
KAIMEI_PUBLIC_API_URL / KAIMEI_DASHBOARD_URLlocalhostUsed for OAuth callbacks, verification and invitation links, CORS, the dashboard pages Stripe returns customers to, and the billing webhook's address, which must be https for Stripe to reach it.
KAIMEI_ALLOW_SIGNUPtrueSet false to stop self-serve sign-up, and create accounts with kaimei-api create-user <email> <password> --accept-terms, once the person has agreed to the Terms of Use. An invitation does not open sign-up: the person invited needs an account first.
KAIMEI_MAX_REFERENCE_ROWS5000Reference rows kept per model.
KAIMEI_EXPLANATION_EVAL_BUDGET60000Soft ceiling on model evaluations per explanation.
KAIMEI_IMAGE_EXPLAIN_SECONDS40Time budget image explanations plan their grid to fit, and Shapley sampling over the parts of a text or series its permutations.
KAIMEI_STRUCTURE_RUN_SECONDS60How long one pass of a general model graph may take.
KAIMEI_REQUEST_RUN_SECONDS90How long all the passes of one request may take together, counted from the first. Keep it under your proxy's own timeout (Cloudflare's is 100 seconds).
KAIMEI_ANALYSIS_RUN_SECONDS900How long all the passes of one background analysis may take.
KAIMEI_GENERATION_SECONDS45How long the whole loop of POST …/generate may take.
KAIMEI_MAX_FOREST_CACHE_BYTES2147483648What compiled forests and graphs kept in memory may hold between them, their loaded constants and parameters counted.
KAIMEI_AGENT_WAITS / _PER_ORGANIZATION16 / 6Requests one process lets wait for agents' answers, in all and for one organization; past either, 429 agent_busy.
KAIMEI_AGENT_ROUTE_THREADS8Threads agents' own routes run on, so waiting callers never keep an answer out.
KAIMEI_AGENT_SESSIONS_PER_MODEL / KAIMEI_AGENT_POLLS_PER_MODEL32 / 32Agents one model may have connected, and requests for work it may have waiting.
KAIMEI_MAX_RUN_ACTIVATION_BYTES / _PARAMETER_BYTES1073741824What one row of a model run here may hold, and its tensors. A model past them is drawn, not run.
KAIMEI_MAX_TENSOR_BYTES536870912Largest single binary tensor upload, compressed.
KAIMEI_REMOTE_INFERENCE_TIMEOUT_S30Default timeout for remote endpoints.
KAIMEI_BEHIND_CLOUDFLARE / KAIMEI_TRUST_PROXYfalseRead the client IP from CF-Connecting-IP / X-Forwarded-For for rate limits.
KAIMEI_TURNSTILE_* , KAIMEI_SMTP_*, KAIMEI_GOOGLE_* , KAIMEI_GITHUB_*unsetBot protection on sign-up; verification, invitation and join-confirmation email (without SMTP an invitation link is returned to whoever sent it, to pass on, and joining by email domain can neither be turned on nor used, since the API never returns those links: without SMTP they only reach the server log); OAuth sign-in.
KAIMEI_TERMS_VERSION2026-11-20The effective date of the Terms of Use that every new account records as agreed. Every way of making an account requires that agreement. Change it when the Terms change.
KAIMEI_STRIPE_SECRET_KEY / KAIMEI_STRIPE_WEBHOOK_SECRETunsetPaid plans through Stripe. Without a secret key the plans are listed and buying one is 503 billing_not_configured. Use a test-mode key (sk_test_…) anywhere but production.
KAIMEI_STRIPE_PRICE_STANDARD / _PRICE_TEAM / _PORTAL_CONFIGURATIONunsetOnly for prices or a portal configuration that kaimei-api billing setup did not make; it finds its own.
KAIMEI_STRIPE_AUTOMATIC_TAX / KAIMEI_STRIPE_PROMOTION_CODESfalseSales tax and VAT worked out by Stripe Tax at checkout, and a promotion-code field there.

Point the Python client at your installation with base_url or KAIMEI_BASE_URL; the dashboard with VITE_API_URL.

Paid plans. Set KAIMEI_STRIPE_SECRET_KEY, run kaimei-api billing setup to see what the Stripe account lacks, then --apply to create the plans' prices, the customer portal configuration and the webhook endpoint, and set KAIMEI_STRIPE_WEBHOOK_SECRET to the secret it prints. In Stripe, under Billing, Revenue recovery, keep cancelling the subscription as what happens when every retry of a payment has failed: a subscription left past_due keeps its plan. Enterprise is set by hand: kaimei-api billing set-limits saves the contract's limits, which the organization's admins cannot lift, and kaimei-api billing set-plan puts it on enterprise, where they apply. Save the limits first, so it is never on Enterprise without them; a limit left out is unlimited. billing show-limits prints what is in effect.

Upgrading. Starting the API applies any database migration it has not recorded. The release that adds organizations gives every existing account an owner's membership of its own workspace, and nothing moves. An organization that already has more than one member and is not on Team becomes read-only, so put it on a plan first if it should keep working as it does: kaimei-api billing set-plan <slug> team.

shell
pip install -e ".[dev]"                 # from the kaimei API repository
kaimei-api init-db                      # create tables
kaimei-api seed-demo                    # demo@kaimei.ai / kaimei-demo, five reference models, an API key
kaimei-api serve --reload               # http://localhost:8000

# Paid plans: with KAIMEI_STRIPE_SECRET_KEY set
kaimei-api billing setup                # check the Stripe account; changes nothing
kaimei-api billing setup --apply        # create what is missing, print KAIMEI_STRIPE_WEBHOOK_SECRET
kaimei-api billing sync                 # read every subscription back from Stripe
kaimei-api billing set-limits acme-risk --models 500 --predictions-per-month 2000000 --note "MSA 2026-031"
kaimei-api billing set-plan acme-risk enterprise
kaimei-api billing show-limits acme-risk

# Postgres / Supabase
pip install -e ".[postgres]"
export KAIMEI_DATABASE_URL=postgresql://postgres.[ref]:[password]@aws-0-[region].pooler.supabase.com:5432/postgres
kaimei-api migrate-db --inspect         # row counts in the local SQLite file
kaimei-api migrate-db                   # copy it over