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

Download files

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

Source Distribution

sayay-0.1.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

sayay-0.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sayay-0.1.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for sayay-0.1.0.tar.gz
Algorithm Hash digest
SHA256 85d4c10bb298a8d8f9e15d217f1c0fc9eef8321861cfbbd61aee00c54412f402
MD5 ac7c11add9edd0a87d2e759c24b3e75c
BLAKE2b-256 58176cd38191f152e2b72c3f3d7b9ba60463289b1e6faaf126af4cd51e16c4bc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sayay-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for sayay-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1c1e1b929520204096b8a48ef91405cbc79ee13793cc030e815cc4aa520d3a0e
MD5 e6165774e7195675694f7c49d098fa7f
BLAKE2b-256 03fa9e3fb71c422eead8cbb3e80d6b3dcdd64b243d4dae9e2d90471bbaafa5a9

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

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