> ## 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.

# Control layers

> Decide how much TensorCost may do with each call, from watching it to routing it, and raise that step by step.

Control is a ladder. The SDK sets how far up it is allowed to go, and the console sets how far TensorCost actually goes today.

| Layer | Path | What happens |
| - | - | - |
| `off` | Direct | Nothing. The client comes back unwrapped. |
| `observe` | Direct | Telemetry only. This is the default. |
| `govern` | Direct | A metadata-only check before each call. Budgets, approved models, guardrails and compliance profiles can refuse it. |
| `steer` | Via proxy | The call goes through the TensorCost proxy, which can apply policy to it. |
| `route` | Via proxy | The proxy can send the call to another model under your routing policies. |

## The layer you set is a ceiling

TensorCost uses the lower of two settings. The diagram shows the ladder, with what each step adds, and how the two settings combine.

```mermaid theme={null}
flowchart LR
  L0["off<br/>nothing, client unwrapped"] --> L1["observe<br/>records model, tokens, cost"]
  L1 --> L2["govern<br/>asks first, can refuse"]
  L2 --> L3["steer<br/>via proxy, policy applies"]
  L3 --> L4["route<br/>proxy may swap the model"]
  C["maxLayer in your code"] --> E{{"Effective layer:<br/>the lower of the two"}}
  K["Published on<br/>Authority › Routing"] --> E
```

The two settings are: `maxLayer` (Node.js) or `max_layer` (Python) in your code, and the layer published in the console under **Authority › Routing**. You can turn control up or down in the console without a deploy, and your code can never be pushed past its own ceiling. Pausing routing holds the published layer at `govern`, and without a content-retention grant a published `steer` or `route` also runs as `govern`.

```ts Node.js theme={null}
const client = wrap(new OpenAI(), {
  maxLayer: "route",
  proxyUrl: process.env.TENSORCOST_PROXY_URL,
});
```

```python Python theme={null}
client = wrap(
    OpenAI(),
    max_layer="route",
    proxy_url=os.environ["TENSORCOST_PROXY_URL"],
)
```

`steer` and `route` need a proxy URL, which is `https://api.tensorcost.com`; `wrap()` fails without one. `govern` does not need it.

The SDK caches the console's layer for 30 seconds and never waits more than 250 ms for it. If TensorCost cannot be reached for five minutes, the SDK settles at `govern`. It keeps checking, and it never raises itself to `route` on its own.

## Roll out one step at a time

**1. Observe**

Wrap your clients and leave everything at the default. Check that spend, customers and agents look right.

**2. Govern**

Set `maxLayer` to `govern` and publish the same layer in **Authority › Routing**. Budgets, approved models and compliance profiles now apply before each call.

**3. Route**

Set `maxLayer` to `route` and supply the proxy URL. A call moves to another model only when a routing policy for it is switched on. Until then it goes to your provider unchanged, and the decision is recorded in **Activity › Decisions**.

Policies themselves, and how to preview them, are described in [Routing](https://docs.tensorcost.com/control/routing.md).

## What can refuse a call

At `govern` and above, a refused call raises an error from the SDK instead of reaching the provider. Every refusal extends one base class, `TensorCostRefusedError`, so a single `catch` covers them, including refusals a newer server adds.

| Error | When |
| - | - |
| `TensorCostBudgetHardCapError` | A budget's hard cap was reached. `retryable` is true when its spend is not counted yet. |
| `TensorCostPeriodBudgetExceededError` | A daily or monthly cap was reached. |
| `TensorCostRunBudgetExceededError` | A run reached its cap, where your workspace has enforcement for that layer. |
| `TensorCostRunStoppedError` | Someone stopped the run. |
| `TensorCostModelGovernanceDeniedError` | The model is not approved for this team. |
| `TensorCostGuardrailHardStopError` | A guardrail stopped the call. |
| `TensorCostComplianceDeniedError` | A compliance profile refused the call. |

```ts Node.js theme={null}
import { TensorCostRefusedError } from "@tensorcost/sdk";

try {
  await client.chat.completions.create({ model: "gpt-4o", messages });
} catch (err) {
  if (err instanceof TensorCostRefusedError) {
    console.warn("TensorCost refused the call:", err.code, err.reason);
  }
  throw err;
}
```

```python Python theme={null}
from tensorcost import TensorCostRefusedError

try:
    client.chat.completions.create(model="gpt-4o", messages=messages)
except TensorCostRefusedError as err:
    print("TensorCost refused the call:", err.code, err.reason)
    raise
```

A provider's own error, such as a bad request, reaches you as the provider SDK's error, as it would without TensorCost. Compliance profiles also run pattern detectors inside your process before the call, so a matched secret never leaves it; set the team (`teamId` or `team_id`) to apply that team's profile.

## Reliability

The SDK only goes around TensorCost when it cannot get an answer: a network error, a timeout, a 5xx, or a 429 or 402 with no refusal in it. Anything TensorCost actually answers reaches your code as an error and is never retried or sent around it.

| Behaviour | Detail |
| - | - |
| Retries | 5xx, network errors and 429s, with jittered backoff |
| Timeouts | The check before a call is capped at 500 ms. Through the proxy, a call waits up to 60 s. |
| Circuit breaker | Repeated proxy failures send calls straight to the provider until the proxy answers again |

> [!WARNING]
>
> If the proxy forwarded a call to the provider and then failed, going around it can bill the call twice. Where calls must never go around TensorCost, turn off fail-open (`failOpenEnabled: false` in Node.js, `fail_open_enabled=False` in Python); proxy failures then surface as errors.

## Limits by provider

Bedrock and Vertex sign requests inside their own SDKs, so they cannot use the proxy. `steer` and `route` are not available for them, and `govern` is accepted but not enforced yet; the SDK warns once. Anthropic at `steer` or `route` runs at `govern`. In Python, streamed calls at `steer` and `route` go straight to the provider.


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