> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tensorcost.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Running the install

> How a BYOC install scales, what it does when spend processing falls behind, the alerts to wire up, and the database checks it runs on every boot.

This page is for the platform team that runs the install from day to day. Where a section says "on Kubernetes" it means the TensorCost Helm chart; "on AWS" means the CloudFormation stack. Everything here applies from release byoc-v0.1.114 unless the section says otherwise.

## Scaling

An install is set up at a size band, chosen from the model calls a month you expect: S up to 1 million, M up to 5 million, L up to 20 million, XL above that. The band sets each service's CPU, memory and starting count.

At L and XL, three services scale on CPU. The gateway and the inference proxy, which sit on every model call's path, already did. From byoc-v0.1.114, ai-service does too. It turns each call's events into spend rows, and in load tests its fixed pods became a limit as calls rose.

| Service | L | XL |
| - | - | - |
| Gateway | 2 to 4 | 3 to 6 |
| Inference proxy | 2 to 6 | 3 to 10 |
| ai-service | 1 to 4 | 2 to 6 |

The scaler holds each service near an average of 600 millicores per pod or task. Each of these services is a single Node.js process that tops out a little above one vCPU however large the pod is, so the target is a fixed amount of CPU rather than a share of what the pod is given. At S and M autoscaling is off and the counts are fixed.

**On Kubernetes** the chart creates a HorizontalPodAutoscaler for each of the three. The autoscaler reads the cluster's metrics API. AKS ships metrics-server. On EKS, install the metrics-server add-on first, or the autoscaler never moves off its minimum. While an autoscaler scales a service it owns the replica count: an upgrade leaves the count where it is, and you should not `kubectl scale` those Deployments. If a GitOps tool applies the chart's output, tell it to ignore `.spec.replicas` on the gateway, proxy and ai-service Deployments, or its first sync can drop them to one pod. If you already run your own autoscaler on any of them, remove it or set `sizing.autoscale: false`, because two autoscalers on one Deployment fight each other. `sizing.autoscale: true` turns autoscaling on at S and M. The high-availability values file raises ai-service's minimum to 2.

**On AWS** the stack adds an ECS target-tracking policy for each of the three, at the same minimums and maximums. While it is on, Application Auto Scaling owns the task count, and a stack update leaves it alone.

The same three services answer their liveness check on a port of their own (4199), from a separate thread. It fails only when the service's main event loop has stopped turning for 30 seconds, so a pod that is busy, however busy, is not restarted. On Kubernetes, a busy pod that can't answer its readiness check in time is taken out of rotation until it catches up. Leave the liveness port at its default.

## When spend processing falls behind

Services write the spend and audit events they produce to an outbox table in Postgres, and a relay publishes them to Redis streams. The services that record spend and audit entries read those streams as consumer groups. Each stream is capped at about 100,000 entries, which keeps Redis's memory bounded.

In earlier releases, a consumer group that fell a full cap behind (for example while ai-service was down) could lose the oldest entries it hadn't read yet when Redis trimmed the stream. From byoc-v0.1.114 the spend and audit streams don't lose them. When a consumer group that is still running falls a full cap behind, the relay stops adding to that stream and keeps the new rows in the Postgres outbox. Other streams keep flowing. When the group catches up, the relay writes the held rows. Nothing is sent twice, so nothing is counted twice. Spend shows up late, not missing.

A consumer group is in one of three states.

| State | When | What the relay does |
| - | - | - |
| Live | One of its consumers has read within the last 15 minutes | Holds the stream when the group is a full cap behind |
| Absent | No consumer has read for 15 minutes | Still holds, and reports the group as absent |
| Dead | No consumer has read for 6 hours, or the group has no consumers at all | Stops holding. The stream is trimmed by length again, past entries the group hasn't read |

So ai-service can be down for up to 6 hours without losing spend. Other streams, such as traces and alerts, are trimmed by length as before.

Held rows don't count toward the inference proxy's outbox check, which answers SDK observations with 429 when undelivered rows pile up. That check counts only rows the relay could send and hasn't, such as during a Redis outage, so a hold does not push back on your applications.

### What a hold costs in Postgres

We measured about 1.42 KB per held row, including its indexes, on 50,000 synthetic proxy rows. At that size, 6 hours held at 320 calls a second would be about 6.9 million rows, or about 9.1 GiB. That figure is arithmetic from the measured row size, not a measurement of a 6-hour hold. The rows would be in the table anyway, since delivered outbox rows are kept for 30 days. A hold only delays marking them delivered. Still, check the database has that much free space if a hold might run for hours.

### Settings

These are environment variables the services read. The defaults suit every install we know of. Change them only with the TensorCost team.

| Variable | Default | What it sets |
| - | - | - |
| `EVENT_BUS_GROUP_ABSENT_MS` | `900000` (15 minutes) | How long a group can go without a consumer before it is reported absent. Never longer than the live window |
| `EVENT_BUS_LIVE_GROUP_WINDOW_MS` | `21600000` (6 hours) | How long a group can go without a consumer before it is dead and stops holding |
| `OUTBOX_HELD_ALARM_AGE_S` | `21600` (6 hours) | The age of the oldest held row past which the relay logs `outbox-relay.stream_held_long`. It keeps holding |
| `OUTBOX_HELD_ALARM_ROWS` | `7000000` | The number of held rows past which it logs the same line |

On Kubernetes the chart has no value for them. If the TensorCost team asks you to change one, add it to the app secret the services load (`appSecrets.existingSecret`). The alert rules below are written for the defaults, so change the matching rule as well. On AWS they are fixed at their defaults.

### Metrics

On Kubernetes these are on each service's metrics port when `metrics.enabled` is on.

| Metric | What it is |
| - | - |
| `tc_stream_lag_ratio{stream,group,guarded}` | A consumer group's unread plus unacknowledged entries, divided by the stream's cap. At 1 a spend or audit group's stream is held. `guarded` is `true` for spend and audit groups, `dead` for one that has stopped holding, and `false` for the rest |
| `tc_outbox_held_rows{schema}` | Rows held in a service's outbox |
| `tc_outbox_oldest_held_age_seconds{schema}` | Age of the oldest held row |
| `tc_stream_trimmed_unacked_total{stream,group}` | Entries trimmed from a stream after a consumer had picked them up and before it finished them, with no copy left to retry from |

### Alerts

The chart's alert rules (`metrics.prometheusRule.enabled`) include six for this.

| Alert | Severity | What it means | What to do |
| - | - | - | - |
| TensorCostStreamLagNearCap | warning | A spend or audit consumer group has been more than half a cap behind for 5 minutes | Check that the group's service (ai-service, cost-service or audit-service) is up and keeping pace |
| TensorCostStreamHeld | critical | A group has been a full cap behind for 10 minutes, and the relay is holding its stream | Bring the group's service back. The outbox drains on its own. Spend and audit entries arrive late, not lost |
| TensorCostStreamGroupAbsent | warning | A spend or audit group has had no consumer for 15 minutes. It is still holding | Its service is down or gone. Bring it back within 6 hours and nothing is lost |
| TensorCostStreamGroupDead | critical | A group has had no consumer for 6 hours. It has stopped holding, and the stream is being trimmed past entries it hasn't read | If you removed that service on purpose, delete its consumer group in Redis (`XGROUP DESTROY`). Otherwise bring the service back and contact TensorCost support to resend the outbox rows from the time in the `event-bus.stream_group_dead` log line |
| TensorCostOutboxHeldLong | critical | A service's outbox has held rows for 6 hours, or holds 7 million of them. They are still held, not refused | Check the consumer group named in the `outbox-relay.stream_held_long` log line, and the database's free space |
| TensorCostStreamTrimmedUnacked | critical | An entry was trimmed before its consumer finished it: it was in flight when the consumer stopped, and the stream moved a full cap past it | Contact TensorCost support to resend the outbox rows around the time in the `event-bus.stream_trimmed_unacked` log line |

Outbox rows are kept for 30 days, so a resend has that long to run.

On AWS, this release has no CloudWatch alarms of their own for these. A hold that lasts longer than 15 minutes still shows up: the stack's OutboxBacklog alarm watches the oldest undelivered outbox row, held rows included, and **Settings › Support** reports events that have waited more than 15 minutes. The log lines above go to each service's CloudWatch log group, where you can add your own metric filters.

## Database checks on every boot

These shipped in byoc-v0.1.112.

### The RLS tripwire

TensorCost keeps each workspace's data apart in Postgres with row-level security. On top of that, every query a service sends on behalf of a workspace is checked for the shapes SQL injection takes. A hit writes a structured warning, `rls.tripwire`, at most once a minute per statement shape. It names the service, the route when known, the rule that matched and a hash of the statement with its values blanked out, never the SQL or its values. It also counts the hit in `tc_rls_tripwire_total{rule}`.

The setting is `TC_RLS_TRIPWIRE`, and it is `log` by default: the warning is written and the statement still runs. `enforce` also refuses the statement, so the request that sent it fails. `off` checks nothing.

| Where | How to set it |
| - | - |
| Kubernetes | `rlsTripwire: log` in your values file. `enforce` and `off` are the other values |
| AWS | Fixed at `log` |

Run `log` and read the hits before you set `enforce`. Let it run against your real traffic for a while, search the services' logs for `rls.tripwire`, and send any hit to TensorCost support with its service, route, rule and hash. A hit from normal use is a statement the check doesn't know yet, and under `enforce` it would refuse that request. Turn on `enforce` only once log mode has been quiet.

### Legacy database roles are locked

Older releases created database roles named `tensorcost_jobs` and `tensorcost_ml` that no service on a BYOC install signs in as. After every migration pass, each service now takes away their login and password, and their `BYPASSRLS` attribute. You don't need to do anything for that part.

Taking `BYPASSRLS` away needs more privilege than some managed databases give their admin user. Where the install's database admin isn't allowed to, as with the RDS master user or the Azure Database for PostgreSQL admin, the roles are left unable to sign in but keep `BYPASSRLS`, and each boot logs a warning that names the role and gives the statement to run, for example:

```sql theme={null}
ALTER ROLE tensorcost_jobs NOBYPASSRLS;
```

Have someone with a superuser role on the database server (on RDS, `rds_superuser` where it is allowed) run the statement from the warning once. The warning stops on the next boot. If your database service gives nobody that privilege, the roles stay unable to sign in and the warning repeats on each boot; tell TensorCost support. If a warning instead says a role can still sign in, run the statement it gives, which takes away the login and password.

## Background jobs

**Settings › Support › Background jobs** lists each scheduled job with its last run, and the error when a run failed. On Kubernetes, the chart's alert rules include TensorCostJobFailing, which fires when a job's last run has failed for 2 hours, and TensorCostJobStale, which fires when a job has had no successful run in twice its interval plus an hour. Both are warnings, so a restart or a short outage doesn't page anyone. On AWS, the JobsStale alarm fires when a job hasn't run in twice its interval plus an hour.

## Measured capacity

These figures come from load tests on a test cluster on AKS, with five Standard D4s\_v4 application nodes and one D4ds\_v5 Azure Database for PostgreSQL flexible server, running one workspace with SDK, proxy and gateway traffic mixed 4 : 3 : 1. The per-workspace rate limits were raised for the test, so these rates are above what the size bands' default limits let through.

| Release | Load | Result |
| - | - | - |
| Test build v0.1.113, between byoc-v0.1.112 and byoc-v0.1.114 | 5-minute steps from 64 calls a second | Clean through 256 calls a second: no errors. p99 at 256 calls a second: 154 ms through the proxy, 58 ms for the SDK, 59 ms through the gateway. Every call's spend recorded once |
| byoc-v0.1.114 | 128 calls a second, with ai-service stopped for 10 minutes | Spend was exact after the backlog drained: every accepted call had its spend row, and none was counted twice. ai-service scaled to 4 pods to work off the backlog |

For scale, and as arithmetic rather than a measurement: 256 calls a second, sustained, is about 660 million calls in a 30-day month. Band L is sized for up to 20 million.

These are the highest rates measured so far, not a ceiling. If you expect sustained load near them, talk to the TensorCost team about the database size first.


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