# Rate Limit

> Throttles the number of executions allowed within a specific time window.

Source: https://docs.sommatic.ai/en/nodes/flow.rate.limit/

- Slug: `flow.rate.limit`
- Family: Flow control
- Version: 1.0.0
- Status: Experimental. Nothing is enforced: the limit is only reported in `meta`, `throttled` is never emitted and execution always continues through `out`.
- Execution: sync

Throttles the number of executions allowed within a specific time window.

This operator enforces a rate limit policy (e.g., "10 requests per minute"). If the limit is exceeded, subsequent executions are routed to the 'throttled' port (or delayed, depending on engine configuration).

**Behavior:**
- **Key-based Throttling:** Can throttle globally or per-key (e.g., per user ID).
- **Strategies:** Fixed Window, Sliding Window (engine dependent).
- **Overflow:** Excess requests trigger the 'throttled' port immediately.

**Use Cases:**
- Protecting a fragile downstream API from overload.
- Enforcing usage quotas for different user tiers.
- Preventing spam or abuse.

## Configuration

- `limit` (integer, optional): Number of allowed executions in the window. Default: `10`.
- `window_seconds` (integer, optional): Time duration for the rate limit window. Default: `60`.
- `key_expr` (string, optional, accepts expressions): JSONata expression to derive the unique key. If empty, applies globally.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `out` (default): Execution allowed (within limit).
- `throttled`: Execution blocked (limit exceeded).
- `error`: Internal error.

## Input contract

- `payload` (object, optional)

## Output contract

- `payload` (object, optional)

## In expressions

What a later node can read from this node's output with `$("Rate Limit").item.json.<path>` (write your node's label instead of `Rate Limit`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Rate Limit").item.json.payload` (object) **The worker does not return it: reads as undefined.**
- `$("Rate Limit").item.json.user_id` (string) = `"u-42"` (Not suggested by the editor)
- `$("Rate Limit").item.json.action` (string) = `"export"` (Not suggested by the editor)


## How it works

Computes a throttling key and passes the payload through `out` with the limit as metadata; no limit is enforced, because the engine never reads it.

1. Resolves the config; `limit` defaults to 10 and `window_seconds` to 60.
2. If the original `key_expr` text contains `{{`, the resolved value is used as the key (converted to a string). Otherwise `key_expr` is evaluated as JSONata against the payload (`user_id`); a falsy result or no `key_expr` gives `global`.
3. A JSONata error is ignored and the key stays `global`.
4. Returns the payload unchanged through `out` with `meta.rate_limit` `{ key, limit, window }`. The engine ignores it: every execution passes.

## In a workflow

### Node

```json
{
  "node_id": "node-limit-exports",
  "label": "Limit exports",
  "operator_slug": "flow.rate.limit",
  "operator_version": "1.0.0",
  "config": {
    "limit": 10,
    "window_seconds": 60,
    "key_expr": "{{ $json.user_id }}"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "user_id": "u-42",
    "action": "export"
  },
  "port": "out",
  "meta": {
    "rate_limit": {
      "key": "u-42",
      "limit": 10,
      "window": 60
    }
  }
}
```

## Output ports (behaviour in the code)

### `out`

Always.

```json
{
  "user_id": "u-42",
  "action": "export"
}
```

### `throttled`

Never emitted: no counter is kept.

### `error`

Never emitted.

## Behaviour in the code

- `limit`: Only echoed in `meta.rate_limit`; `window_seconds` is echoed as `window`.

## Notes and limits

- Not implemented: there is no shared counter, so `throttled` never fires. Rate-limit at the called service or space calls with your own logic.
- `meta` is recorded in the run steps only: downstream nodes receive `data` as their payload and cannot read `meta` through `$json` or `$("Node")`.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "flow.rate.limit",
  "name": "Rate Limit",
  "category": "flow",
  "version": "1.0.0",
  "description": "Throttles the number of executions allowed within a specific time window.\n\n  This operator enforces a rate limit policy (e.g., \"10 requests per minute\"). If the limit is exceeded, subsequent executions are routed to the 'throttled' port (or delayed, depending on engine configuration).\n\n  **Behavior:**\n  - **Key-based Throttling:** Can throttle globally or per-key (e.g., per user ID).\n  - **Strategies:** Fixed Window, Sliding Window (engine dependent).\n  - **Overflow:** Excess requests trigger the 'throttled' port immediately.\n\n  **Use Cases:**\n  - Protecting a fragile downstream API from overload.\n  - Enforcing usage quotas for different user tiers.\n  - Preventing spam or abuse.",
  "capabilities": [
    "throttle",
    "limit",
    "control-flow"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "limit": {
        "type": "integer",
        "default": 10,
        "title": "Max Executions",
        "description": "Number of allowed executions in the window."
      },
      "window_seconds": {
        "type": "integer",
        "default": 60,
        "title": "Window (Seconds)",
        "description": "Time duration for the rate limit window."
      },
      "key_expr": {
        "type": "string",
        "title": "Throttling Key",
        "format": "expression",
        "description": "JSONata expression to derive the unique key. If empty, applies globally."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "payload": {
        "type": "object"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "payload": {
        "type": "object"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Passed",
      "is_default": true,
      "description": "Execution allowed (within limit)."
    },
    {
      "port_id": "throttled",
      "label": "Throttled",
      "description": "Execution blocked (limit exceeded)."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Internal error."
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "throttle",
    "limit",
    "quota"
  ],
  "is_stateful": true,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "limit": {
      "type": "integer",
      "default": 10,
      "title": "Max Executions",
      "description": "Number of allowed executions in the window."
    },
    "window_seconds": {
      "type": "integer",
      "default": 60,
      "title": "Window (Seconds)",
      "description": "Time duration for the rate limit window."
    },
    "key_expr": {
      "type": "string",
      "title": "Throttling Key",
      "format": "expression",
      "description": "JSONata expression to derive the unique key. If empty, applies globally."
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "payload": {
      "type": "object"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "payload": {
      "type": "object"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Passed",
    "is_default": true,
    "description": "Execution allowed (within limit)."
  },
  {
    "port_id": "throttled",
    "label": "Throttled",
    "description": "Execution blocked (limit exceeded)."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Internal error."
  }
]
```
