Metadata-Version: 2.5
Name: tensorcost-litellm
Version: 0.1.5
Summary: A LiteLLM proxy guardrail that checks TensorCost budgets before each call, and a logger that reports spend back.
Project-URL: Homepage, https://github.com/vaadhlabs/tensorcost-integrations/tree/main/litellm
Project-URL: Repository, https://github.com/vaadhlabs/tensorcost-integrations
Project-URL: Issues, https://github.com/vaadhlabs/tensorcost-integrations/issues
Author: Vaadh Labs
License-Expression: Apache-2.0
Keywords: budget,cost,guardrail,litellm,llm
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.9
Requires-Dist: httpx>=0.27
Provides-Extra: proxy
Requires-Dist: litellm[proxy]>=1.60; extra == 'proxy'
Provides-Extra: test
Requires-Dist: fastapi>=0.110; extra == 'test'
Requires-Dist: pytest>=8; extra == 'test'
Description-Content-Type: text/markdown

# tensorcost-litellm

TensorCost for the [LiteLLM](https://github.com/BerriAI/litellm) proxy. Two
pieces, both loaded by the proxy from its config:

- `TensorCostAdmit`, a pre-call guardrail. Before each call it asks
  TensorCost (`POST /v1/gateway/admit`) whether the call fits the budgets
  that cover its team, user and key, and does what the answer says: lets it
  through, refuses it with a 429 that names the budget and who can raise it,
  or switches it to the model a routing policy picked.
- `spend_logger`, a callback. After each call it sends the call's spend row
  to TensorCost (`POST /v1/gateway/logs?format=litellm`), so the budget
  counts what the call cost.

## Install

Install it into the same Python environment as the proxy:

    pip install "tensorcost-litellm @ git+https://github.com/vaadhlabs/tensorcost-integrations#subdirectory=litellm"

It isn't on PyPI yet. Once the first release is published,
`pip install tensorcost-litellm` will do the same. Pin a commit
(`...tensorcost-integrations@<sha>#subdirectory=litellm`) if you want the
install to repeat exactly.

## Configure

Create a LiteLLM gateway connection in TensorCost (Connect › Gateways). It
gives you a key once. Then add this to the proxy's `config.yaml`:

```yaml
guardrails:
  - guardrail_name: tensorcost
    litellm_params:
      guardrail: tensorcost_litellm.TensorCostAdmit
      mode: pre_call
      default_on: true
      api_base: os.environ/TENSORCOST_URL
      api_key: os.environ/TENSORCOST_GATEWAY_KEY
      fail_mode: open

litellm_settings:
  callbacks: ["tensorcost_litellm.spend_logger"]

general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY
```

and set the environment:

    TENSORCOST_URL=https://<your TensorCost API host>/api/inference-proxy
    TENSORCOST_GATEWAY_KEY=tcgw_...
    LITELLM_MASTER_KEY=<a long random secret>

`LITELLM_MASTER_KEY` is LiteLLM's own admin key, not ours. We've seen
LiteLLM 1.110 refuse to start without one (`UnsafeMasterKeyError`), so a
config with only the TensorCost settings can fail at startup. If your proxy
already has a master key, keep it.

`default_on: true` runs the check on every call; without it LiteLLM runs a
guardrail only for requests that name it. `callbacks` takes the module-level
instance `spend_logger`, not the class: LiteLLM's callback setting names an
instance.

Guardrail settings, all optional:

| Setting | Default | What it does |
| --- | --- | --- |
| `fail_mode` | `open` | What to do when TensorCost doesn't answer in time or answers with an error: `open` lets the call through, `closed` refuses it with a 503. Once TensorCost has answered, the workspace's own setting (sent with every answer) replaces this one until the proxy restarts. |
| `timeout_seconds` | `1.0` | How long the check may take. |
| `instance` | none | The connection's instance, when you gave it one in TensorCost. |
| `count_input_tokens` | `true` | Count the prompt's tokens with LiteLLM's token counter and send them, so the check is the call's worst case. |

## What the check sends

The call's callers (the key's team id, user id and key alias, and any
request tags), the model and its provider as the proxy's `model_list` names
them, the application (below), `max_tokens` (or `max_completion_tokens`),
the prompt's token count, whether it streams, and the models in
`model_list` a reroute may pick. It never sends the prompt.

Those go as `accepts_models`, one entry per `model_name` with the
deployment it routes to, then the bare names:

```json
"accepts_models": [
  {"name": "azure-gpt-5.4-mini", "model": "gpt-5.4-mini", "provider": "azure"},
  {"name": "claude-haiku", "model": "us.anthropic.claude-haiku-4-5-20251001-v1:0", "provider": "bedrock"},
  "azure-gpt-5.4-mini",
  "claude-haiku"
]
```

An entry for a deployment with a `base_model` (below) carries the base model
as `model` and the deployment's own name as `deployment`. TensorCost matches a
routing policy's target model against each entry's `model` and `provider`, and answers a reroute with the entry's `name`, so a
`model_name` can be any alias you like. Up to 0.1.3 the guardrail sent only
the names, and a reroute fired only when a `model_name` was spelled like the
target model. The bare names are still sent for a TensorCost that reads only
strings; it drops the objects (the answer's `detail` names them as ignored)
and matches the names as before. A wildcard route (`bedrock/*`) goes only as
a bare name.

It also mints a correlation id for the call, puts it in the request's
metadata under `tensorcost_correlation_id` (over any value the caller sent),
and sends it with the check, so the call's spend row finds its decision.

### The end customer

To have a call judged against one of your customers' margin floors and fixed
limits, name the customer in the request's metadata under
`tensorcost_customer`, the LiteLLM side of the SDK's `x-tc-customer` header:

```python
client.chat.completions.create(
    model="claude-sonnet",
    messages=[...],
    extra_body={"metadata": {"tensorcost_customer": "acme"}},
)
```

The check sends it as `customer`, and the logger puts it on the call's spend
row (`metadata.tensorcost_customer`), so the call counts toward that customer.
It is trimmed and at most 128 characters; a longer one, or one with control
characters, is left off. A cached allow is kept per customer, so one
customer's allow never covers another's call. A call with no customer is
judged by no customer's limit.

If your app also goes through the TensorCost SDK with LiteLLM as its base
URL, send the customer here too: TensorCost counts that call once, from
LiteLLM's row, and a LiteLLM row with no customer leaves the call off the
customer's figures.

### The application

A routing policy can be scoped to an application. The check sends the call's
application as `application`, the first of:

1. an `x-tc-application` header the caller sent (the header the SDK and
   TensorCost's proxy use);
2. `tensorcost_application` in the request's metadata;
3. an `application=` or `app=` tag (`application:` and `app:` work too);
4. LiteLLM's end user (the request body's `user`);
5. the key's alias.

```python
client.chat.completions.create(
    model="claude-sonnet",
    messages=[...],
    extra_headers={"x-tc-application": "support-bot"},
)
```

The logger works it out the same way from the same request and puts it on
the call's spend row (`metadata.tensorcost_application`), so the row's
application is the one the check was matched on. The last three steps are the
order TensorCost already read a LiteLLM row's application in. It is trimmed
and at most 256 characters; a longer one, or one with control characters, is
skipped for the next. A cached allow is kept per application.

## Decisions

- **allow**: the call goes on unchanged.
- **refuse**: the guardrail raises HTTP 429 with
  `{"error": {"type": "budget_exceeded", "message": ...}}` and a
  `Retry-After` header when TensorCost gives one. The message names the
  budget and who can raise it. A refusal because TensorCost couldn't check
  the call (`check_unavailable`, `invalid_request`) is a 503
  `budget_check_unavailable` instead, so a client doesn't take an outage for
  a spent budget. A refusal no wait can change (`model_governance_denied`,
  `compliance_denied`, `run_stopped`) is a 403
  `{"error": {"type": "permission_denied", "reason": ...}}`, so a client
  doesn't back off and retry a call that can never pass.
- **reroute**: `model` is set to the `model_name` TensorCost names. The
  decision covers that one attempt. A name that isn't in `model_list` (and
  no wildcard route matches) would fail the call, so the call runs as asked
  instead and the guardrail logs a warning.

Every answered decision is an HTTP 200. Anything else means TensorCost
failed, and the fail mode decides.

Each gateway connection has an admit rate, 60,000 checks a minute unless a
TensorCost admin sets another on Connect › Gateways. Past it TensorCost
still answers 200, with the fail mode's decision and `detail: "throttled"`:
under open the call goes through unchecked, under closed it gets the 503.
The guardrail logs that once a minute. A 429 from the check means
TensorCost didn't know the key yet (a wrong `TENSORCOST_GATEWAY_KEY`, most
likely). The check is never sent twice for one
call: a second check would hold the call's reservation twice.

An allow can be reused for the same callers, model, `max_tokens` and prompt
size for as long as TensorCost's `cache_ttl_seconds` says. TensorCost sends 0
for any allow that placed a hold, and the guardrail never reuses an allow
that reserved anything, so in practice only calls no hard cap covers are
reused. Refusals, reroutes and degraded answers are never reused.

The prompt's token count runs off the event loop, and the check goes without
it if counting takes longer than 0.25 s.

## Budgets and large `max_tokens`

An allowed call holds its worst-case cost against the hard caps it falls
under until its spend lands, or for five minutes. When the check knows both
the prompt's size and `max_tokens`, that worst case is exact, and a call
whose worst case doesn't fit in what's left is refused even if it would have
used far less. Clients that send a large default `max_tokens` on every call
(64k, say) while the prompt is counted are judged strictly on that worst
case, so near a cap they are refused earlier than their real spend would
need. Send a `max_tokens` close to what the call needs, or set
`count_input_tokens: false`, which makes the hold an estimate that refuses
only once a cap is already full.

## Holds after a failed call

When a call fails in a way that proves the provider can't have billed it (a
401, 403, 404 or 429 from the provider, or LiteLLM's own budget refusal),
the guardrail cancels the call's hold at once
(`POST /v1/gateway/admit/{decision_id}/cancel`). After any other failure (a
timeout, a 5xx, a 400, a stream that broke off, a call LiteLLM's router
retried or fell back) the provider may have billed, so the hold stays until
the call's spend row releases it or it expires. A call that got through on a
reused allow has no hold of its own and is never cancelled.

The call is found by the correlation id the guardrail minted, and only for
the request it admitted. LiteLLM's own call id is never used: a client can
set it with the `x-litellm-call-id` header.

LiteLLM's router retries and fallbacks run inside one checked call. Each
attempt is logged as its own row and its spend counted.

## Azure deployments named your own way

An Azure OpenAI deployment carries the name you gave it (`prod-mini-deploy`),
not the model behind it, and no price list knows that name. Set `base_model`
on the deployment, which LiteLLM's own Azure docs ask for so that LiteLLM can
price the call too:

```yaml
model_list:
  - model_name: chat-mini
    litellm_params:
      model: azure/prod-mini-deploy
      api_base: https://acme.openai.azure.com
      api_key: os.environ/AZURE_API_KEY
    model_info:
      base_model: azure/gpt-4o-mini
```

LiteLLM also reads `base_model` under `litellm_params`, and that one wins when
both are set; the guardrail and the logger read it the same way. With it set,
the call goes to admit as `gpt-4o-mini` (priced, budgeted, governed and
routed as that model), its spend row's model is `azure/gpt-4o-mini`, and the
deployment's own name goes beside it as `deployment` in both, so it still
shows on the row. That is the name as Azure shows it, `prod-mini-deploy`,
without LiteLLM's `azure/` prefix; the `model_name` alias is the row's
`model_group`. A model approved in TensorCost under the deployment's name
before you set `base_model` stays approved. A budget scoped to the deployment's
name doesn't follow: scope it to the model instead. Without `base_model`
nothing changes, and the call is checked and recorded under the deployment's
name.

## Spend rows

The logger sends LiteLLM's spend-log row shape: `request_id`, the provider's
response `id`, model, tokens, cache read and write tokens, LiteLLM's cost,
start and end time, team, user, key alias, end user, tags, and for a failed
call the error's class and code, and the end customer the request named under
`tensorcost_customer`. It never sends the prompt, the response, the request's
parameters, the rest of the caller's own metadata or an error's message (only
whether it says a budget was exceeded, or that LiteLLM had no model by the
name asked for).

The model is the deployment LiteLLM's router picked (`azure/gpt-5.4` for a
`model_name: azure-gpt-5.4` entry), or the deployment's `base_model` when its
config names one (below), and the name the caller asked for goes beside it as
`model_group`. TensorCost reads the provider from the
deployment's prefix and api base, so a `model_list` alias such as `prod-chat`
or `cheap` is never a model of record. A call LiteLLM couldn't resolve (its
400 "Invalid model name passed in") reached no provider: TensorCost reads its
row as LiteLLM's own refusal, not as spend, and releases the call's hold.

`request_id`, which TensorCost dedupes on, is a UUID the logger mints for
each logged event. It isn't LiteLLM's call id, which a client can set; one
call id on every request would otherwise have all but the first row deduped
away. Each router attempt is its own row.

The guardrail's correlation id goes on one row per call: the success, or,
when no success comes, one of its failure rows. A failed attempt's row waits
up to two minutes for the call to finish. That way TensorCost books its
estimate for an attempt whose cost it can't tell at most once per call, and
never on top of the attempt that succeeded. A correlation id the guardrail
didn't mint in this process is never sent.

Rows go in batches of at most 500, every 5 seconds or sooner when a batch
fills. Pushes run on the logger's own task, so LiteLLM's calls never wait
for TensorCost.

When TensorCost can't take a push (a 5xx, a 408 or 429, a timeout, a
refused or dropped connection) nothing is dropped. The rows stay queued and
the logger tries again after about a second, doubling the wait each time up
to a minute, with jitter, for as long as the outage lasts; a 429's
`Retry-After` is honoured. Every row keeps its `request_id` across tries,
so a push that landed before its answer was lost is deduped and counts
once. The log says when pushes start failing, about once a minute while
they keep failing, and when they get through again.

A refusal that waiting can't fix (400, 401, 403, 413, 422 and any other 4xx)
drops that batch and logs one error with the status and TensorCost's
message: usually a wrong `TENSORCOST_GATEWAY_KEY`.

The queue holds at most 20,000 rows, the batch in flight included: about
twenty minutes of a busy proxy at a thousand calls a minute. If an outage
outlasts that, the oldest rows go first and each thousandth drop is logged
as an error with the running count. Those calls are missing from spend,
budgets and the FOCUS export. There is no disk spool; the queue lives in
the proxy's process.

When the proxy exits, an exit hook sends what is queued once, within five
seconds, and logs how many rows it couldn't send. A proxy that is killed
outright loses them.

## Team budgets in LiteLLM

`tensorcost_litellm.budget_sync` sets a LiteLLM team's `max_budget` from what
is left on a TensorCost budget, so LiteLLM refuses on its own if TensorCost
can't be reached. TensorCost can do this for you every 15 minutes when you
give the connection your LiteLLM admin URL and key; the module is the same
push for anyone who would rather run it themselves:

    LITELLM_URL=https://litellm.example.com LITELLM_ADMIN_KEY=sk-... \
      python -m tensorcost_litellm.budget_sync --team-id search --room-usd 412.50

It reads the team with `GET /team/info`, sets `max_budget` to the team's
LiteLLM spend plus the room with `POST /team/update`, and reports `pushed`
only when LiteLLM's answer, or a second read of the team, carries the new
value back. If this LiteLLM names the fields differently it changes nothing
and reports `unsupported`.

The push never loosens the team's own limit: it sets the lower of the two.
The team's own limit is its `max_budget` before the first push. The CLI saves
it to a state file (`--state-file`, default `tensorcost-budget-sync.json`)
before it changes anything, since an update whose answer is lost may still
have landed. If the limit can't be saved, nothing is sent. A saved limit is
never replaced. Later pushes cap at it, and `--restore` puts it back and
forgets it once the TensorCost budget no longer covers the team:

    python -m tensorcost_litellm.budget_sync --team-id search --restore

Keep the state file; without it a restore doesn't know what to put back.
`--original-max-budget` gives the team's own limit for a team the file
doesn't have yet.

Use one or the other for a team, never both. TensorCost's own push and this
module each remember the team's limit from before their first push. If this
module pushed first, TensorCost takes its value as the team's own limit, and
a later revoke in TensorCost puts that figure back instead of yours. To move
from this module to TensorCost's push, run `--restore` first.

## Tests

    pip install -e ".[test]"
    pytest

The tests run against a stub TensorCost server. With LiteLLM installed they
use its real `CustomGuardrail` and `CustomLogger`; without it, stand-ins with
the same names (in `tests/conftest.py`).

## Licence

Apache-2.0.
