Skip to main content

SpendGuard SDK — runtime safety layer client for AI agent frameworks (Pydantic-AI, LangChain, LangGraph, OpenAI Agents SDK).

Project description

spendguard-sdk

Python SDK for Agentic SpendGuard — the audit-chain spend control layer for LLM agents. Talks to the Agentic SpendGuard sidecar over Unix-domain-socket gRPC; gates each LLM / tool-call boundary through a Contract DSL evaluator and an atomic budget ledger with KMS-signed, immutable audit chain.

Install

# Core only (raw client, no framework integration)
pip install spendguard-sdk

# With the integration you need
pip install 'spendguard-sdk[pydantic-ai]'
pip install 'spendguard-sdk[langchain]'
pip install 'spendguard-sdk[langgraph]'
pip install 'spendguard-sdk[openai-agents]'

Quickstart (Pydantic-AI)

import asyncio
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel

from spendguard import SpendGuardClient, new_uuid7
from spendguard.integrations.pydantic_ai import (
    RunContext,
    SpendGuardModel,
    run_context,
)
from spendguard._proto.spendguard.common.v1 import common_pb2


async def main():
    client = SpendGuardClient(
        socket_path="/var/run/spendguard/adapter.sock",
        tenant_id="00000000-0000-4000-8000-000000000001",
    )
    await client.connect()
    await client.handshake()

    guarded = SpendGuardModel(
        inner=OpenAIModel("gpt-4o-mini"),
        client=client,
        budget_id="44444444-4444-4444-8444-444444444444",
        window_instance_id="55555555-5555-4555-8555-555555555555",
        unit=common_pb2.UnitRef(
            unit_id="66666666-6666-4666-8666-666666666666",
            token_kind="output_token",
            model_family="gpt-4",
        ),
        pricing=common_pb2.PricingFreeze(
            pricing_version="demo-pricing-v1",
            price_snapshot_hash=b"<32 bytes>",
            fx_rate_version="demo-fx-v1",
            unit_conversion_version="demo-units-v1",
        ),
        claim_estimator=lambda messages, settings: [
            common_pb2.BudgetClaim(
                budget_id="44444444-4444-4444-8444-444444444444",
                unit=common_pb2.UnitRef(
                    unit_id="66666666-6666-4666-8666-666666666666",
                    token_kind="output_token",
                    model_family="gpt-4",
                ),
                amount_atomic="500",
                direction=common_pb2.BudgetClaim.DEBIT,
                window_instance_id="55555555-5555-4555-8555-555555555555",
            )
        ],
    )

    agent = Agent(model=guarded)
    async with run_context(RunContext(run_id=str(new_uuid7()))):
        result = await agent.run("Say hello in three words.")
    print(result.output)


asyncio.run(main())

If a contract rule denies the call, agent.run(...) raises spendguard.DecisionStopped carrying reason_codes and matched_rule_ids.

API surface (core)

Symbol Purpose
SpendGuardClient UDS gRPC client to the sidecar
DecisionStopped, DecisionSkipped, ApprovalRequired per-decision exceptions
derive_idempotency_key(...) deterministic key from (tenant, run, step, llm_call, trigger)
new_uuid7() UUID v7 helper

Wire-protocol compatibility

This SDK pins to a specific protobuf wire version. Check the sidecar's published version against spendguard.__version__; minor versions are wire-compatible, major bumps are breaking. (Not yet enforced at handshake; planned for v0.2.)

License

Apache-2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spendguard_sdk-0.3.0.tar.gz (95.5 kB view details)

Uploaded Source

Built Distribution

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

spendguard_sdk-0.3.0-py3-none-any.whl (87.3 kB view details)

Uploaded Python 3

File details

Details for the file spendguard_sdk-0.3.0.tar.gz.

File metadata

  • Download URL: spendguard_sdk-0.3.0.tar.gz
  • Upload date:
  • Size: 95.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for spendguard_sdk-0.3.0.tar.gz
Algorithm Hash digest
SHA256 03651493ee0549f247ea901fb542ccd0eb7e2b3d6c104fcd54f6cfb7a262197d
MD5 6d5ad79588b99b4ec75bb3aeeeffe678
BLAKE2b-256 6294f76e4f20289c660d9a87a0aecea522cdd980bd6f8001938a1c73f5a4ed63

See more details on using hashes here.

File details

Details for the file spendguard_sdk-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: spendguard_sdk-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 87.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for spendguard_sdk-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9f4f38aa51855c622fa9a2395ed70d8baee8349951245dcebac880650777f306
MD5 9fd99a4c8fbaa2ea056df81443c1303d
BLAKE2b-256 a8cbc235af5ad596ea00884765af122c16a3f98696ead0f2ae4a715a616af35f

See more details on using hashes here.

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