Skip to main content

Python SDK for the Tollgate agent control plane

Project description

tollgate-sdk

Python SDK for Tollgate — the policy and approval layer for AI agents.

Install

pip install tollgate-sdk

5-minute quickstart

from tollgate import Tollgate

tg = Tollgate(
    api_key="tg_live_...",
    base_url="http://localhost:8000",  # your Tollgate API
)

@tg.guard("issue_refund")
def issue_refund(amount: float, customer_id: str) -> dict:
    # Your actual refund logic here
    return {"refunded": amount, "customer": customer_id}

# Safe to call — Tollgate checks policy before executing
result = issue_refund(amount=50.0, customer_id="c_123")

When the policy says require_approval, the decorator blocks until a human approves or rejects in Slack.

Usage patterns

Decorator (recommended)

@tg.guard("issue_refund")
def issue_refund(amount: float, customer_id: str) -> dict:
    return stripe.refund(amount=amount, customer=customer_id)

Context manager

with tg.check("issue_refund", {"amount": 500, "customer_id": "c_123"}):
    stripe.refund(amount=500, customer="c_123")

Explicit

decision = tg.check_action("issue_refund", {"amount": 500})
if decision.allowed:
    stripe.refund(...)

Error handling

Exception When How to handle
ActionDenied Policy denied or human rejected Abort the action, inform the user
ActionPending Approval timed out Retry later or escalate
TollgateAuthError Invalid API key Check your api_key
TollgateConnectionError Can't reach Tollgate API Check network; consider fail_open=True
from tollgate import ActionDenied, ActionPending, TollgateConnectionError

try:
    result = issue_refund(amount=500, customer_id="c_123")
except ActionDenied as e:
    print(f"Refund not allowed: {e.reason}")
except ActionPending as e:
    print(f"Timed out waiting for approval: {e.action_id}")
except TollgateConnectionError:
    print("Tollgate unreachable")

fail_open

By default, if Tollgate is unreachable the SDK raises TollgateConnectionError (safe — nothing proceeds). Set fail_open=True to allow actions through when Tollgate is down:

tg = Tollgate(api_key="...", fail_open=True)

Risk: If Tollgate is down, all actions proceed without policy checks. Only use fail_open=True when availability matters more than safety for your use case.

Async usage

from tollgate import AsyncTollgate

tg = AsyncTollgate(api_key="tg_live_...")

@tg.aguard("issue_refund")
async def issue_refund(amount: float, customer_id: str) -> dict:
    return await stripe_async.refund(amount=amount)

# In an async context:
result = await issue_refund(amount=50.0, customer_id="c_123")

Configuration

tg = Tollgate(
    api_key="tg_live_...",
    base_url="https://api.tollgate.dev",  # override for local dev
    poll_interval=2.0,   # seconds between approval polls
    max_wait=300.0,      # max seconds to wait for approval
    on_pending=lambda action_id: print(f"Waiting for approval: {action_id}"),
    fail_open=False,     # raise on network errors (default)
    timeout=10.0,        # HTTP request timeout
)

Links

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

tollgate_sdk-0.1.0.tar.gz (45.8 kB view details)

Uploaded Source

Built Distribution

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

tollgate_sdk-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file tollgate_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: tollgate_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 45.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.11.15 HTTPX/0.28.1

File hashes

Hashes for tollgate_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 40fd9e002e9c8727e583340f6a1338d838a2ee59e1d09aaff31b903f2a2a8fa9
MD5 f051e0f657c75250e128f42bfcacc78a
BLAKE2b-256 32d9f090d4d9185300d2c52d1264114fff164a522d89bfe02f6714dce536114c

See more details on using hashes here.

File details

Details for the file tollgate_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tollgate_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.11.15 HTTPX/0.28.1

File hashes

Hashes for tollgate_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2b6d4bfec08869dc6be86831b28cbb1397e3403f5c935966f541b5c4c1695e33
MD5 a32d000d9dcd8e75eaa358eb658e83b5
BLAKE2b-256 c24287beb1df436c75f99cc865e8b18d6f2a228e12f6b971896b7074af6a5f3f

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