spendctl-sdk
Zero-dependency Python client for the spendctl sidecar. Python 3.10+, typed, optional integrations for httpx and requests.
spendctl is the spend-policy control plane for AI agents: budgets, approvals, receipts and chain tracing across every wallet and every HTTP 402 protocol (x402, MPP), enforced in a sidecar you run, never by holding your keys or funds. This package is the thin client; the sidecar ships separately (npx spendctl init && npx spendctl start, docs at https://spendctl.dev/docs/).
pip install spendctl-sdk # standard library only
pip install "spendctl-sdk[httpx]" # + httpx transport
pip install "spendctl-sdk[requests]"
The import name is spendctl_sdk (the PyPI name spendctl belongs to an unrelated project).
For agents
Every absolute http(s) request goes to the sidecar with the real destination in a header. If the seller answers 402, the sidecar applies your policy, asks a human when needed, pays, and returns the paid response, or a 402 that says why it refused.
httpx (what the OpenAI SDK, LangChain and most agent frameworks use):
from spendctl_sdk import httpx_client, read_refusal
with httpx_client(agent="researcher") as http: # reads SPENDCTL_URL / SPENDCTL_AGENT by default
res = http.get("https://api.example.com/paid")
if res.status_code == 200:
print(res.headers["x-spendctl-amount"]) # "$0.01"
elif refusal := read_refusal(res):
if refusal.needs_approval:
# A human was asked. AgentClient().approval(refusal.approval, wait=25) returns once
# they decide; when approved, retry with headers={"x-spendctl-approval": refusal.approval}.
...
async_httpx_client(...) is the httpx.AsyncClient version. To keep your own client configuration, mount the transport instead: httpx.Client(transport=SidecarTransport(agent="researcher")) / AsyncSidecarTransport.
requests:
from spendctl_sdk import requests_session, read_refusal
http = requests_session(agent="researcher")
res = http.post("https://api.example.com/paid", json={"q": "tokyo"})
refusal = read_refusal(res) # None when paid; otherwise why spendctl refused or is waiting for a human
Or mount SidecarAdapter(agent=...) on an existing requests.Session for http:// and https://.
Standard library only:
from spendctl_sdk import pay
res = pay("https://api.example.com/paid", agent="researcher")
res.status, res.amount, res.decision, res.json(), res.refusal
pay() follows no redirects and raises SpendctlError only when the sidecar cannot be reached; a refusal is a normal response with res.refusal set.
Options, each falling back to an environment variable:
| Option | Env var | Default |
|---|---|---|
url |
SPENDCTL_URL |
http://127.0.0.1:8402 |
agent |
SPENDCTL_AGENT |
— |
token |
SPENDCTL_AGENT_TOKEN |
— (only when the sidecar runs with agentAuth: token) |
Requests that already point at the sidecar, or that are not absolute http(s) URLs, go through untouched. The agent side never holds a wallet key or the admin token. sidecar_headers(target, ...) returns the three headers for any other HTTP library.
read_refusal(response) accepts httpx.Response, requests.Response and the SidecarResponse from pay(); it returns a Refusal (error, message, attempt, decision, amount, approval, approval_expires_at, hint, reasons, idempotency_key, request_id, would_be; properties denied, needs_approval, dry_run) or None when the response was paid or did not come from spendctl.
For agents: AgentClient
The agent's own budget, receipts and approvals. It can wait for a human's decision and withdraw an approval it no longer needs, never approve or deny.
from spendctl_sdk import AgentClient
me = AgentClient(agent="researcher")
me.budget() # {"windows": [{"window": "daily", "limit": ..., "spent": ..., "remaining": ...}], "endpoints": [...], ...}
me.receipts(20)
me.approval("apr_…") # {"id", "status", "expiresAt", "attempt", "decidedAt", "decidedBy", "note"}
me.approval("apr_…", wait=25) # long poll: returns as soon as a human decides
me.withdraw("apr_…", "task cancelled") # the task no longer needs it, so a late approve cannot pay
me.pay("https://api.example.com/paid") # pay() with this client's url, agent and token
status is pending, approved, denied, expired or withdrawn. Waiting never pays: once the approval is approved, repeat the request with approval= (or the x-spendctl-approval header). The sidecar caps wait at 25 s, and the call's HTTP timeout is raised to wait plus 5 s. withdraw() raises SpendctlError with status 409 when a human already decided or the approval expired; .body["approval"] holds the outcome. wait, endpoints and the extra approval fields need sidecar 0.6, withdraw() and decidedBy 0.7; an older sidecar answers at once and leaves them out.
For humans and tooling: AdminClient
Needs the admin token (.spendctl/admin.token). Approvals, ledger, export, trace.
import os
from spendctl_sdk import AdminClient
admin = AdminClient(os.environ["SPENDCTL_ADMIN_TOKEN"])
for approval in admin.pending():
admin.approve(approval["id"], "looks right")
csv = admin.export("csv", since="2026-01-01")
admin.trace("att_…")["text"] # agent-to-agent chain as text
Errors are raised as SpendctlError with the sidecar's message (.status, .body); an unreachable sidecar says so and reminds you to run spendctl start.
Wire protocol
The client is small because the protocol is small; it is documented in the repository README so that anyone can write one in another language.
Licence
Apache-2.0. Source and issues: https://github.com/miraistudio/spendctl-sdk. "spendctl" is a trademark of Mirai Studio, Inc.; the licence does not cover the name.
Metadata
Release files for spendctl-sdk 0.2.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 |
|---|---|---|---|---|
| spendctl_sdk-0.2.0.tar.gz | 21.3 kB | With provenance | Details for spendctl_sdk-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| spendctl_sdk-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for spendctl_sdk-0.2.0-py3-none-any.whl |
Total release size: 39.2 kB
Release files / spendctl_sdk-0.2.0.tar.gz
| Download URL | spendctl_sdk-0.2.0.tar.gz |
|---|---|
| Size | 21.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dde400b6ff2bd8d73a10b1e68181aeb20431fab05c065d989dbe5c8c834a816f
|
|
BLAKE2b-256 checksum How to use checksums |
9d35953e0c5d259f93f476201ad8acfea5f6129da599107f9a4e4ddcbde0567b
|
| 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 4, 2026.
Transparency logRelease files / spendctl_sdk-0.2.0-py3-none-any.whl
| Download URL | spendctl_sdk-0.2.0-py3-none-any.whl |
|---|---|
| Size | 18.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e4cdd075f8d4db9fcd904dccc20a356420eccea3b1cc343b5e5e0534925d8d03
|
|
BLAKE2b-256 checksum How to use checksums |
21fd474191ee9bbfc68a8c7539d6cca88694cf3c7eb5c6484907e1b8e51d8275
|
| 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 4, 2026.
Transparency logSecurity for spendctl-sdk 0.2.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 spendctl_sdk-0.2.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 4, 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.