Skip to main content

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. A seller's own error that follows a payment is never a refusal, whatever its body says: on such a response the x-spendctl-decision header is unknown, the payment was sent and its outcome is not confirmed, so do not pay again. When the paid request itself could not be completed the sidecar says so with spendctl_upstream_error, which read_refusal returns as well (none of denied, needs_approval, dry_run is set on it).

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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spendctl-sdk 0.2.1
File Size Uploaded Provenance Actions
spendctl_sdk-0.2.1.tar.gz 22.3 kB With provenance Details for spendctl_sdk-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for spendctl-sdk 0.2.1
File Interpreter ABI Platform Actions
spendctl_sdk-0.2.1-py3-none-any.whl Python 3 none any Details for spendctl_sdk-0.2.1-py3-none-any.whl

Total release size: 40.8 kB

Release files / spendctl_sdk-0.2.1.tar.gz

Download URL spendctl_sdk-0.2.1.tar.gz
Size 22.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c9c66b1cca0f9a825a3581f3a197dbc138a608c2c21ce1e866f9a236f8f1f7e6
BLAKE2b-256 checksum
How to use checksums
735f4a453e32ef02a472d0f18728e2fbc33465935e9a896b0fee4a7b410d1195
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 14, 2026.

Transparency log

Release files / spendctl_sdk-0.2.1-py3-none-any.whl

Download URL spendctl_sdk-0.2.1-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
399ac84a78aaf44193346d8377283dacedcc8d581dec9b180d17f4fcd172ea29
BLAKE2b-256 checksum
How to use checksums
47a6e47d865aa3baa7d0888da6d48c625a054faf424cb30c92607b412fe48a94
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 14, 2026.

Transparency log

Security for spendctl-sdk 0.2.1

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.

Select from 2 files

Attestation for spendctl_sdk-0.2.1.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 14, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page