-
-
Notifications
You must be signed in to change notification settings - Fork 2
plugin nself ai gateway
The nSelf AI Gateway manages a pool of encrypted provider API keys (Anthropic, OpenAI, Google, Ollama, vLLM, TEI) and dispatches AI requests across them using LRU round-robin rotation with per-tenant isolation, quota enforcement, and automatic demotion on errors.
Port: 3761 | License: Pro | Bundle: ɳClaw (AI-CP)
The gateway maintains one pool per provider (max 30 keys per pool, configurable via
NSELF_AI_POOL_MAX_KEYS). Keys are stored AES-256-GCM encrypted; plaintext is only
decrypted in memory at dispatch time and never logged.
Each key can be restricted to specific capability lanes:
| Lane | Use case |
|---|---|
fast |
Low-latency responses, short context |
deep |
Long-context reasoning |
multimodal |
Vision / image understanding |
embedding |
Text embedding generation |
rerank |
Cross-encoder reranking |
live |
Real-time streaming |
local |
On-premises models (ollama, vLLM) |
Keys with lane_affinity = NULL serve all lanes.
Keys carry an optional tenant_id:
-
tenant_id = NULL: shared pool, accessible to all callers -
tenant_id = <UUID>: dedicated to that tenant only
Dedicated keys are preferred over shared pool keys in selection.
nself license set <your-key>
nself plugin install nself-ai-gatewaySet required environment variables:
nself plugin env set nself-ai-gateway NSELF_VAULT_KEY=<64-hex-char-key>
nself plugin env set nself-ai-gateway NSELF_DB_URL=postgres://...Generate a vault key:
openssl rand -hex 32| Variable | Required | Default | Description |
|---|---|---|---|
NSELF_DB_URL |
Yes | — | PostgreSQL connection string |
NSELF_VAULT_KEY |
Yes | — | 32-byte AES-256 key as 64-char hex |
NSELF_AI_GATEWAY_PORT |
No | 3761 |
HTTP server port |
NSELF_AI_POOL_MAX_KEYS |
No | 30 |
Max keys per provider pool |
NSELF_AI_HEALTH_INTERVAL_SEC |
No | 60 |
Key health check interval |
NSELF_AI_AUDIT_RETENTION_DAYS |
No | 90 |
Audit log retention days |
NSELF_LICENSE_KEY |
Yes | — | nSelf Pro license key |
GET /health
→ {"status": "ok"}
→ {"status": "unhealthy"} (503 if DB unreachable)
POST /admin/keys
{
"provider": "anthropic",
"plain_key": "sk-ant-...",
"tier": "shared",
"quota_day": 1000,
"lane_affinity": ["fast", "deep"],
"tenant_id": null
}plain_key is encrypted with AES-256-GCM before storage. Returns {"id": "uuid"}.
provider must be one of: anthropic, google, openai, ollama, vllm, tei.
tier must be one of: shared, dedicated, premium.
Pool cap: at most NSELF_AI_POOL_MAX_KEYS enabled keys per provider.
POST /dispatch
{
"provider": "anthropic",
"lane": "fast",
"tenant_id": "uuid-or-null"
}Returns:
{
"key_id": "uuid",
"provider": "anthropic",
"key": "sk-ant-...",
"tier": "shared"
}The key field is the decrypted plaintext API key. It is transmitted over TLS only.
Callers must not log or cache this value.
On quota exceeded:
{
"error": "QUOTA_EXCEEDED",
"provider": "anthropic",
"retry_after_ms": 45000
}- Filter:
enabled = true,error_count < 3,cooldown_until < now(), lane match, tenant match - Sort: dedicated keys first, then LRU (oldest
last_used_atfirst) - Lock:
FOR UPDATE SKIP LOCKEDfor distributed safety - Update: decrement
quota_remaining, setlast_used_at = now()
Keys are stored as base64(nonce || ciphertext) where:
-
nonce= 12 random bytes (unique per encryption) -
ciphertext= AES-256-GCM(plaintext, NSELF_VAULT_KEY, nonce)
Security invariants:
- Plaintext API key is NEVER stored, NEVER logged
- Decryption occurs only in memory at dispatch time
-
ag_key_auditstores only the key UUID reference, never key values
On 3 consecutive errors, a key enters cooldown:
error_count |
cooldown |
|---|---|
| 3 | 240s (4 min) |
| 4 | 480s (8 min) |
| 5 | 960s (16 min) |
| 6+ | 3600s (cap) |
Recovery: a single successful health probe clears error_count = 0 and cooldown_until = NULL.
| Table | Purpose |
|---|---|
ag_key_pool |
Encrypted key registry with quota, demotion, tenant, lane data |
ag_key_audit |
Per-call cost audit log (90-day retention) |
ag_* tables use tenant_id UUID (cloud multi-tenancy), not source_account_id.
Hasura row filters enforce tenant isolation on tenant_admin role.
Requires an active nSelf Pro license. Purchase at nself.org/pro.
- plugin-nself-ai-mcp — MCP dispatch layer over this gateway
- Architecture — nSelf AI-CP stack overview
- Home
ɳSelf CLI v1.0.9. MIT licensed. Docs CC BY 4.0.
GitHub · Issues · Discussions · nself.org · nself.org/docs
Getting Started
Commands
- Commands, Overview
- Lifecycle: cmd-init · cmd-build · cmd-start · cmd-stop · cmd-restart · cmd-dev
- Monitoring: cmd-status · cmd-logs · cmd-health · cmd-urls · cmd-doctor · cmd-monitor · cmd-alerts · cmd-sentry · cmd-watchdog
- Data: cmd-db · cmd-backup · cmd-dr · cmd-queue · cmd-webhooks
- Config: cmd-config · cmd-service · cmd-env · cmd-promote
- Networking: cmd-ssl · cmd-trust · cmd-dns-setup
- Security: cmd-access · cmd-security · cmd-secrets
- Tenancy: cmd-tenant · cmd-billing
- Plugins: cmd-plugin · cmd-license · cmd-dogfood (extracted, CLI-R11) · cmd-k8s (extracted, CLI-R11) · cmd-encryption (extracted, CLI-R11) · cmd-waf (extracted, CLI-R11) · cmd-federation (extracted, CLI-R11) · cmd-mail (extracted, CLI-R11) · cmd-dlq (extracted, CLI-R11)
- AI: cmd-ai · cmd-claw · cmd-model
- Templates: cmd-template
- Utilities: cmd-exec · cmd-clean · cmd-reset · cmd-update · cmd-upgrade · cmd-version · cmd-admin · cmd-migrate · cmd-migrate-firebase · cmd-migrate-supabase · cmd-completion
Features
- Features, Overview
- Feature-Auth
- Feature-Storage
- Feature-Search
- Feature-Functions
- Feature-Email
- Feature-Monitoring
- Feature-Plugins
- Feature-nClaw, AI Assistant
- Feature-nChat, Messaging
- Feature-nTV, Media Player
- Feature-nFamily, Family Social
- Feature-nCloud, Managed Hosting
- Feature-Memory-Rooms, Knowledge Organization
- Feature-Agent-Dashboard, Agent Metrics
- Feature-Image-Generation, AI Image Generation
Configuration
- Configuration, Overview
- Config-Env-Vars
- Config-Postgres
- Config-Hasura
- Config-Auth
- Config-Nginx
- Config-Optional-Services
- Config-Custom-Services
- Config-System
Plugins (87 + 10 monitoring)
Free (25)
- plugin-backup
- plugin-content-acquisition
- plugin-content-progress
- plugin-cron
- plugin-donorbox
- plugin-feature-flags
- plugin-github
- plugin-github-runner
- plugin-invitations
- plugin-jobs
- plugin-link-preview
- plugin-mdns
- plugin-mlflow
- plugin-monitoring
- plugin-notifications
- plugin-notify
- plugin-paypal
- plugin-search
- plugin-shopify
- plugin-stripe
- plugin-subtitle-manager
- plugin-tokens
- plugin-torrent-manager
- plugin-vpn
- plugin-webhooks
Pro (62)
- plugin-access-controls
- plugin-activity-feed
- plugin-admin-api
- plugin-nself-ai-gateway
- plugin-nself-ai-mcp
- plugin-nself-ai-mcp
- plugin-analytics
- plugin-auth
- plugin-backup-pro
- plugin-bots
- plugin-browser
- plugin-calendar
- plugin-cdn
- plugin-chat
- plugin-claw
- plugin-claw-budget
- plugin-claw-news
- plugin-claw-web
- plugin-cloudflare
- plugin-cms
- plugin-compliance
- plugin-cron-pro
- plugin-ddns
- plugin-devices
- plugin-documents
- plugin-donorbox-pro
- plugin-entitlements
- plugin-epg
- plugin-file-processing
- plugin-game-metadata
- plugin-geocoding
- plugin-geolocation
- plugin-google
- plugin-home
- plugin-idme
- plugin-knowledge-base
- plugin-linkedin
- plugin-livekit
- plugin-media-processing
- plugin-meetings
- plugin-moderation
- plugin-mux
- plugin-notify-pro
- plugin-object-storage
- plugin-observability
- plugin-paypal-pro
- plugin-photos
- plugin-podcast
- plugin-post
- plugin-realtime
- plugin-recording
- plugin-retro-gaming
- plugin-rom-discovery
- plugin-shopify-pro
- plugin-social
- plugin-sports
- plugin-stream-gateway
- plugin-streaming
- plugin-stripe-pro
- plugin-support
- plugin-tmdb
- plugin-voice
- plugin-web3
- plugin-workflows
Planned (26)
plugin-auditplugin-blogplugin-checkoutplugin-commerceplugin-drmplugin-exportplugin-flowplugin-importplugin-ldapplugin-mailgunplugin-mediaplugin-oauth-providersplugin-pagesplugin-postmarkplugin-rate-limitplugin-reportsplugin-samlplugin-schedulerplugin-sendgridplugin-ssoplugin-subscriptionplugin-thumbplugin-transcoderplugin-twilioplugin-wafplugin-watermark
Guides
- Guide-Production-Deployment
- Guide-SSL-Setup
- Guide-Multi-Tenancy
- Guide-Security-Hardening
- Guide-Monitoring-Setup
- Guide-Backup-Restore
- Guide-Custom-Services
- Guide-Migration-from-v1
Architecture
Reference
- API-Reference
- error-codes, Error Codes
Licensing
Security
Brand
Operations
- operations/release-cascade, Release Cascade
- operations/self-healing, Self-Healing Schema
- operations/redis-tuning, Redis Pool Tuning
- operations/meilisearch-warmup, MeiliSearch Warm-Up
- operations/jwt-rotation, JWT Key Rotation
- operations/windows-wsl2-setup, Windows / WSL2 Setup
- operations/gemini-oauth-reauth, Gemini OAuth Reauth
Contributing
Admin
- USER-ACTION-QUEUE, Pending Admin Actions
All commands (52)
- A: cmd-access · cmd-account · cmd-admin
- B: cmd-backup · cmd-build · cmd-bundle
- C: cmd-ci · cmd-clean · cmd-completion · cmd-config
- D: cmd-db · cmd-deploy · cmd-dev · cmd-doctor
- E: cmd-env · cmd-exec
- F: cmd-functions
- G: cmd-generate
- H: cmd-health · cmd-help-topics
- I: cmd-init · cmd-install
- L: cmd-license · cmd-login · cmd-logout · cmd-logs
- M: cmd-man · cmd-mcp · cmd-migrate
- O: cmd-oauth · cmd-ops
- P: cmd-plugin · cmd-promote
- R: cmd-remove · cmd-reset · cmd-restart · cmd-runner
- S: cmd-secrets · cmd-security · cmd-self-heal · cmd-server · cmd-service · cmd-start · cmd-status · cmd-stop
- T: cmd-telemetry · cmd-template · cmd-trust
- U: cmd-update · cmd-urls
- V: cmd-verify-sbom · cmd-version