@kapishdima/ai-ledger
v0.0.11
Published
AI provider balance and spending tracker for OpenCode
Maintainers
Readme
ai-ledger
AI provider balance and spending tracker for OpenCode
Track your AI API usage across multiple providers (OpenAI, Anthropic, Kimi, MiniMax) directly in OpenCode with automatic cost deduction and beautiful balance visualization.
Features
- 🔍 Automatic Provider Detection - Detects configured AI providers from environment variables
- 💰 Balance Tracking - Track spending and remaining balances in real-time
- 📊 Visual Progress Bars - Unicode-based progress bars show usage at a glance
- 💾 Local SQLite Storage - All data stored locally in
~/.config/opencode/ai-ledger/wallet.db - 🎯 Automatic Cost Deduction - Automatically deducts costs from AI requests
- 🏦 Easy Top-ups - Simple commands to add funds to any provider
Installation
npm install ai-ledger
# or
bun add ai-ledgerThen add to your OpenCode configuration:
{
"plugins": ["ai-ledger"]
}Usage
Initialize
Detects configured AI providers and shows setup instructions:
/ledger_initExample output:
Found 2 AI provider(s):
/ledger_set openai <amount> - Set OpenAI balance
/ledger_set anthropic <amount> - Set Anthropic balance
Example: /ledger_set openai 50.00Set Initial Balance
Set the initial balance for a provider:
/ledger_set openai 50.00
/ledger_set anthropic 25.00
/ledger_set kimi 100.00Add Funds
Add money to an existing provider balance:
/ledger_add openai 20.00Check Balances
View all provider balances with visual progress bars:
/ledger_balanceExample output:
┌──────────────┬──────────────┬──────────┬────────────────────┐
│ Provider │ Balance │ Spent │ Progress │
├──────────────┼──────────────┼──────────┼────────────────────┤
│ OpenAI │ $42.86 │ $7.14 │ ████░░░░░░░░░░░░░░ │
│ Anthropic │ $25.00 │ $0.00 │ ░░░░░░░░░░░░░░░░░░░░ │
└──────────────┴──────────────┴──────────┴────────────────────┘Automatic Tracking
Once configured, the plugin automatically tracks all AI requests and deducts costs from your balances. You'll see log messages like:
ai-ledger: openai (gpt-4): -$0.0023 | balance: $42.86Supported Providers
The plugin automatically detects and supports:
- OpenAI - Requires
OPENAI_API_KEY - Anthropic - Requires
ANTHROPIC_API_KEY - Kimi (Moonshot) - Requires
MOONSHOT_API_KEY - MiniMax - Requires
MINIMAX_API_KEY
Database
Data is stored locally in SQLite at:
~/.config/opencode/ai-ledger/wallet.dbThe database contains two tables:
wallets- Stores provider balances and metadatatransactions- Stores every AI request with cost details
Requirements
- OpenCode
- Bun runtime (>= 1.0.0)
- Node.js (>= 18.0.0)
- TypeScript (>= 5.0.0)
Configuration
No additional configuration required! The plugin automatically detects your API keys from environment variables.
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For issues and feature requests, please use the GitHub issue tracker.
