Spend protection plugin for Moltbot. Monitors API costs and stops the gateway when limits are breached.
Moltbot users wake up to $120+ API bills from runaway loops. No native spend limits exist.
A plugin that:
- Runs a background service checking spend every minute
- Gets session data via
moltbot sessions --json - Calculates cost using
models.providerspricing config - Alerts at 80% of limit, stops gateway at 100%
moltbot plugins install github:meridianix/moltbot-spend-guardAdd to ~/.clawdbot/moltbot.json:
{
"plugins": {
"entries": {
"spend-guard": {
"enabled": true,
"config": {
"dailyLimitUsd": 50,
"hourlyLimitUsd": 10
}
}
}
}
}Restart gateway for changes to take effect.
| Option | Default | Description |
|---|---|---|
dailyLimitUsd |
50 | Daily spend limit |
hourlyLimitUsd |
10 | Hourly spend limit |
checkIntervalMs |
60000 | Check interval (1 min) |
alertThreshold |
0.8 | Log warning at this % |
stopThreshold |
1.0 | Stop gateway at this % |
moltbot spend-guard status # Show current spend
moltbot spend-guard check # Force immediate check
moltbot spend-guard reset # Clear alert statemoltbot gateway call spend-guard.status --params '{}'Plugin loads
→ api.registerService({ id, start, stop })
→ setInterval(checkIntervalMs)
→ exec('moltbot sessions --json')
→ calculate cost from token counts
→ compare against limits
→ 80%: api.logger.warn()
→ 100%: exec('moltbot gateway stop')
Documented APIs used:
api.registerService()- background serviceapi.registerCli()- CLI commandsapi.registerGatewayMethod()- RPC endpointapi.logger- loggingmoltbot sessions --json- session datamoltbot gateway stop- halt on breach
- Token data (
inputTokens,outputTokens) must be present inmoltbot sessions --jsonoutput - If missing, plugin logs a warning and spend shows $0
- Pricing config in
models.providersrecommended for accurate costs (falls back to $3/$15 per 1M tokens)
src/
├── plugin.ts # Entry point, registers service/cli/rpc
├── monitor.ts # Background check loop
├── cost.ts # Token → USD calculation
├── types.ts # TypeScript definitions
└── index.ts # Re-exports
MIT