⚓ 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 callcredits+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 stepexamples/langchain_callback.py— LangChainBaseCallbackHandlerwiring
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85d4c10bb298a8d8f9e15d217f1c0fc9eef8321861cfbbd61aee00c54412f402
|
|
| MD5 |
ac7c11add9edd0a87d2e759c24b3e75c
|
|
| BLAKE2b-256 |
58176cd38191f152e2b72c3f3d7b9ba60463289b1e6faaf126af4cd51e16c4bc
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c1e1b929520204096b8a48ef91405cbc79ee13793cc030e815cc4aa520d3a0e
|
|
| MD5 |
e6165774e7195675694f7c49d098fa7f
|
|
| BLAKE2b-256 |
03fa9e3fb71c422eead8cbb3e80d6b3dcdd64b243d4dae9e2d90471bbaafa5a9
|