tensorcost-litellm 0.1.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tensorcost_litellm-0.1.5/.gitignore +10 -0
- tensorcost_litellm-0.1.5/PKG-INFO +391 -0
- tensorcost_litellm-0.1.5/README.md +370 -0
- tensorcost_litellm-0.1.5/ci/e2e.py +120 -0
- tensorcost_litellm-0.1.5/ci/model_list.yaml +25 -0
- tensorcost_litellm-0.1.5/ci/stub_tensorcost.py +100 -0
- tensorcost_litellm-0.1.5/ci/tensorcost.yaml +12 -0
- tensorcost_litellm-0.1.5/pyproject.toml +36 -0
- tensorcost_litellm-0.1.5/tensorcost_litellm/__init__.py +47 -0
- tensorcost_litellm-0.1.5/tensorcost_litellm/_shared.py +306 -0
- tensorcost_litellm-0.1.5/tensorcost_litellm/budget_sync.py +315 -0
- tensorcost_litellm-0.1.5/tensorcost_litellm/guardrail.py +641 -0
- tensorcost_litellm-0.1.5/tensorcost_litellm/logger.py +654 -0
- tensorcost_litellm-0.1.5/tests/conftest.py +157 -0
- tensorcost_litellm-0.1.5/tests/test_budget_sync.py +200 -0
- tensorcost_litellm-0.1.5/tests/test_conformance.py +195 -0
- tensorcost_litellm-0.1.5/tests/test_guardrail.py +833 -0
- tensorcost_litellm-0.1.5/tests/test_logger.py +724 -0
- tensorcost_litellm-0.1.5/tests/test_package_imports.py +60 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: tensorcost-litellm
|
|
3
|
+
Version: 0.1.5
|
|
4
|
+
Summary: A LiteLLM proxy guardrail that checks TensorCost budgets before each call, and a logger that reports spend back.
|
|
5
|
+
Project-URL: Homepage, https://github.com/vaadhlabs/tensorcost-integrations/tree/main/litellm
|
|
6
|
+
Project-URL: Repository, https://github.com/vaadhlabs/tensorcost-integrations
|
|
7
|
+
Project-URL: Issues, https://github.com/vaadhlabs/tensorcost-integrations/issues
|
|
8
|
+
Author: Vaadh Labs
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
Keywords: budget,cost,guardrail,litellm,llm
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Requires-Dist: httpx>=0.27
|
|
15
|
+
Provides-Extra: proxy
|
|
16
|
+
Requires-Dist: litellm[proxy]>=1.60; extra == 'proxy'
|
|
17
|
+
Provides-Extra: test
|
|
18
|
+
Requires-Dist: fastapi>=0.110; extra == 'test'
|
|
19
|
+
Requires-Dist: pytest>=8; extra == 'test'
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# tensorcost-litellm
|
|
23
|
+
|
|
24
|
+
TensorCost for the [LiteLLM](https://github.com/BerriAI/litellm) proxy. Two
|
|
25
|
+
pieces, both loaded by the proxy from its config:
|
|
26
|
+
|
|
27
|
+
- `TensorCostAdmit`, a pre-call guardrail. Before each call it asks
|
|
28
|
+
TensorCost (`POST /v1/gateway/admit`) whether the call fits the budgets
|
|
29
|
+
that cover its team, user and key, and does what the answer says: lets it
|
|
30
|
+
through, refuses it with a 429 that names the budget and who can raise it,
|
|
31
|
+
or switches it to the model a routing policy picked.
|
|
32
|
+
- `spend_logger`, a callback. After each call it sends the call's spend row
|
|
33
|
+
to TensorCost (`POST /v1/gateway/logs?format=litellm`), so the budget
|
|
34
|
+
counts what the call cost.
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
Install it into the same Python environment as the proxy:
|
|
39
|
+
|
|
40
|
+
pip install "tensorcost-litellm @ git+https://github.com/vaadhlabs/tensorcost-integrations#subdirectory=litellm"
|
|
41
|
+
|
|
42
|
+
It isn't on PyPI yet. Once the first release is published,
|
|
43
|
+
`pip install tensorcost-litellm` will do the same. Pin a commit
|
|
44
|
+
(`...tensorcost-integrations@<sha>#subdirectory=litellm`) if you want the
|
|
45
|
+
install to repeat exactly.
|
|
46
|
+
|
|
47
|
+
## Configure
|
|
48
|
+
|
|
49
|
+
Create a LiteLLM gateway connection in TensorCost (Connect › Gateways). It
|
|
50
|
+
gives you a key once. Then add this to the proxy's `config.yaml`:
|
|
51
|
+
|
|
52
|
+
```yaml
|
|
53
|
+
guardrails:
|
|
54
|
+
- guardrail_name: tensorcost
|
|
55
|
+
litellm_params:
|
|
56
|
+
guardrail: tensorcost_litellm.TensorCostAdmit
|
|
57
|
+
mode: pre_call
|
|
58
|
+
default_on: true
|
|
59
|
+
api_base: os.environ/TENSORCOST_URL
|
|
60
|
+
api_key: os.environ/TENSORCOST_GATEWAY_KEY
|
|
61
|
+
fail_mode: open
|
|
62
|
+
|
|
63
|
+
litellm_settings:
|
|
64
|
+
callbacks: ["tensorcost_litellm.spend_logger"]
|
|
65
|
+
|
|
66
|
+
general_settings:
|
|
67
|
+
master_key: os.environ/LITELLM_MASTER_KEY
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
and set the environment:
|
|
71
|
+
|
|
72
|
+
TENSORCOST_URL=https://<your TensorCost API host>/api/inference-proxy
|
|
73
|
+
TENSORCOST_GATEWAY_KEY=tcgw_...
|
|
74
|
+
LITELLM_MASTER_KEY=<a long random secret>
|
|
75
|
+
|
|
76
|
+
`LITELLM_MASTER_KEY` is LiteLLM's own admin key, not ours. We've seen
|
|
77
|
+
LiteLLM 1.110 refuse to start without one (`UnsafeMasterKeyError`), so a
|
|
78
|
+
config with only the TensorCost settings can fail at startup. If your proxy
|
|
79
|
+
already has a master key, keep it.
|
|
80
|
+
|
|
81
|
+
`default_on: true` runs the check on every call; without it LiteLLM runs a
|
|
82
|
+
guardrail only for requests that name it. `callbacks` takes the module-level
|
|
83
|
+
instance `spend_logger`, not the class: LiteLLM's callback setting names an
|
|
84
|
+
instance.
|
|
85
|
+
|
|
86
|
+
Guardrail settings, all optional:
|
|
87
|
+
|
|
88
|
+
| Setting | Default | What it does |
|
|
89
|
+
| --- | --- | --- |
|
|
90
|
+
| `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. |
|
|
91
|
+
| `timeout_seconds` | `1.0` | How long the check may take. |
|
|
92
|
+
| `instance` | none | The connection's instance, when you gave it one in TensorCost. |
|
|
93
|
+
| `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. |
|
|
94
|
+
|
|
95
|
+
## What the check sends
|
|
96
|
+
|
|
97
|
+
The call's callers (the key's team id, user id and key alias, and any
|
|
98
|
+
request tags), the model and its provider as the proxy's `model_list` names
|
|
99
|
+
them, the application (below), `max_tokens` (or `max_completion_tokens`),
|
|
100
|
+
the prompt's token count, whether it streams, and the models in
|
|
101
|
+
`model_list` a reroute may pick. It never sends the prompt.
|
|
102
|
+
|
|
103
|
+
Those go as `accepts_models`, one entry per `model_name` with the
|
|
104
|
+
deployment it routes to, then the bare names:
|
|
105
|
+
|
|
106
|
+
```json
|
|
107
|
+
"accepts_models": [
|
|
108
|
+
{"name": "azure-gpt-5.4-mini", "model": "gpt-5.4-mini", "provider": "azure"},
|
|
109
|
+
{"name": "claude-haiku", "model": "us.anthropic.claude-haiku-4-5-20251001-v1:0", "provider": "bedrock"},
|
|
110
|
+
"azure-gpt-5.4-mini",
|
|
111
|
+
"claude-haiku"
|
|
112
|
+
]
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
An entry for a deployment with a `base_model` (below) carries the base model
|
|
116
|
+
as `model` and the deployment's own name as `deployment`. TensorCost matches a
|
|
117
|
+
routing policy's target model against each entry's `model` and `provider`, and answers a reroute with the entry's `name`, so a
|
|
118
|
+
`model_name` can be any alias you like. Up to 0.1.3 the guardrail sent only
|
|
119
|
+
the names, and a reroute fired only when a `model_name` was spelled like the
|
|
120
|
+
target model. The bare names are still sent for a TensorCost that reads only
|
|
121
|
+
strings; it drops the objects (the answer's `detail` names them as ignored)
|
|
122
|
+
and matches the names as before. A wildcard route (`bedrock/*`) goes only as
|
|
123
|
+
a bare name.
|
|
124
|
+
|
|
125
|
+
It also mints a correlation id for the call, puts it in the request's
|
|
126
|
+
metadata under `tensorcost_correlation_id` (over any value the caller sent),
|
|
127
|
+
and sends it with the check, so the call's spend row finds its decision.
|
|
128
|
+
|
|
129
|
+
### The end customer
|
|
130
|
+
|
|
131
|
+
To have a call judged against one of your customers' margin floors and fixed
|
|
132
|
+
limits, name the customer in the request's metadata under
|
|
133
|
+
`tensorcost_customer`, the LiteLLM side of the SDK's `x-tc-customer` header:
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
client.chat.completions.create(
|
|
137
|
+
model="claude-sonnet",
|
|
138
|
+
messages=[...],
|
|
139
|
+
extra_body={"metadata": {"tensorcost_customer": "acme"}},
|
|
140
|
+
)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
The check sends it as `customer`, and the logger puts it on the call's spend
|
|
144
|
+
row (`metadata.tensorcost_customer`), so the call counts toward that customer.
|
|
145
|
+
It is trimmed and at most 128 characters; a longer one, or one with control
|
|
146
|
+
characters, is left off. A cached allow is kept per customer, so one
|
|
147
|
+
customer's allow never covers another's call. A call with no customer is
|
|
148
|
+
judged by no customer's limit.
|
|
149
|
+
|
|
150
|
+
If your app also goes through the TensorCost SDK with LiteLLM as its base
|
|
151
|
+
URL, send the customer here too: TensorCost counts that call once, from
|
|
152
|
+
LiteLLM's row, and a LiteLLM row with no customer leaves the call off the
|
|
153
|
+
customer's figures.
|
|
154
|
+
|
|
155
|
+
### The application
|
|
156
|
+
|
|
157
|
+
A routing policy can be scoped to an application. The check sends the call's
|
|
158
|
+
application as `application`, the first of:
|
|
159
|
+
|
|
160
|
+
1. an `x-tc-application` header the caller sent (the header the SDK and
|
|
161
|
+
TensorCost's proxy use);
|
|
162
|
+
2. `tensorcost_application` in the request's metadata;
|
|
163
|
+
3. an `application=` or `app=` tag (`application:` and `app:` work too);
|
|
164
|
+
4. LiteLLM's end user (the request body's `user`);
|
|
165
|
+
5. the key's alias.
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
client.chat.completions.create(
|
|
169
|
+
model="claude-sonnet",
|
|
170
|
+
messages=[...],
|
|
171
|
+
extra_headers={"x-tc-application": "support-bot"},
|
|
172
|
+
)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
The logger works it out the same way from the same request and puts it on
|
|
176
|
+
the call's spend row (`metadata.tensorcost_application`), so the row's
|
|
177
|
+
application is the one the check was matched on. The last three steps are the
|
|
178
|
+
order TensorCost already read a LiteLLM row's application in. It is trimmed
|
|
179
|
+
and at most 256 characters; a longer one, or one with control characters, is
|
|
180
|
+
skipped for the next. A cached allow is kept per application.
|
|
181
|
+
|
|
182
|
+
## Decisions
|
|
183
|
+
|
|
184
|
+
- **allow**: the call goes on unchanged.
|
|
185
|
+
- **refuse**: the guardrail raises HTTP 429 with
|
|
186
|
+
`{"error": {"type": "budget_exceeded", "message": ...}}` and a
|
|
187
|
+
`Retry-After` header when TensorCost gives one. The message names the
|
|
188
|
+
budget and who can raise it. A refusal because TensorCost couldn't check
|
|
189
|
+
the call (`check_unavailable`, `invalid_request`) is a 503
|
|
190
|
+
`budget_check_unavailable` instead, so a client doesn't take an outage for
|
|
191
|
+
a spent budget. A refusal no wait can change (`model_governance_denied`,
|
|
192
|
+
`compliance_denied`, `run_stopped`) is a 403
|
|
193
|
+
`{"error": {"type": "permission_denied", "reason": ...}}`, so a client
|
|
194
|
+
doesn't back off and retry a call that can never pass.
|
|
195
|
+
- **reroute**: `model` is set to the `model_name` TensorCost names. The
|
|
196
|
+
decision covers that one attempt. A name that isn't in `model_list` (and
|
|
197
|
+
no wildcard route matches) would fail the call, so the call runs as asked
|
|
198
|
+
instead and the guardrail logs a warning.
|
|
199
|
+
|
|
200
|
+
Every answered decision is an HTTP 200. Anything else means TensorCost
|
|
201
|
+
failed, and the fail mode decides.
|
|
202
|
+
|
|
203
|
+
Each gateway connection has an admit rate, 60,000 checks a minute unless a
|
|
204
|
+
TensorCost admin sets another on Connect › Gateways. Past it TensorCost
|
|
205
|
+
still answers 200, with the fail mode's decision and `detail: "throttled"`:
|
|
206
|
+
under open the call goes through unchecked, under closed it gets the 503.
|
|
207
|
+
The guardrail logs that once a minute. A 429 from the check means
|
|
208
|
+
TensorCost didn't know the key yet (a wrong `TENSORCOST_GATEWAY_KEY`, most
|
|
209
|
+
likely). The check is never sent twice for one
|
|
210
|
+
call: a second check would hold the call's reservation twice.
|
|
211
|
+
|
|
212
|
+
An allow can be reused for the same callers, model, `max_tokens` and prompt
|
|
213
|
+
size for as long as TensorCost's `cache_ttl_seconds` says. TensorCost sends 0
|
|
214
|
+
for any allow that placed a hold, and the guardrail never reuses an allow
|
|
215
|
+
that reserved anything, so in practice only calls no hard cap covers are
|
|
216
|
+
reused. Refusals, reroutes and degraded answers are never reused.
|
|
217
|
+
|
|
218
|
+
The prompt's token count runs off the event loop, and the check goes without
|
|
219
|
+
it if counting takes longer than 0.25 s.
|
|
220
|
+
|
|
221
|
+
## Budgets and large `max_tokens`
|
|
222
|
+
|
|
223
|
+
An allowed call holds its worst-case cost against the hard caps it falls
|
|
224
|
+
under until its spend lands, or for five minutes. When the check knows both
|
|
225
|
+
the prompt's size and `max_tokens`, that worst case is exact, and a call
|
|
226
|
+
whose worst case doesn't fit in what's left is refused even if it would have
|
|
227
|
+
used far less. Clients that send a large default `max_tokens` on every call
|
|
228
|
+
(64k, say) while the prompt is counted are judged strictly on that worst
|
|
229
|
+
case, so near a cap they are refused earlier than their real spend would
|
|
230
|
+
need. Send a `max_tokens` close to what the call needs, or set
|
|
231
|
+
`count_input_tokens: false`, which makes the hold an estimate that refuses
|
|
232
|
+
only once a cap is already full.
|
|
233
|
+
|
|
234
|
+
## Holds after a failed call
|
|
235
|
+
|
|
236
|
+
When a call fails in a way that proves the provider can't have billed it (a
|
|
237
|
+
401, 403, 404 or 429 from the provider, or LiteLLM's own budget refusal),
|
|
238
|
+
the guardrail cancels the call's hold at once
|
|
239
|
+
(`POST /v1/gateway/admit/{decision_id}/cancel`). After any other failure (a
|
|
240
|
+
timeout, a 5xx, a 400, a stream that broke off, a call LiteLLM's router
|
|
241
|
+
retried or fell back) the provider may have billed, so the hold stays until
|
|
242
|
+
the call's spend row releases it or it expires. A call that got through on a
|
|
243
|
+
reused allow has no hold of its own and is never cancelled.
|
|
244
|
+
|
|
245
|
+
The call is found by the correlation id the guardrail minted, and only for
|
|
246
|
+
the request it admitted. LiteLLM's own call id is never used: a client can
|
|
247
|
+
set it with the `x-litellm-call-id` header.
|
|
248
|
+
|
|
249
|
+
LiteLLM's router retries and fallbacks run inside one checked call. Each
|
|
250
|
+
attempt is logged as its own row and its spend counted.
|
|
251
|
+
|
|
252
|
+
## Azure deployments named your own way
|
|
253
|
+
|
|
254
|
+
An Azure OpenAI deployment carries the name you gave it (`prod-mini-deploy`),
|
|
255
|
+
not the model behind it, and no price list knows that name. Set `base_model`
|
|
256
|
+
on the deployment, which LiteLLM's own Azure docs ask for so that LiteLLM can
|
|
257
|
+
price the call too:
|
|
258
|
+
|
|
259
|
+
```yaml
|
|
260
|
+
model_list:
|
|
261
|
+
- model_name: chat-mini
|
|
262
|
+
litellm_params:
|
|
263
|
+
model: azure/prod-mini-deploy
|
|
264
|
+
api_base: https://acme.openai.azure.com
|
|
265
|
+
api_key: os.environ/AZURE_API_KEY
|
|
266
|
+
model_info:
|
|
267
|
+
base_model: azure/gpt-4o-mini
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
LiteLLM also reads `base_model` under `litellm_params`, and that one wins when
|
|
271
|
+
both are set; the guardrail and the logger read it the same way. With it set,
|
|
272
|
+
the call goes to admit as `gpt-4o-mini` (priced, budgeted, governed and
|
|
273
|
+
routed as that model), its spend row's model is `azure/gpt-4o-mini`, and the
|
|
274
|
+
deployment's own name goes beside it as `deployment` in both, so it still
|
|
275
|
+
shows on the row. That is the name as Azure shows it, `prod-mini-deploy`,
|
|
276
|
+
without LiteLLM's `azure/` prefix; the `model_name` alias is the row's
|
|
277
|
+
`model_group`. A model approved in TensorCost under the deployment's name
|
|
278
|
+
before you set `base_model` stays approved. A budget scoped to the deployment's
|
|
279
|
+
name doesn't follow: scope it to the model instead. Without `base_model`
|
|
280
|
+
nothing changes, and the call is checked and recorded under the deployment's
|
|
281
|
+
name.
|
|
282
|
+
|
|
283
|
+
## Spend rows
|
|
284
|
+
|
|
285
|
+
The logger sends LiteLLM's spend-log row shape: `request_id`, the provider's
|
|
286
|
+
response `id`, model, tokens, cache read and write tokens, LiteLLM's cost,
|
|
287
|
+
start and end time, team, user, key alias, end user, tags, and for a failed
|
|
288
|
+
call the error's class and code, and the end customer the request named under
|
|
289
|
+
`tensorcost_customer`. It never sends the prompt, the response, the request's
|
|
290
|
+
parameters, the rest of the caller's own metadata or an error's message (only
|
|
291
|
+
whether it says a budget was exceeded, or that LiteLLM had no model by the
|
|
292
|
+
name asked for).
|
|
293
|
+
|
|
294
|
+
The model is the deployment LiteLLM's router picked (`azure/gpt-5.4` for a
|
|
295
|
+
`model_name: azure-gpt-5.4` entry), or the deployment's `base_model` when its
|
|
296
|
+
config names one (below), and the name the caller asked for goes beside it as
|
|
297
|
+
`model_group`. TensorCost reads the provider from the
|
|
298
|
+
deployment's prefix and api base, so a `model_list` alias such as `prod-chat`
|
|
299
|
+
or `cheap` is never a model of record. A call LiteLLM couldn't resolve (its
|
|
300
|
+
400 "Invalid model name passed in") reached no provider: TensorCost reads its
|
|
301
|
+
row as LiteLLM's own refusal, not as spend, and releases the call's hold.
|
|
302
|
+
|
|
303
|
+
`request_id`, which TensorCost dedupes on, is a UUID the logger mints for
|
|
304
|
+
each logged event. It isn't LiteLLM's call id, which a client can set; one
|
|
305
|
+
call id on every request would otherwise have all but the first row deduped
|
|
306
|
+
away. Each router attempt is its own row.
|
|
307
|
+
|
|
308
|
+
The guardrail's correlation id goes on one row per call: the success, or,
|
|
309
|
+
when no success comes, one of its failure rows. A failed attempt's row waits
|
|
310
|
+
up to two minutes for the call to finish. That way TensorCost books its
|
|
311
|
+
estimate for an attempt whose cost it can't tell at most once per call, and
|
|
312
|
+
never on top of the attempt that succeeded. A correlation id the guardrail
|
|
313
|
+
didn't mint in this process is never sent.
|
|
314
|
+
|
|
315
|
+
Rows go in batches of at most 500, every 5 seconds or sooner when a batch
|
|
316
|
+
fills. Pushes run on the logger's own task, so LiteLLM's calls never wait
|
|
317
|
+
for TensorCost.
|
|
318
|
+
|
|
319
|
+
When TensorCost can't take a push (a 5xx, a 408 or 429, a timeout, a
|
|
320
|
+
refused or dropped connection) nothing is dropped. The rows stay queued and
|
|
321
|
+
the logger tries again after about a second, doubling the wait each time up
|
|
322
|
+
to a minute, with jitter, for as long as the outage lasts; a 429's
|
|
323
|
+
`Retry-After` is honoured. Every row keeps its `request_id` across tries,
|
|
324
|
+
so a push that landed before its answer was lost is deduped and counts
|
|
325
|
+
once. The log says when pushes start failing, about once a minute while
|
|
326
|
+
they keep failing, and when they get through again.
|
|
327
|
+
|
|
328
|
+
A refusal that waiting can't fix (400, 401, 403, 413, 422 and any other 4xx)
|
|
329
|
+
drops that batch and logs one error with the status and TensorCost's
|
|
330
|
+
message: usually a wrong `TENSORCOST_GATEWAY_KEY`.
|
|
331
|
+
|
|
332
|
+
The queue holds at most 20,000 rows, the batch in flight included: about
|
|
333
|
+
twenty minutes of a busy proxy at a thousand calls a minute. If an outage
|
|
334
|
+
outlasts that, the oldest rows go first and each thousandth drop is logged
|
|
335
|
+
as an error with the running count. Those calls are missing from spend,
|
|
336
|
+
budgets and the FOCUS export. There is no disk spool; the queue lives in
|
|
337
|
+
the proxy's process.
|
|
338
|
+
|
|
339
|
+
When the proxy exits, an exit hook sends what is queued once, within five
|
|
340
|
+
seconds, and logs how many rows it couldn't send. A proxy that is killed
|
|
341
|
+
outright loses them.
|
|
342
|
+
|
|
343
|
+
## Team budgets in LiteLLM
|
|
344
|
+
|
|
345
|
+
`tensorcost_litellm.budget_sync` sets a LiteLLM team's `max_budget` from what
|
|
346
|
+
is left on a TensorCost budget, so LiteLLM refuses on its own if TensorCost
|
|
347
|
+
can't be reached. TensorCost can do this for you every 15 minutes when you
|
|
348
|
+
give the connection your LiteLLM admin URL and key; the module is the same
|
|
349
|
+
push for anyone who would rather run it themselves:
|
|
350
|
+
|
|
351
|
+
LITELLM_URL=https://litellm.example.com LITELLM_ADMIN_KEY=sk-... \
|
|
352
|
+
python -m tensorcost_litellm.budget_sync --team-id search --room-usd 412.50
|
|
353
|
+
|
|
354
|
+
It reads the team with `GET /team/info`, sets `max_budget` to the team's
|
|
355
|
+
LiteLLM spend plus the room with `POST /team/update`, and reports `pushed`
|
|
356
|
+
only when LiteLLM's answer, or a second read of the team, carries the new
|
|
357
|
+
value back. If this LiteLLM names the fields differently it changes nothing
|
|
358
|
+
and reports `unsupported`.
|
|
359
|
+
|
|
360
|
+
The push never loosens the team's own limit: it sets the lower of the two.
|
|
361
|
+
The team's own limit is its `max_budget` before the first push. The CLI saves
|
|
362
|
+
it to a state file (`--state-file`, default `tensorcost-budget-sync.json`)
|
|
363
|
+
before it changes anything, since an update whose answer is lost may still
|
|
364
|
+
have landed. If the limit can't be saved, nothing is sent. A saved limit is
|
|
365
|
+
never replaced. Later pushes cap at it, and `--restore` puts it back and
|
|
366
|
+
forgets it once the TensorCost budget no longer covers the team:
|
|
367
|
+
|
|
368
|
+
python -m tensorcost_litellm.budget_sync --team-id search --restore
|
|
369
|
+
|
|
370
|
+
Keep the state file; without it a restore doesn't know what to put back.
|
|
371
|
+
`--original-max-budget` gives the team's own limit for a team the file
|
|
372
|
+
doesn't have yet.
|
|
373
|
+
|
|
374
|
+
Use one or the other for a team, never both. TensorCost's own push and this
|
|
375
|
+
module each remember the team's limit from before their first push. If this
|
|
376
|
+
module pushed first, TensorCost takes its value as the team's own limit, and
|
|
377
|
+
a later revoke in TensorCost puts that figure back instead of yours. To move
|
|
378
|
+
from this module to TensorCost's push, run `--restore` first.
|
|
379
|
+
|
|
380
|
+
## Tests
|
|
381
|
+
|
|
382
|
+
pip install -e ".[test]"
|
|
383
|
+
pytest
|
|
384
|
+
|
|
385
|
+
The tests run against a stub TensorCost server. With LiteLLM installed they
|
|
386
|
+
use its real `CustomGuardrail` and `CustomLogger`; without it, stand-ins with
|
|
387
|
+
the same names (in `tests/conftest.py`).
|
|
388
|
+
|
|
389
|
+
## Licence
|
|
390
|
+
|
|
391
|
+
Apache-2.0.
|