Skip to main content

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 Sandhithe 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 sandhi on PyPI is an unrelated Sanskrit-linguistics library; this binding is published as sandhi-gateway. The crate and GitHub repo are sandhi.

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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

sandhi_gateway-0.1.1-cp311-abi3-win_amd64.whl (1.8 MB view details)

Uploaded CPython 3.11+Windows x86-64

sandhi_gateway-0.1.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ x86-64

sandhi_gateway-0.1.1-cp311-abi3-macosx_11_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

File details

Details for the file sandhi_gateway-0.1.1-cp311-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for sandhi_gateway-0.1.1-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 5ec6495896e07e0613cfc416b9f1b6db5c0c473fe3da4eb8a85931b23202c041
MD5 1337baa955e6cf5dbe12617c5352c670
BLAKE2b-256 40637b41e10de32d961161b17b37d3209490a308b7d4ac88ae7f9a52e4dda36b

See more details on using hashes here.

Provenance

The following attestation bundles were made for sandhi_gateway-0.1.1-cp311-abi3-win_amd64.whl:

Publisher: release.yml on anvai-labs/sandhi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sandhi_gateway-0.1.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sandhi_gateway-0.1.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e5d25fe24aab1dae9af40bf80d3c20b8e2b6c75dc263c090440355875f48a762
MD5 57167786f12bee136b529d988ea46238
BLAKE2b-256 ae9e700087c2ad0e638f24128577352e5a6f0779e996d9eb2a39af9448caf4af

See more details on using hashes here.

Provenance

The following attestation bundles were made for sandhi_gateway-0.1.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on anvai-labs/sandhi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sandhi_gateway-0.1.1-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sandhi_gateway-0.1.1-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ba51e097256f4fc9678998fe71ef94afa5397b9a4b5e6c780096dce16ccb66df
MD5 95353fe0fb51d49e739495f3701c2f17
BLAKE2b-256 9d7b9b37ae97035780d912f745b31c6dfecfb4c748387e37b319c7cdb3c78c50

See more details on using hashes here.

Provenance

The following attestation bundles were made for sandhi_gateway-0.1.1-cp311-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on anvai-labs/sandhi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page