spendgate
Status: pre-1.0. The configuration format, error codes and ledger schema may change between minor versions. v0.1 targets local deployments: one machine, Docker Compose, SQLite or Postgres.
spendgate is a small, auditable guard for LLM spending: an OpenAI- and Anthropic-compatible proxy plus a Python SDK and a no-server library mode. Before a call is forwarded it holds the worst-case cost in every budget the call belongs to, refuses anything it cannot price, and settles the real cost exactly once afterwards.
What it guarantees
How money is protected links each guarantee to the code and the tests that prove it.
- Fail closed. An unknown or unpriced model, a missing exchange rate or output ceiling, an unreachable ledger, a kill switch or a bad key refuses the call before the provider is contacted. Price data cannot switch this off.
- Hard holds. The worst case is held before sending, in one transaction across the whole scope
chain, so
spent + heldnever exceeds the cap plus recorded overruns. A call is free only if it never reached the provider or the provider rejected it; anything else after sending is charged the full hold unless usage was seen. - Overruns are recorded, never clamped. Usage above the hold is booked as reported. A breach of
the cap blocks the budget until an operator acknowledges it (the default,
strict_overrun). - Session limits. A session id gets a budget, a step limit, a time limit and an idle expiry,
so a runaway loop ends with
session_limit_reached. - Throttling costs nothing. A provider 429 cools that deployment down and the call goes to the next deployment of the same model inside the same reservation, at no charge.
- A perimeter you can test. In the reference Docker Compose setup only the proxy holds provider keys and has internet access; apps on the internal network cannot reach a provider at all.
Library mode is cooperative: code that holds the provider key can skip it. Use the proxy with the perimeter when something must be enforced. See Running locally.
Quickstart: proxy mode
Install with the SDK you use (openai, anthropic), then write a config. Providers, prices and
budgets are data; this one routes one model and gives it a 5 USD lifetime cap.
pip install "spendgate[openai]"
server: {listen: "127.0.0.1:8080", key_pepper: "env:SPENDGATE_KEY_PEPPER"}
ledger: {currency: USD, backend: {kind: sqlite, path: ./spendgate.db}}
providers:
openai: {base_url: "https://api.openai.com/v1", credential: "env:OPENAI_API_KEY"}
models:
gpt-6-luna: {catalog_id: openai/gpt-6-luna, deployments: [{provider: openai}]}
bootstrap:
scopes: ["org:demo"]
budgets: [{id: demo, scope: "org:demo", period: none, cap: "5 USD"}]
keys: [{name: quickstart, scope: "org:demo"}]
Check it, create the virtual key, then start the proxy. The provider key stays here, in the
proxy's environment. migrate --bootstrap prints each key once; serve never prints a secret, so
none ends up in a container or service log.
export OPENAI_API_KEY=sk-... # your real provider key
export SPENDGATE_KEY_PEPPER=$(openssl rand -hex 32) # keep it: it protects stored keys
spendgate check-config --config spendgate.yaml
spendgate migrate --config spendgate.yaml --bootstrap
# bootstrap: created api key quickstart (org:demo): sg-... <- printed once; copy it
spendgate serve --config spendgate.yaml
In another terminal, call it with the virtual key (sg-...), never the provider key. The helper
sets the base URL and attribution headers; any unmodified OpenAI or Anthropic SDK also works.
import spendgate.client as sg
client = sg.openai("http://127.0.0.1:8080/v1", "sg-...", user="u-42", tags=["quickstart"])
with sg.raise_for_spendgate(): # BudgetExhausted, RateLimited, ModelNotPriced, ...
reply = client.chat.completions.create(
model="gpt-6-luna", max_tokens=50, messages=[{"role": "user", "content": "hi"}]
)
print(reply.choices[0].message.content)
See who spent what:
spendgate usage --local --config spendgate.yaml --by model
Next: running locally with the perimeter, operations (keys, kill switch, backups) and the configuration reference.
Quickstart: library mode
No server and no key store: an httpx transport runs the same pipeline in your process against a
local SQLite ledger.
import spendgate.local as sg
client = sg.guard(cap="5 USD").openai() # needs OPENAI_API_KEY; ledger in your user data dir
client.chat.completions.create(model="gpt-6-luna", messages=[{"role": "user", "content": "hi"}])
The cap lasts for the life of the ledger file and is shared by every script that opens it. Details and limits.
Feature matrix
Every row goes through the same hold, refusal and settlement rules. "stream" means the endpoint streams and the stream is settled from its usage.
| Provider | Chat completions | Responses | Messages | Embeddings | Transcription | Speech |
|---|---|---|---|---|---|---|
| OpenAI | yes, stream | yes, stream | yes | yes | yes, chunked | |
| Azure OpenAI | yes, stream | yes, stream | yes | yes | ||
| Anthropic | yes, stream | |||||
| AWS Bedrock | yes, stream (translated) | |||||
| Google (Gemini API, Vertex AI) | yes, stream (translated) | |||||
| Azure Speech | yes | |||||
| ElevenLabs | yes, chunked |
Endpoints are the OpenAI paths (/v1/chat/completions, /v1/responses, /v1/embeddings,
/v1/audio/transcriptions, /v1/audio/speech) and Anthropic's /v1/messages. Fields outside the
allowlist are refused rather than forwarded; see the wire protocol. Realtime
and hosted tools are not supported. Backends: SQLite (one host) and Postgres (shared).
Documentation
- How money is protected: each guarantee with its code and test.
- Documentation index, configuration reference, operations.
- How to add a model or a provider.
- spendgate and LiteLLM: when to use which.
- Roadmap: what may come next.
- Threat model, contributing, security policy.
Development
Requires uv and Python 3.13 or newer.
uv sync --all-extras
make check # every quality gate
Licence
Metadata
Release files for spendgate 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| spendgate-0.1.0.tar.gz | 231.8 kB | With provenance | Details for spendgate-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| spendgate-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for spendgate-0.1.0-py3-none-any.whl |
Total release size: 532.8 kB
Release files / spendgate-0.1.0.tar.gz
| Download URL | spendgate-0.1.0.tar.gz |
|---|---|
| Size | 231.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
48714140712d8dcb737fa607f92fa3954fe6149d8d223dc7c190b1df876cdc21
|
|
BLAKE2b-256 checksum How to use checksums |
418f63a0348723968554ab3649d6727d4919fbbfb775d0c2dc987bb412c4bb74
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / spendgate-0.1.0-py3-none-any.whl
| Download URL | spendgate-0.1.0-py3-none-any.whl |
|---|---|
| Size | 301.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
365df9262b1303a9bd1c118b8305c7e65b8ace9f945f11c2b2fc6dcb84556697
|
|
BLAKE2b-256 checksum How to use checksums |
bb9d6fd5cca2680015873f908d8815bf48e56bbed2d607d409c81d158aa5d67c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logSecurity for spendgate 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for spendgate-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.