Cost Control 3-Tier API Spend Protection
Prevent runaway API costs with 3-tier protection: caution, emergency, and hard cap. Works for ANY expensive API GPT-4, Claude Opus, Gemini, cloud services.
Prevent runaway API costs. Works for ANY expensive API (GPT-4, Claude Opus, Gemini, cloud services).

[
]()
Cost Control System is a production-grade cost monitoring framework with tiered responses, external watchdog, and kill switch.
Originally built to prevent catastrophic AI API spend after a real runaway cost incident, now generalized for any expensive API that charges per request/token/compute.
The Problem
You're running a system that makes API calls to an expensive service (OpenAI GPT-4, Anthropic Claude Opus, Google Gemini, cloud compute, etc.).
One bug, one infinite loop, one configuration error → hundreds of dollars burned in minutes.
Manual monitoring doesn't work. You need automated cost controls with failsafes.
The Solution
Cost Control System implements 3-tier protection:
Tier 1: Caution Mode (Warning)
When cost velocity exceeds threshold (e.g., $3/15min):
- - Slow down check intervals
- Log warnings
- Continue operating but throttled
Tier 2: Emergency Mode (Stop)
When cost velocity is dangerous (e.g., $5/15min):
- - Stop all API calls
- Enter maintenance mode
- Preserve existing state
- Alert operator
Tier 3: Kill Switch (External Watchdog)
Independent process monitors cost separately:
- - If Tier 2 fails to stop runaway spend
- Kill the entire process
- Prevent restart until manually cleared
- Last line of defense
Quick Start (3 Steps)
1. Install
CODEBLOCK0
2. Integrate with Your API Client
CODEBLOCK1
3. Deploy External Watchdog (Optional but Recommended)
CODEBLOCK2
Time to integrate: 15-20 minutes.
Use Cases
AI API Calls (GPT-4, Claude Opus, Gemini)
Prevent runaway token usage from:
- - Infinite loops generating responses
- Misconfigured context windows (100K+ tokens per call)
- Prompt injection causing excessive output
Cloud Compute (AWS Lambda, GCP Functions, Azure)
Monitor spend on:
- - Serverless function invocations
- Compute hours (EC2, VMs)
- Database queries (expensive per-query pricing)
Third-Party APIs (Stripe, Twilio, SendGrid)
Track costs for:
- - SMS/email sends
- Payment processing fees
- Data enrichment APIs
Research/ML Training
Control costs for:
- - GPU compute time
- Model training runs
- Batch inference jobs
Any API with measurable per-request or per-unit costs benefits from this.
Architecture
3-Tier Response System
CODEBLOCK3
Tier 1: Caution Mode
Trigger: 15-minute cost > $3 (configurable)
Response:
- - Slow down check intervals (reduce API call frequency)
- Log warnings
- Continue operating
Exit: 15-minute cost < $2 for 5 minutes
Tier 2: Emergency Mode
Trigger: 15-minute cost > $5 OR daily cost > $25 (configurable)
Response:
- - Block all API calls (
is_call_allowed() returns False) - Write emergency flag file
- Send alerts (Discord, Slack, email, etc.)
- System remains alive but paused
Exit: Manual intervention required (rm state/cost_emergency.flag)
Tier 3: Kill Switch (External Watchdog)
Trigger: Hourly cost > $12 OR daily cost > $30 (configurable, higher than Tier 2)
Response:
- - Kill the entire process (SIGTERM → SIGKILL)
- Write emergency flag (prevents restart)
- Independent of main process (can't be bypassed)
Purpose: Catch cases where Tier 2 fails (e.g., bug in cost tracker itself)
Configuration
Basic Config (Minimal Setup)
CODEBLOCK4
Advanced Config (All Options)
CODEBLOCK5
Pricing Config (Your API)
CODEBLOCK6
See config_example.py for complete examples.
API Reference
Core Methods
record_call(input_units, output_units, request_id=None)
Record an API call with actual usage.
Args:
- -
input_units (int): Input units consumed (e.g., tokens, requests) - INLINECODE4 (int): Output units consumed
- INLINECODE5 (str, optional): Identifier for logging
Returns: True if within limits, False if emergency triggered
is_call_allowed()
Check if API calls are currently allowed.
Returns: (allowed: bool, reason: str)
- -
allowed=True, reason="ok" → Safe to call API - INLINECODE11 → Blocked
Usage:
allowed, reason = tracker.is_call_allowed()
if not allowed:
handle_emergency(reason)
return
get_stats()
Get current cost statistics.
Returns: Dict with keys:
{
"cost_5min": 0.45,
"cost_15min": 1.23,
"cost_1hour": 3.45,
"cost_daily": 12.34,
"calls_1hour": 23,
"calls_session": 156,
"caution_mode": False,
"emergency_mode": False,
}
clear_emergency()
Clear emergency mode (manual intervention).
Usage:
tracker.clear_emergency()
# Or: rm state/cost_emergency.flag
Properties
- -
caution_mode (bool) — Whether in Tier 1 caution emergency_mode (bool) — Whether in Tier 2 emergencycost_15min (float) — Rolling 15-minute costcost_daily (float) — Total cost today (UTC)
External Watchdog Setup
1. Copy Watchdog Script
CODEBLOCK10
2. Configure Watchdog
Edit
cost_watchdog.py:
CODEBLOCK11
3. Deploy via Cron
CODEBLOCK12
4. Verify Watchdog is Running
tail -f logs/watchdog.log
# Should see entries every 2 minutes
Kill Switch Usage
Manual Kill Switch (Immediate Stop)
CODEBLOCK14
Check Status
CODEBLOCK15
Resume After Emergency
python3 kill_switch.py disable
rm state/cost_emergency.flag # Clear emergency flag
# Restart your application
Testing
Unit Tests
CODEBLOCK17
Integration Test (Simulate Runaway)
from cost_control import CostTracker
tracker = CostTracker(
cost_emergency_15min=1.00, # Low threshold for testing
)
# Simulate 50 expensive calls
for i in range(50):
allowed, reason = tracker.is_call_allowed()
if not allowed:
print(f"Blocked after {i} calls: {reason}")
break
# Simulate $0.10 per call
tracker.record_call(10000, 50000, request_id=f"test-{i}")
# Should trigger emergency after ~10 calls
# Verify emergency mode
assert tracker.emergency_mode
print("✅ Emergency mode triggered successfully")
Limitations
See LIMITATIONS.md for details, including:
- - Clock skew between system and API
- Delayed cost reporting (API quotas update hourly)
- Watchdog reliability (cron timing jitter)
- Multi-process coordination
Performance
Memory
- - ~100 bytes per recorded call
- Rolling window of last 5000 calls ≈ 500KB memory
- Daily total persists across restarts
CPU
- - Cost calculation: O(1) per call
- Threshold checks: O(1)
- Rolling window: O(N) where N = calls in window (typically <500)
I/O
- - State saves: atomic writes on emergency only
- Cost log: append-only, rotates at 10K entries
Troubleshooting
Problem: Emergency mode triggered but spend seems normal
Cause: Clock skew — system time doesn't match API billing time
Fix: Sync system clock with NTP (
ntpdate or
chrony)
Problem: Watchdog doesn't kill runaway process
Cause: Cron not running or watchdog script errors
Fix: Check cron logs (
/var/log/syslog), verify script permissions
Problem: Frequent caution mode cycling
Cause: Threshold too low for your use case
Fix: Raise
cost_caution_15min to match normal usage peaks
Real-World Example: Runaway API Burn Incident
What happened:
- - A trading bot had dozens of orphaned positions
- Each position triggered an expensive AI API call every few minutes
- Costs compounded rapidly — over $100/hour burn rate
- Ran for over an hour before manual discovery = $150+ burn
How Cost Control would have stopped it:
- 1. Tier 1 (~5 min): Caution at velocity threshold → slow down checks
- Tier 2 (~8 min): Emergency at higher threshold → stop all API calls
- Tier 3 (~10 min): Watchdog kill at hourly cap → process terminated
Result: Max damage $5-8 instead of $150+
Migration Guide
From No Cost Tracking
- 1. Calculate baseline cost (1 day of normal usage)
- Set
cost_caution_15min = 2x baseline peak - Set
cost_emergency_15min = 3x baseline peak - Set
cost_daily_cap = acceptable daily budget - Deploy watchdog with higher thresholds (2x emergency)
- Run parallel for 1-2 days (log only, no blocking)
- Enable blocking once validated
From Manual Monitoring
- 1. Export historical cost data to CSV
- Analyze 95th percentile cost per 15-minute window
- Set thresholds based on percentiles
- Integrate tracker with existing alerting
- Gradually enable tiers (start with Tier 1 only)
Contributing
Cost Control System is open source and community-maintained.
Report Issues
- - Bugs, false positives, performance problems
- Integration confusion
- Feature requests (multi-currency, better alerting, etc.)
Submit Improvements
- - Additional API pricing examples
- Watchdog reliability improvements
- Dashboard/visualization tools
Contribution guidelines: Open an issue or PR on GitHub. We review everything.
Philosophy: Why Open Source?
Cost Control solves a universal infrastructure problem — preventing runaway API spend. This affects everyone building on paid APIs.
This is a goodwill project to:
- 1. Prevent the $153 incident from happening to others
- Prove the vibe coding methodology (adaptive systems that heal themselves)
- Build reputation for future paid Shadow Rose products
Support & Community
Get Help
- - 📖 Documentation: You're reading it
- 💬 Discord: https://discord.com/invite/clawd (OpenClaw community)
- 🐦 Twitter: https://x.com/TheShadowyRose
- 🐛 Issues: [GitHub link]
Support Development
Cost Control is free, but if it saved you money:
☕ Ko-fi: https://ko-fi.com/theshadowrose
Donations support ongoing testing, docs, and new features.
License
MIT License — use freely, credit appreciated.
Copyright © 2026 Shadow Rose
See LICENSE for details.
What's Next?
Planned Features
- - Multi-currency support — Track costs in multiple currencies
- Budget forecasting — Predict end-of-day/month spend
- Granular alerting — Per-endpoint or per-user cost tracking
- Dashboard — Web UI for real-time cost monitoring
Want to work on these? Open an issue or reach out on Discord.
Credits
Created by: Shadow Rose
Extracted from: Production trading bot (battle-tested in live operation)
Philosophy: Vibe coding — infrastructure that protects itself
License: MIT
Built from a real runaway cost incident — learned the hard way so you don't have to.
Final Thought
Every system that calls expensive APIs will eventually have a runaway cost incident.
The question is: does your system detect and stop it in 5 minutes, or do you find out when the $1000 bill arrives?
Cost Control ensures you find out in 5 minutes.
Ship it. Break it. Tell us what you find.
— Shadow Rose
February 2026
☕ Support: https://ko-fi.com/theshadowrose
🐦 Follow: https://x.com/TheShadowyRose
💬 Community: https://discord.com/invite/clawd
⚠️ Disclaimer
This software is provided "AS IS", without warranty of any kind, express or implied.
USE AT YOUR OWN RISK.
- - The author(s) are NOT liable for any damages, losses, or consequences arising from
the use or misuse of this software — including but not limited to financial loss,
data loss, security breaches, business interruption, or any indirect/consequential damages.
- - This software does NOT constitute financial, legal, trading, or professional advice.
- Users are solely responsible for evaluating whether this software is suitable for
their use case, environment, and risk tolerance.
- - No guarantee is made regarding accuracy, reliability, completeness, or fitness
for any particular purpose.
- - The author(s) are not responsible for how third parties use, modify, or distribute
this software after purchase.
By downloading, installing, or using this software, you acknowledge that you have read
this disclaimer and agree to use the software entirely at your own risk.
TRADING DISCLAIMER: This software is a tool, not a trading system. It does not
make trading decisions for you and does not guarantee profits. Trading cryptocurrency,
stocks, or any financial instrument carries significant risk of loss. You can lose
some or all of your investment. Past performance of any system or methodology is not
indicative of future results. Never trade with money you cannot afford to lose.
Consult a qualified financial advisor before making investment decisions.
Support & Links
| |
|---|
| 🐛 Bug Reports | TheShadowyRose@proton.me |
| ☕ Ko-fi |
ko-fi.com/theshadowrose |
| 🛒
Gumroad |
shadowyrose.gumroad.com |
| 🐦
Twitter |
@TheShadowyRose |
| 🐙
GitHub |
github.com/TheShadowRose |
| 🧠
PromptBase |
promptbase.com/profile/shadowrose |
Built with OpenClaw — thank you for making this possible.
🛠️
Need something custom? Custom OpenClaw agents & skills starting at $500. If you can describe it, I can build it. →
Hire me on Fiverr
成本控制三层API支出保护
通过三层保护机制防止API成本失控:警告、紧急和硬上限。适用于任何昂贵的API——GPT-4、Claude Opus、Gemini、云服务。
防止API成本失控。适用于任何昂贵的API(GPT-4、Claude Opus、Gemini、云服务)。

[
]()
成本控制系统是一个生产级成本监控框架,具备分层响应、外部看门狗和紧急终止开关。
最初是为了在真实的成本失控事件后防止灾难性的AI API支出而构建的,现已推广到任何按请求/令牌/计算收费的昂贵API。
问题
你正在运行一个系统,该系统向昂贵的服务(OpenAI GPT-4、Anthropic Claude Opus、Google Gemini、云计算等)发出API调用。
一个bug、一个无限循环、一个配置错误 → 几分钟内烧掉数百美元。
手动监控行不通。 你需要具备故障安全机制的自动化成本控制。
解决方案
成本控制系统实现了三层保护:
第一层:警告模式(警告)
当成本速度超过阈值时(例如,15分钟3美元):
第二层:紧急模式(停止)
当成本速度危险时(例如,15分钟5美元):
- - 停止所有API调用
- 进入维护模式
- 保留现有状态
- 通知操作员
第三层:紧急终止开关(外部看门狗)
独立进程单独监控成本:
- - 如果第二层未能阻止失控支出
- 终止整个进程
- 防止重启,直到手动清除
- 最后一道防线
快速入门(3步)
1. 安装
bash
pip install cost-control-system
或者将cost_control.py复制到你的项目中
2. 集成到你的API客户端
python
from cost_control import CostTracker
tracker = CostTracker(
costcaution15min=3.00, # 15分钟3美元时警告
costemergency15min=5.00, # 15分钟5美元时紧急
costdailycap=25.00, # 每日硬上限25美元
)
每次API调用前,检查是否允许
allowed, reason = tracker.is
callallowed()
if not allowed:
print(fAPI调用被阻止:{reason})
# 处理紧急模式(警报、退出等)
return
进行API调用
response = your
expensiveapi_call()
记录实际成本
input
tokens = response.usage.inputtokens
output
tokens = response.usage.outputtokens
tracker.record
call(inputtokens, output
tokens, requestid=req-123)
3. 部署外部看门狗(可选但推荐)
bash
每2分钟运行外部看门狗(通过cron)
/2 * cd /your/project && python3 cost_watchdog.py >> logs/watchdog.log 2>&1
集成时间:15-20分钟。
使用场景
AI API调用(GPT-4、Claude Opus、Gemini)
防止以下情况导致的令牌使用失控:
- - 生成响应的无限循环
- 配置错误的上下文窗口(每次调用10万+令牌)
- 导致过度输出的提示注入
云计算(AWS Lambda、GCP Functions、Azure)
监控以下方面的支出:
- - 无服务器函数调用
- 计算小时数(EC2、虚拟机)
- 数据库查询(昂贵的按查询定价)
第三方API(Stripe、Twilio、SendGrid)
跟踪以下成本:
研究/机器学习训练
控制以下成本:
任何具有可衡量的按请求或按单位成本的API都能从中受益。
架构
三层响应系统
正常 → 警告(第一层) → 紧急(第二层) → 终止(第三层)
↓ ↓ ↓ ↓
正常 减慢速度 停止调用 终止进程
第一层:警告模式
触发条件: 15分钟成本 > 3美元(可配置)
响应:
- - 减慢检查间隔(降低API调用频率)
- 记录警告
- 继续运行
退出条件: 15分钟成本 < 2美元持续5分钟
第二层:紧急模式
触发条件: 15分钟成本 > 5美元 或 每日成本 > 25美元(可配置)
响应:
- - 阻止所有API调用(iscallallowed()返回False)
- 写入紧急标志文件
- 发送警报(Discord、Slack、电子邮件等)
- 系统保持存活但暂停
退出条件: 需要手动干预(rm state/cost_emergency.flag)
第三层:紧急终止开关(外部看门狗)
触发条件: 每小时成本 > 12美元 或 每日成本 > 30美元(可配置,高于第二层)
响应:
- - 终止整个进程(SIGTERM → SIGKILL)
- 写入紧急标志(防止重启)
- 独立于主进程(无法绕过)
目的: 捕获第二层失败的情况(例如,成本跟踪器本身的bug)
配置
基本配置(最小设置)
python
from cost_control import CostTracker
tracker = CostTracker(
# 第一层(警告)
costcaution15min=3.00,
# 第二层(紧急)
costemergency15min=5.00,
costdailycap=25.00,
# 状态持久化
state_dir=./state,
)
高级配置(所有选项)
python
tracker = CostTracker(
# 阈值
cost
caution15min=3.00,
cost
emergency15min=5.00,
cost
dailycap=25.00,
max
costper_call=0.50, # 单次调用合理性检查
# 恢复
cautionrecoverythreshold=2.00, # 低于2美元/15分钟时退出警告
cautionrecoveryduration=300, # 必须保持低于阈值5分钟
# 状态
state_dir=./state,
costlogfile=cost_log.jsonl,
# 警报
alertcallback=myalert_function,
)
定价配置(你的API)
python
定义你的API定价
tracker.set_pricing(
input
priceper_unit=15.00, # 例如,每百万输入令牌15美元
output
priceper_unit=75.00, # 例如,每百万输出令牌75美元
unit
size=1000_000, # 例如,每百万令牌
)
对于非令牌API(例如,按请求定价):
tracker.set_pricing(
fixed
costper_call=0.01, # 每次API调用0.01美元
)
参见config_example.py获取完整示例。
API参考
核心方法
recordcall(inputunits, outputunits, requestid=None)
记录带有实际使用量的API调用。
参数:
- - inputunits(int):消耗的输入单位(例如,令牌、请求)
- outputunits(int):消耗的输出单位
- request_id(str,可选):用于日志记录的标识符
返回: 如果在限制内返回True,如果触发紧急则返回False
iscallallowed()
检查当前是否允许API调用。
返回: (allowed: bool, reason: str)
- - allowed=True, reason=ok → 可以安全调用API
- allowed=False, reason=costemergencymode → 被阻止
用法:
python
allowed, reason = tracker.iscallallowed()
if not allowed:
handle_emergency(reason)
return
get_stats()
获取当前成本统计信息。
返回: 包含以下键的字典:
python
{
cost_5min: 0.45,
cost_15min: 1.23,
cost_1hour: 3.45,
cost_daily: 12.34,
calls_1hour: 23,
calls_session: 156,
caution_mode: False,
emergency_mode: False,
}
clear_emergency