Skip to content

User Limits & Resource Controls

Backflow provides flexible limit enforcement at two levels:

  • Backflow to Tenants: Platform-level limits on tenant usage
  • Tenants to End-Users: Tenant-configurable limits for their users

Configuration

Add limits to your tenant config:

json
{
  "userLimits": {
    "enabled": true,
    "defaultGateAction": "upgrade",
    "entityLimits": { ... },
    "storageLimits": { ... },
    "resourceLimits": [...]
  }
}

Entity Limits

Limit how many entities a user can create per collection:

json
{
  "userLimits": {
    "enabled": true,
    "entityLimits": {
      "projects": {
        "limit": 10,
        "gateAction": "upgrade",
        "tiers": { "free": 3, "pro": 10, "enterprise": -1 },
        "scope": "user"
      },
      "files": {
        "limit": 100,
        "scope": "tenant"
      }
    }
  }
}

Options

FieldTypeDescription
limitnumberDefault max count
gateActionlogin | upgradeAction when limit reached
tiersRecordPer-tier overrides (-1 = unlimited)
scopeuser | tenantCount per user or entire tenant

Storage Limits

Limit file storage per user or tenant:

json
{
  "userLimits": {
    "storageLimits": {
      "enabled": true,
      "scope": "user",
      "gateAction": "upgrade",
      "tiers": {
        "free": { "maxStorageMB": 100, "maxFileSizeMB": 10 },
        "pro": { "maxStorageMB": 5000, "maxFileSizeMB": 100 }
      }
    }
  }
}

Resource Limits

Limit workflows, LLM calls, API requests, embeddings, and jobs:

json
{
  "userLimits": {
    "resourceLimits": [
      {
        "resource": "workflow",
        "metric": "count",
        "period": "day",
        "limit": 100,
        "tiers": { "free": 10, "pro": 100 }
      },
      {
        "resource": "llm",
        "metric": "tokens",
        "period": "month",
        "limit": 100000,
        "scope": "user",
        "gateAction": "block"
      },
      {
        "resource": "api",
        "metric": "count",
        "period": "minute",
        "limit": 60,
        "conditions": { "roles": ["viewer"] }
      }
    ]
  }
}

Resource Types

ResourceDescription
workflowWorkflow executions
llmLLM/AI calls
apiAPI requests
embeddingEmbedding operations
jobQueue job submissions

Metrics

MetricDescription
countNumber of operations
tokensToken usage (LLM)
costCost in cents

Periods

PeriodDescription
minutePer-minute limit
hourPer-hour limit
dayPer-day limit
monthPer-month limit

Gate Actions

ActionBehavior
blockReject request (default)
warnAllow but return warning
upgradeSuggest tier upgrade

Conditions

Apply limits selectively:

json
{
  "resource": "llm",
  "metric": "tokens",
  "period": "day",
  "limit": 1000,
  "conditions": {
    "roles": ["viewer"],
    "userTier": "free"
  }
}

SDK Usage

Check Limits Before Action

typescript
// Check if user can execute a workflow
const result = await bf.tenant.limits.resources.check('workflow', 'count', {
  userId: 'user-123',
  amount: 1,
  context: { userTier: 'free' }
});

if (!result.allowed) {
  console.log(result.reason); // "workflow count limit exceeded for day (10/10)"
  console.log(result.upgrade); // { tier: 'pro', newLimit: 100 }
}

Get Usage Stats

typescript
// Get all resource usage
const usage = await bf.tenant.limits.resources.getUsage('user-123');

// Get specific resource usage
const llmUsage = await bf.tenant.limits.resources.getUsage('user-123', 'llm');

Track Usage Manually

typescript
// Track LLM token usage
await bf.tenant.limits.resources.track('llm', 'tokens', {
  userId: 'user-123',
  amount: 1500
});

Entity Limits

typescript
// Check entity limit
const canCreate = await bf.tenant.limits.check(
  'user-123',
  'projects',
  'create',
  'free' // user tier
);

// Get usage stats
const usage = await bf.tenant.limits.getUsage('user-123');

Storage Limits

typescript
// Check if file upload allowed
const result = await bf.tenant.limits.storage.check('user-123', 5000000); // 5MB

// Get storage usage
const stats = await bf.tenant.limits.storage.getUsage('user-123');

API Endpoints

Resource Limits

MethodEndpointDescription
GET/tenant/limits/resourcesGet resource limits config
PUT/tenant/limits/resourcesUpdate resource limits
GET/tenant/limits/resources/usageGet usage statistics
POST/tenant/limits/resources/checkCheck if action allowed
POST/tenant/limits/resources/trackTrack resource usage

Entity Limits

MethodEndpointDescription
GET/tenant/limitsGet limits config
PUT/tenant/limitsUpdate limits config
GET/tenant/limits/usageGet usage stats
POST/tenant/limits/checkCheck if action allowed

Storage Limits

MethodEndpointDescription
GET/tenant/limits/storageGet storage config
PUT/tenant/limits/storageUpdate storage config
GET/tenant/limits/storage/usageGet storage usage
POST/tenant/limits/storage/checkCheck upload allowed

Automatic Enforcement

Resource limits are automatically enforced in:

  • Workflow Executor: Checks workflow limits before execution
  • LLM Routes: Can track token usage via callbacks
  • Queue Jobs: Checked before job submission

For custom enforcement, use the check/track APIs before operations.

Released under the ISC License.