> ## Documentation Index
> Fetch the complete documentation index at: https://sonar-cf3aeac7.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Limits and subscription cost

> How multi-events subscriptions are priced against the per-connection budget, plus message, rate, buffer and heartbeat limits, close codes and error codes.

Each connection has a budget. Every subscription has a cost that depends on its chains, event kinds and filter, and the sum of all subscriptions on a connection must stay within the budget. Other limits protect the connection itself.

All values on this page are current defaults and may change.

## How a filter is priced

The cost is calculated **per pair** (one chain and one event kind):

* A **chain-wide** pair costs **10**. A pair is chain-wide when it has no filter, or when its filter doesn't pin any address. The server has to check a chain-wide filter against every trade on the chain.
* Any other pair costs **1 for every started 500 pinned addresses**: 1 to 500 addresses cost 1, 501 to 1,000 cost 2, and so on.

**Pinned addresses** are the `poolAddress`, `tokenAddress` and `sender` values in `eq` and `in` conditions. How they combine:

| Part of the filter | Pins |
| - | - |
| `eq` / `in` on `poolAddress`, `tokenAddress` or `sender` | Its values |
| `eq` / `in` on `poolType` or `type` | Nothing |
| `gt`, `gte`, `lt`, `lte` on `tokenAmountUsd` | Nothing |
| `and` | The pins of its **narrowest** pinned child (the one with the fewest values). Children that pin nothing are ignored. If no child pins anything, nothing. |
| `or` | The **union** of its children's pins. If **any** child pins nothing, the whole `or` pins nothing and the pair is chain-wide. |

Examples, per pair:

| Filter | Pinned | Cost |
| - | - | - |
| None | Nothing | 10 |
| `{ "gte": ["tokenAmountUsd", 100000] }` | Nothing | 10 |
| `{ "in": ["poolType", ["uniswap-v3", "uniswap-v4"]] }` | Nothing | 10 |
| `{ "eq": ["tokenAddress", WETH] }` | 1 token | 1 |
| `{ "in": ["sender", [500 wallets]] }` | 500 wallets | 1 |
| `{ "or": [{ "in": ["sender", [500 wallets]] }, { "eq": ["poolAddress", P] }] }` | 501 addresses | 2 |
| `{ "and": [{ "in": ["sender", [A, B]] }, { "eq": ["poolAddress", P] }, { "gte": ["tokenAmountUsd", 1] }] }` | Only `P`, the narrowest | 1 |
| `{ "and": [{ "eq": ["tokenAddress", WETH] }, { "eq": ["type", "buy"] }, { "gte": ["tokenAmountUsd", 100000] }] }` | 1 token | 1 |
| `{ "or": [{ "in": ["sender", [A]] }, { "gte": ["tokenAmountUsd", 100000] }] }` | Nothing: the `gte` branch pins nothing | 10 |

## Subscription cost and the connection budget

A subscription costs the **sum of its pairs**. Each pair is priced with its own filter, so a [per-kind filter map](https://sonar-cf3aeac7.mintlify.site/streams/filters.md#one-filter-or-one-per-event-kind) can mix cheap and chain-wide pairs.

The total cost of all subscriptions on one connection must be **100 or less**. A subscribe that would go over is refused:

```json theme={null}
{
  "type": "error",
  "id": "third",
  "code": "SUBSCRIPTION_LIMIT",
  "message": "At most 100 subscriptions, each entry in a list counts as one"
}
```

> [!NOTE]
>
> The message text doesn't describe this channel's cost model. On the multi-events stream, the 100 is a budget of cost units calculated as described on this page, not a count of subscriptions or list entries.

Unsubscribing frees the cost of that subscription right away.

### Worked examples

**Four chain-wide pairs: 40.**

```json theme={null}
{ "chains": ["evm:1", "evm:8453"], "events": ["swap", "swap-enriched"] }
```

2 chains × 2 kinds = 4 pairs, each chain-wide: 4 × 10 = **40**. Two of these subscriptions fit on one connection (80). A third (120) is refused with `SUBSCRIPTION_LIMIT`.

**A mixed map: 22.**

```json theme={null}
{
  "chains": ["evm:1", "evm:8453"],
  "events": ["swap", "swap-enriched"],
  "filters": {
    "swap": { "eq": ["sender", "0xd0b53d9277642d899df5c87a3966a349a798f224"] },
    "swap-enriched": { "gte": ["tokenAmountUsd", 100000] }
  }
}
```

| Pair | Filter | Cost |
| - | - | - |
| `swap` on `evm:1` | One wallet | 1 |
| `swap-enriched` on `evm:1` | USD threshold, chain-wide | 10 |
| `swap` on `evm:8453` | One wallet | 1 |
| `swap-enriched` on `evm:8453` | USD threshold, chain-wide | 10 |
| **Total** | | **22** |

### Keeping costs down

* Pin something. A filter that names tokens, pools or wallets costs 1 per 500 of them instead of 10.
* Ask for one event kind. `swap-enriched` contains every `swap` field, so requesting both doubles the cost for the same trades.
* Only list the chains you need. Each chain adds its own pairs.
* [Sampling](https://sonar-cf3aeac7.mintlify.site/streams/subscribing.md#sampling) reduces the number of events, not the cost.

## Connection limits

| Limit | Value | What happens |
| - | - | - |
| Subscription budget | 100 cost units per connection | `SUBSCRIPTION_LIMIT` error; the subscription isn't created |
| Inbound message size | 64 KiB (65,536 bytes) per message | Connection closed with `1009` |
| Inbound message rate | 20 messages per connection in each 1-second window. Every message you send counts, including `ping` ops and invalid messages. WebSocket pong frames don't count. | Connection closed with `4029`, reason `Too many messages` |
| Send buffer | 4 MiB of data the server couldn't deliver yet | Connection closed with `4008`, reason `Too slow, reconnect and resume from your cursor` |
| Heartbeat | Server sends a WebSocket ping every 30 seconds | No pong by the next ping: connection dropped without a close frame (`1006`) |
| Server capacity | Per server instance | Upgrade refused with HTTP `503` |
| `maxUpdatesPerMinute` | 1 to 600 | `INVALID_PARAMETERS` |
| Filters | 3 levels, 50 conditions, 500 values per list | `INVALID_PARAMETERS`. See [Filters](https://sonar-cf3aeac7.mintlify.site/streams/filters.md#limits) |
| Resume history | About 10 minutes | `RESUME_EXPIRED`. See [Cursors and resuming](https://sonar-cf3aeac7.mintlify.site/streams/cursors-and-resume.md#resume_expired) |

A full list of 500 addresses takes about 22 KB of JSON, so a subscribe message can hold at most two full lists before it reaches 64 KiB.

### Heartbeat

The server sends a WebSocket protocol ping frame every `heartbeatMs` (30,000 ms, also given in the `welcome` message). Browsers and most WebSocket libraries answer with a pong automatically. If you turned automatic pongs off, answer them yourself.

The application-level `{"op":"ping"}` doesn't count as a pong. It doesn't keep the connection alive and it counts toward the 20-messages-per-second limit.

### Slow consumers

If your client reads more slowly than trades arrive, data piles up on the server. Past 4 MiB the server closes the connection with `4008`. Reconnect and resume from your cursors. If it keeps happening, narrow the filter, request fewer chains or kinds, or move processing off the thread that reads the socket.

## Close codes

| Code | Reason | When | What to do |
| - | - | - | - |
| `1006` | none (no close frame) | Missed heartbeat pong, or the connection dropped | Reconnect and resume |
| `1009` | set by the WebSocket library | A message you sent is larger than 64 KiB | Send smaller subscribe messages, for example split address lists across subscriptions |
| `1012` | `Server restarting, reconnect and resume` | Server restart. Preceded by `{"type":"reconnect"}` | Reconnect and resume |
| `4008` | `Too slow, reconnect and resume from your cursor` | Send buffer over 4 MiB | Reconnect, resume, and read faster |
| `4029` | `Too many messages` | More than 20 messages in one second | Reconnect and send fewer messages |

Subscriptions are removed when a connection closes. After any of these, subscribe again on the new connection. See the [reconnect recipe](https://sonar-cf3aeac7.mintlify.site/streams/cursors-and-resume.md#reconnect-recipe).

## Error codes

Errors are sent as `{ "type": "error", "id"?, "code", "message", "details"? }` and don't close the connection. Codes the multi-events stream can send:

| Code | `id` | Message | Cause |
| - | - | - | - |
| `BAD_REQUEST` | No | `Messages must be JSON text` | Binary frame or invalid JSON |
| `INVALID_PARAMETERS` | No | Field-level messages | The message itself is invalid: unknown `op`, bad or missing `id`, malformed `since` |
| `INVALID_PARAMETERS` | Yes | Field-level messages, with `details` | Invalid `params`, including [filter errors](https://sonar-cf3aeac7.mintlify.site/streams/filters.md#validation-errors) |
| `INVALID_PARAMETERS` | Yes | `This subscription has several pairs, since must map each pair to its cursor` | `since` is a string on a multi-pair subscription |
| `INVALID_PARAMETERS` | Yes | `Unknown pairs in since` | `since` names a pair the subscription doesn't have |
| `INVALID_PARAMETERS` | Yes | `A sampled subscription drops items and cannot resume, subscribe without since or maxUpdatesPerMinute` | `since` combined with `maxUpdatesPerMinute` |
| `UNKNOWN_CHANNEL` | Yes | `Unknown channel: <name>` | `channel` isn't `multi-events` |
| `DUPLICATE_SUBSCRIPTION` | Yes | `Subscription <id> already exists` | The id is already active on this connection |
| `SUBSCRIPTION_LIMIT` | Yes | `At most 100 subscriptions, each entry in a list counts as one` | Over the connection budget |
| `RESUME_EXPIRED` | Yes | `Cursor is older than the stream history` | A `since` cursor is older than the history. See [details](https://sonar-cf3aeac7.mintlify.site/streams/cursors-and-resume.md#resume_expired) |
| `NOT_FOUND` | Yes | `Unknown subscription <id>` | `unsubscribe` with an id that isn't active |
| `INTERNAL_ERROR` | Yes | `Subscription failed, try again` | Unexpected server failure while subscribing. Retry. |

After a subscribe error, the id is free to use again, except after `DUPLICATE_SUBSCRIPTION`, where the existing subscription keeps it.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.