Metadata-Version: 2.5
Name: spendctl-sdk
Version: 0.2.0
Summary: Zero-dependency Python client for the spendctl sidecar: route paid HTTP calls through your spend policy, read budgets and receipts, approve from tooling.
Project-URL: Homepage, https://spendctl.dev
Project-URL: Documentation, https://spendctl.dev/docs/integrations/
Project-URL: Source, https://github.com/miraistudio/spendctl-sdk
Project-URL: Changelog, https://github.com/miraistudio/spendctl-sdk/blob/main/packages/python/CHANGELOG.md
Project-URL: Issues, https://github.com/miraistudio/spendctl-sdk/issues
Author-email: "Mirai Studio, Inc." <security@spendctl.dev>
License-Expression: Apache-2.0
License-File: LICENSE
License-File: NOTICE
Keywords: agents,approvals,budget,httpx,mpp,payments,requests,spendctl,x402
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Python: >=3.10
Provides-Extra: httpx
Requires-Dist: httpx>=0.27; extra == 'httpx'
Provides-Extra: requests
Requires-Dist: requests>=2.31; extra == 'requests'
Description-Content-Type: text/markdown

# spendctl-sdk

Zero-dependency Python client for the [spendctl](https://spendctl.dev) 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/>).

```sh
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):

```python
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:**

```python
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:**

```python
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.

```python
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.

```python
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.
