Skip to main content

⚓ Sayay — AI Agent Cost Guardrails (Python)

Budget enforcement middleware for LLM calls. Prevent runaway AI costs with per-user daily/monthly/session budgets or credit systems. Zero dependencies.

Python port of @carloscortezcloud/sayay-guard.

Install

pip install sayay
# Redis storage (optional):
pip install 'sayay[redis]'

Quick Start

import asyncio
from sayay import SayayGuard, MemoryStorage

guard = SayayGuard(
    storage=MemoryStorage(),
    budget={"daily_usd": 5.0, "monthly_usd": 50.0},
    on_exceeded="block",
    degrade_to_model="meta-llama/llama-3.3-70b-instruct:free",
)

async def main():
    # Before LLM call:
    decision = await guard.check("user-123", estimated_cost=0.003)
    if decision.action == "block":
        raise RuntimeError(f"Budget exceeded: {decision.reason}")
    if decision.action == "degrade":
        # use decision.suggested_model instead of the expensive model
        pass

    # ... make the LLM call ...

    # After LLM call:
    await guard.record("user-123", actual_cost=0.004)

    usage = await guard.get_usage("user-123")
    print(usage.daily, usage.monthly)

asyncio.run(main())

Storage Backends

Backend When to use
MemoryStorage Tests, scripts, single-process agents
FileStorage Local CLI agents that must survive restarts
RedisStorage Production, shared across workers/pods

Implement SayayStorage (get/increment/reset) for your infra (KV, D1, DynamoDB, Firestore).

Budget options

  • daily_usd / monthly_usd / session_usd — USD limits (resets UTC midnight / month start)
  • per_call_max_usd — blocks a single expensive call
  • credits + credits_per_call — credit-based system

Actions

allow → proceed · warn (≥80%) → proceed + warn · degrade (≥95%) → use suggested_model · block → stop.

Examples

  • examples/strands_agent.py — Strands-style agent hooking check/record per step
  • examples/langchain_callback.py — LangChain BaseCallbackHandler wiring

Tests

uv venv --python 3.12 .venv && uv pip install -e '.[redis]' pytest pytest-asyncio
uv run pytest

License

Apache 2.0

Metadata

Release files for sayay 0.1.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 sayay 0.1.1
File Size Uploaded Provenance Actions
sayay-0.1.1.tar.gz 8.8 kB Without provenance Details for sayay-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sayay 0.1.1
File Interpreter ABI Platform Actions
sayay-0.1.1-py3-none-any.whl Python 3 none any Details for sayay-0.1.1-py3-none-any.whl

Total release size: 17.0 kB

Release files / sayay-0.1.1.tar.gz

Download URL sayay-0.1.1.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7c4c7e5e8cb46ce8faeeb62f1a2b9e8c3786fa89358f9fc812f68c68c7683e2e
BLAKE2b-256 checksum
How to use checksums
19ba9f9aa171c5162b978635d5d9ca04627f13208ebc93cb2712d510ab586f64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release files / sayay-0.1.1-py3-none-any.whl

Download URL sayay-0.1.1-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
529604aab47577b20a408eb914e98e19d167f2b70b4ddf6eb2b3e0e9a88d69bb
BLAKE2b-256 checksum
How to use checksums
9b46bcc992a3dc8526d1f5461e48ad099600f45d9b65fbc7d9686bec3a6202c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Security for sayay 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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