← Back to Plugins
Integration

Moltbot Spend Guard

meridianix By meridianix ⭐ 1 stars 👁 28 views ▲ 0 votes

Spend protection plugin for Moltbot. Prevents runaway API costs

GitHub

Configuration Example

{
  "plugins": {
    "entries": {
      "spend-guard": {
        "enabled": true,
        "config": {
          "dailyLimitUsd": 50,
          "hourlyLimitUsd": 10
        }
      }
    }
  }
}

README

# Moltbot Spend Guard

Spend protection plugin for Moltbot. Monitors API costs and stops the gateway when limits are breached.

## Problem

Moltbot users wake up to $120+ API bills from runaway loops. No native spend limits exist.

## Solution

A plugin that:
1. Runs a background service checking spend every minute
2. Gets session data via `moltbot sessions --json`
3. Calculates cost using `models.providers` pricing config
4. Alerts at 80% of limit, stops gateway at 100%

## Install

```bash
moltbot plugins install github:meridianix/moltbot-spend-guard
```

## Config

Add to `~/.clawdbot/moltbot.json`:

```json
{
  "plugins": {
    "entries": {
      "spend-guard": {
        "enabled": true,
        "config": {
          "dailyLimitUsd": 50,
          "hourlyLimitUsd": 10
        }
      }
    }
  }
}
```

Restart gateway for changes to take effect.

## Options

| 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 % |

## Commands

```bash
moltbot spend-guard status   # Show current spend
moltbot spend-guard check    # Force immediate check
moltbot spend-guard reset    # Clear alert state
```

## RPC

```bash
moltbot gateway call spend-guard.status --params '{}'
```

## How It Works

```
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 service
- `api.registerCli()` - CLI commands
- `api.registerGatewayMethod()` - RPC endpoint
- `api.logger` - logging
- `moltbot sessions --json` - session data
- `moltbot gateway stop` - halt on breach

## Requirements

- Token data (`inputTokens`, `outputTokens`) must be present in `moltbot sessions --json` output
- If missing, plugin logs a warning and spend shows $0
- Pricing config in `models.providers` recommended for accurate costs (falls back to $3/$15 per 1M tokens)

## Architecture

```
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
```

## License

MIT
integration

Comments

Sign in to leave a comment

Loading comments...