CloudCircuit Python package.
Project description
cloudcircuit
Lightweight, deterministic safeguards you can embed into cloud spend enforcement:
- Budget usage checks (
check_budget) - Spend spike detection (
check_anomaly_spike) - Simple circuit-breaker decisioning (
evaluate_circuit_breaker)
This package focuses on the core logic only. You bring your own metrics ingestion (billing exports, CloudWatch, BigQuery, etc.) and enforcement (alerts, policy, throttling, kill-switches).
Install
From PyPI (once published):
python -m pip install cloudcircuit
For local development from this repo:
cd cloudcircuit
python -m pip install -e ".[dev]"
pytest
Usage
The public APIs currently live in cloudcircuit.safeguards:
from cloudcircuit.safeguards import (
check_anomaly_spike,
check_budget,
evaluate_circuit_breaker,
)
All functions are deterministic and raise ValueError on invalid inputs (negative spend, invalid thresholds, etc.).
Quickstart
1) Budget guardrail
from cloudcircuit.safeguards import check_budget
result = check_budget(current_spend=920.0, budget_limit=1000.0, warning_ratio=0.9)
if result.is_breached:
# Hard stop: disable non-essential workloads, block expensive operations, etc.
print("BUDGET BREACHED:", result)
elif result.is_warning:
# Soft stop: alert, reduce concurrency, apply stricter policy.
print("BUDGET WARNING:", result)
else:
print("OK:", result)
2) Spike detection (latest point vs baseline)
from cloudcircuit.safeguards import check_anomaly_spike
# Example: daily spend totals (or hourly), latest point last.
series = [120.0, 125.0, 118.0, 260.0]
result = check_anomaly_spike(series, spike_multiplier=1.5, min_baseline=0.0)
if result.is_spike:
print("SPEND SPIKE:", result.latest_spend, "threshold:", result.threshold)
3) Circuit-breaker decisioning
from cloudcircuit.safeguards import evaluate_circuit_breaker
decision = evaluate_circuit_breaker(
consecutive_failures=3,
failure_threshold=3,
cooldown_steps_remaining=0,
)
if not decision.allow_operation:
print("BREAKER OPEN; retry after steps:", decision.retry_after_steps)
Architecture
Current modules:
src/cloudcircuit/safeguards.py: core dataclasses and guardrail functions:BudgetCheckResult,check_budgetAnomalyCheckResult,check_anomaly_spikeCircuitBreakerDecision,evaluate_circuit_breaker
tests/test_safeguards.py: contract tests for edge cases and input validation.
Design goals:
- Deterministic, side-effect free logic (easy to unit-test and reason about)
- Minimal dependencies (safe to embed in CLIs, jobs, and services)
- Typed, explicit return objects (so callers can log/audit decisions)
Non-goals (by design):
- Cloud provider integrations (metrics ingestion / policy enforcement)
- Stateful breaker implementation (persistence, jitter/backoff, distributed locks)
Deployment Guide
Typical deployment looks like a thin integration layer around cloudcircuit:
- Collect spend and operational signals.
- Spend totals: billing exports, cost explorer, warehouse tables, or internal chargeback.
- Failure counters: request error rates, provider API failures, budget retrieval failures.
- Evaluate guardrails.
check_budget()for hard/soft budget thresholds.check_anomaly_spike()to catch sudden jumps early.evaluate_circuit_breaker()to gate high-cost operations when systems are unstable.
- Enforce decisions.
- Alerting: Slack/email/PagerDuty with
*_Resultfields for audit context. - Throttling: reduce concurrency, disable non-critical jobs, or switch to cheaper defaults.
- Kill-switch: block expensive actions when
is_breachedis true.
- Alerting: Slack/email/PagerDuty with
- Persist state externally when needed.
- Store the last N spend points, consecutive failure counts, and cooldown counters in your DB/redis.
- Run the evaluator on a schedule (cron / Airflow / Cloud Scheduler) or inline per request.
Recommended patterns:
- Treat spend series as monotonic time buckets (hourly/daily) and pass the newest point last.
- Log the full result objects (they are small and serializable) so you can explain why a safeguard tripped.
- Prefer "fail closed" for high-risk operations: if your metrics pipeline is down, open the breaker or block expensive actions until signals recover.
License
MIT (see pyproject.toml for the current license declaration).
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
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 cloudcircuit-0.1.0.tar.gz.
File metadata
- Download URL: cloudcircuit-0.1.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
beec4f890b6d666b73f4b73a9bffcf81e9b931f1be032e92178db5ba068874cd
|
|
| MD5 |
508d0b7c3b68cbc10cc6f58db7b4208f
|
|
| BLAKE2b-256 |
9f8bb8f2574c1af04450ed0e4a6973aa2ad1f57352f06ce0e92f6d135151def4
|
File details
Details for the file cloudcircuit-0.1.0-py3-none-any.whl.
File metadata
- Download URL: cloudcircuit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b47a822a6080dd89f476594b04856e8cf07137f055e9a54aa406a1adba430af5
|
|
| MD5 |
5b9e480c43aaee8d543f22ac551b681d
|
|
| BLAKE2b-256 |
27ab43b190a4d2a3a4232febe21c632d7f7ca14912d0b0edf1a66167d7fce919
|