Sandhi — the metering layer for AI agents (Python binding of the Rust core). The bare name `sandhi` on PyPI is an unrelated Sanskrit-linguistics library.
Project description
sandhi-gateway
Python binding for Sandhi — the metering layer for AI agents. The Rust core, in-process via PyO3: virtual keys, budgets, and neutral usage-event metering with zero network hop. Keep making your own provider calls; hand the response to Sandhi to meter it.
pip install sandhi-gateway # import as: import sandhi_gateway
The bare name
sandhion PyPI is an unrelated Sanskrit-linguistics library; this binding is published assandhi-gateway. The crate and GitHub repo aresandhi.
Usage
import json
import sandhi_gateway as sg
gw = sg.Gateway(sink_path="usage.jsonl") # events append as JSONL (+ in-memory)
gw.add_virtual_key("vk_alice", subject="alice", group="platform", upstream="anthropic")
gw.set_budget("group:platform", 1_000_000)
# ... you make your own provider call and get the raw response JSON ...
event = gw.meter(
"vk_alice", "anthropic", "claude-x", response_json,
session_id="conv_7",
)
# event["tokens_in"], event["cache_read_tokens"], event["subject_id"], ...
print(gw.spent("group:platform")) # budget recorded
print(gw.check_budget("group:platform", 5000)) # True/False
# Just parse usage (same Rust parsers as the proxy), no attribution:
sg.parse_usage("openai", response_json) # {tokens_in, tokens_out, cache_*}
Custom / unknown providers (host escape hatch)
# (a) register a host parser callback for a provider Sandhi doesn't know:
gw.register_parser("myprovider", lambda body: {"tokens_in": 30, "tokens_out": 12,
"cache_creation_tokens": 0, "cache_read_tokens": 0})
gw.meter("vk_alice", "myprovider", "model", response_json) # uses your callback
# (b) or skip parsing and pass counts directly:
gw.meter_tokens("vk_alice", "myprovider", "model", tokens_in=30, tokens_out=12)
meter() parses the usage at the source (the same cache-split logic as the reverse
proxy), attributes it to the virtual key's subject/group, records the budget, emits the
neutral usage event (matching usage-event.v1.schema.json),
and returns it for local display. Unknown key → KeyError; bad JSON → ValueError.
Apache-2.0. See the main README and ADR-0001.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sandhi_gateway-0.1.0-cp311-abi3-win_amd64.whl.
File metadata
- Download URL: sandhi_gateway-0.1.0-cp311-abi3-win_amd64.whl
- Upload date:
- Size: 194.8 kB
- Tags: CPython 3.11+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e684459f2e8699030f1da9a22891bb0c842ad238dacf0c7919a2003ccb5c5b8f
|
|
| MD5 |
b5708a1a5ef614382cc83317e7c22df5
|
|
| BLAKE2b-256 |
e3b9399abdcabdb397f953b0b44ac5482ce3579870dae09d340ca18028d518a5
|
Provenance
The following attestation bundles were made for sandhi_gateway-0.1.0-cp311-abi3-win_amd64.whl:
Publisher:
release.yml on anvai-labs/sandhi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sandhi_gateway-0.1.0-cp311-abi3-win_amd64.whl -
Subject digest:
e684459f2e8699030f1da9a22891bb0c842ad238dacf0c7919a2003ccb5c5b8f - Sigstore transparency entry: 2201857726
- Sigstore integration time:
-
Permalink:
anvai-labs/sandhi@c856ee080db60277ebd6176f465883c9b070af43 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/anvai-labs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c856ee080db60277ebd6176f465883c9b070af43 -
Trigger Event:
push
-
Statement type:
File details
Details for the file sandhi_gateway-0.1.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: sandhi_gateway-0.1.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 337.9 kB
- Tags: CPython 3.11+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87ce5ea17df961513092ba2215e35e3eda4c1886160bc6c5fd6e53f7d24708b9
|
|
| MD5 |
cfee40882120cb7c6b5914f6e3d78d90
|
|
| BLAKE2b-256 |
60f32528c15b1a110e4360434394a760bebec1ad1ceb5e8ab4c53b8540c718b6
|
Provenance
The following attestation bundles were made for sandhi_gateway-0.1.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
release.yml on anvai-labs/sandhi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sandhi_gateway-0.1.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
87ce5ea17df961513092ba2215e35e3eda4c1886160bc6c5fd6e53f7d24708b9 - Sigstore transparency entry: 2201858020
- Sigstore integration time:
-
Permalink:
anvai-labs/sandhi@c856ee080db60277ebd6176f465883c9b070af43 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/anvai-labs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c856ee080db60277ebd6176f465883c9b070af43 -
Trigger Event:
push
-
Statement type:
File details
Details for the file sandhi_gateway-0.1.0-cp311-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: sandhi_gateway-0.1.0-cp311-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 291.2 kB
- Tags: CPython 3.11+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e8569f3fa67b5aade2e388460e73ce08135557aaaa894138d7da6fef4e5be9a
|
|
| MD5 |
cb102be6d2c611ef46d75ac8560dd6e7
|
|
| BLAKE2b-256 |
778635826b9c7c55d346be516ed4931bc71910b94a114b5af1a1c801fcf60000
|
Provenance
The following attestation bundles were made for sandhi_gateway-0.1.0-cp311-abi3-macosx_11_0_arm64.whl:
Publisher:
release.yml on anvai-labs/sandhi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sandhi_gateway-0.1.0-cp311-abi3-macosx_11_0_arm64.whl -
Subject digest:
9e8569f3fa67b5aade2e388460e73ce08135557aaaa894138d7da6fef4e5be9a - Sigstore transparency entry: 2201858509
- Sigstore integration time:
-
Permalink:
anvai-labs/sandhi@c856ee080db60277ebd6176f465883c9b070af43 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/anvai-labs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c856ee080db60277ebd6176f465883c9b070af43 -
Trigger Event:
push
-
Statement type: