A lightweight Python utility to prevent cloud cost landmines by monitoring and intercepting sudden HTTP request spikes.
Project description
AI GuardRail 🚨
A lightweight Python utility designed to prevent cloud cost landmines. It monitors HTTP requests and automatically intercepts and blocks sudden spikes or infinite loop request patterns (e.g., more than 50 calls to the same endpoint in 10 seconds), saving you from unexpected API bills.
Features
- In-Process Monkey Patching: Zero-config protection for your existing Python applications. Instantly intercepts and blocks excessive requests using standard libraries (
urllib,urllib3,requests). - Standalone Proxy Server: Can run as a local proxy on any port, letting any application/environment route requests through it using
HTTP_PROXY. - Dynamic Sliding Window: Tracks request history using a thread-safe sliding window.
- Customizable Thresholds: Easily customize request rate limits and window sizes.
Installation
pip install ai-cost-guardrail
Usage
1. In-Process Monkey-Patcher (Python Apps)
Import ai_guardrail at the very beginning of your application entry point to intercept cost landmines:
import ai_guardrail
# Block requests if an endpoint is called more than 30 times in 10 seconds
ai_guardrail.enable(max_calls=30, window_seconds=10)
import requests
# Any HTTP requests made via urllib, urllib3, requests, etc., are now automatically protected!
If a loop triggers, a RuntimeError is raised, blocking the request from hitting the cloud API and saving your budget.
2. Standalone Proxy Server
You can run the GuardRail proxy server locally and route any external application traffic through it.
Run via CLI:
ai-cost-guardrail-proxy --port 8090 --max-calls 50 --window 10
Or via Python:
import ai_guardrail
ai_guardrail.run_proxy_server(port=8090)
Then configure your environment variables to use the proxy:
export HTTP_PROXY="http://127.0.0.1:8090"
export HTTPS_PROXY="http://127.0.0.1:8090"
Running Tests
To run unit tests:
python -m unittest discover -s tests
License
MIT
Project details
Release history Release notifications | RSS feed
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 ai_cost_guardrail-0.1.0.tar.gz.
File metadata
- Download URL: ai_cost_guardrail-0.1.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
024fc685a30c899c489dca67515b64cd5d855ddcd34a8be9021d82501b1e8af0
|
|
| MD5 |
bd47a4875198f4e1bf31baa9a70555ce
|
|
| BLAKE2b-256 |
1df3354fec3ef6aace16a0392b00c4721ffd2d06442994b5c480a0ce3916223d
|
File details
Details for the file ai_cost_guardrail-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ai_cost_guardrail-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94bb8bdadd4f0d62cea5ee56df7f4eb5906b574407760c09a773a5f76403b171
|
|
| MD5 |
2e5e37505ec841dbf4aff9d0786037fc
|
|
| BLAKE2b-256 |
f9440e79277a218bb44b1d7ff04ec9b63abf460ce82166826d60f1333039d231
|