AgentLedger - AI Agent Expense Tracking
Overview
AgentLedger is an accounting skill that logs every transaction you make, tracks spending across multiple accounts, and provides detailed reports. Use this to maintain a complete financial record of your purchases.
Important: Always log transactions immediately after making purchases. This skill is designed for AI agents to track their own expenses (API costs, tool subscriptions, etc.).
Core Functions
1. Log a Transaction
When you make any purchase, immediately log it:
CODEBLOCK0
CLI Usage (supports both positional and named parameters):
CODEBLOCK1
2. Check Current Spending
CODEBLOCK2
3. Generate Reports
CODEBLOCK3
4. Budget Management
CODEBLOCK4
Categories
Use these predefined categories for consistent tracking:
- - API/Services - API credits, SaaS subscriptions
- Infrastructure - Hosting, domains, CDN
- Marketing - Ads, social media tools
- Tools - Software licenses, utilities
- Subscriptions - Recurring monthly/yearly services
- Other - Miscellaneous expenses
Account Integration
Privacy.com Cards
The ledger automatically detects Privacy.com card data if available:
CODEBLOCK5
Manual Account Setup
CODEBLOCK6
Natural Language Queries
Ask questions like:
- - "How much did I spend on API keys this month?"
- "What was that $20 charge from yesterday?"
- "Show me all infrastructure costs from last quarter"
- "Am I over budget on marketing spend?"
The CLI handles these queries:
CODEBLOCK7
Time Periods
Supported natural language time periods:
- -
today, INLINECODE1 - INLINECODE2 , INLINECODE3
- INLINECODE4 , INLINECODE5
- INLINECODE6 , INLINECODE7
- INLINECODE8 , INLINECODE9
- INLINECODE10 , INLINECODE11
Data Export
CODEBLOCK8
CLI Quick Reference
Essential Commands for AI Agents
CODEBLOCK9
File Storage
- - Transactions: INLINECODE12
- Accounts: INLINECODE13
- Budgets: INLINECODE14
- Settings: INLINECODE15
Best Practices
- 1. Log immediately - Don't wait, log every purchase as it happens
- Add context - Explain why the purchase was necessary
- Use consistent categories - Stick to the predefined categories
- Include receipts - Store confirmation numbers and receipt URLs
- Set budgets - Establish spending limits for each category
- Review regularly - Generate monthly reports to track spending patterns
Error Handling & Edge Cases
The ledger handles common errors gracefully:
Input Validation
- - Negative amounts: Rejected (use positive amounts only)
- Missing required fields: Clear error messages with usage examples
- Invalid currency: Accepted (no validation - assumes user knows what they're doing)
- Very long descriptions: Handled without truncation
Data Safety
- - Automatic backups: Created before each save operation
- Corrupted data recovery: Automatic recovery from
.backup files - Empty periods: Gracefully shows $0.00 totals
- Multi-currency: Properly separated in summaries and reports
Example Error Recovery
CODEBLOCK10
Security & Privacy
- - Local storage only: All data stays in
workspace/ledger/ JSON files - No external API calls: Core functionality works offline
- No sensitive data: Never store actual card numbers or passwords
- Account aliases: Use descriptive IDs like
privacy-card-1 or INLINECODE19 - Receipt URLs: Store links to receipts, not receipt content itself
AgentLedger - AI代理费用追踪
概述
AgentLedger是一个会计技能,记录您进行的每笔交易,追踪多个账户的支出,并提供详细报告。使用此技能维护完整的购买财务记录。
重要提示:购买后立即记录交易。此技能专为AI代理追踪自身费用(API成本、工具订阅等)而设计。
核心功能
1. 记录交易
进行任何购买时,立即记录:
javascript
// 在Node.js环境中,引入账本
const AgentLedger = require(./src/ledger.js);
const ledger = new AgentLedger();
// 记录交易
await ledger.logTransaction({
amount: 29.99,
currency: USD,
vendor: OpenAI,
description: GPT-4 API积分,
category: API/服务,
account: privacy-card-1,
context: 需要额外令牌用于用户项目分析,
receiptUrl: https://platform.openai.com/receipts/xyz,
confirmationId: sub_1234567890
});
CLI使用(支持位置参数和命名参数):
bash
位置参数风格
node src/cli.js log 29.99 OpenAI GPT-4 API积分 --category=API/服务
命名参数风格
node src/cli.js log --amount=29.99 --vendor=OpenAI --description=GPT-4 API积分 --category=API/服务 --context=月度API充值
2. 查看当前支出
javascript
// 获取支出摘要
const summary = await ledger.getSummary(this-month);
console.log(本月总支出:$${summary.total});
// 查看特定类别
const apiSpending = await ledger.getCategorySpending(API/服务, this-month);
3. 生成报告
javascript
// 月度报告
const report = await ledger.generateReport(monthly, { month: 2024-01 });
// 自定义日期范围
const customReport = await ledger.generateReport(custom, {
startDate: 2024-01-01,
endDate: 2024-01-31
});
4. 预算管理
javascript
// 设置API服务的月度预算
await ledger.setBudget(API/服务, 500, monthly);
// 检查预算状态
const budgetStatus = await ledger.checkBudget(API/服务);
if (budgetStatus.isNearLimit) {
console.log(警告:已使用API预算的${budgetStatus.percentUsed}%);
}
类别
使用以下预定义类别进行一致追踪:
- - API/服务 - API积分、SaaS订阅
- 基础设施 - 托管、域名、CDN
- 营销 - 广告、社交媒体工具
- 工具 - 软件许可、实用工具
- 订阅 - 月度/年度循环服务
- 其他 - 杂项支出
账户集成
Privacy.com卡片
账本会自动检测可用的Privacy.com卡片数据:
javascript
// 如果您在workspace/privacy/中有Privacy.com JSON导出文件
await ledger.importPrivacyTransactions(./privacy/card-1.json);
手动账户设置
javascript
// 注册新的支付方式
await ledger.addAccount({
id: stripe-main,
name: 主Stripe账户,
type: credit_card,
currency: USD
});
自然语言查询
提出如下问题:
- - 我这个月在API密钥上花了多少钱?
- 昨天那笔20美元的扣款是什么?
- 显示上个季度所有基础设施成本
- 我的营销支出超预算了吗?
CLI处理这些查询:
bash
node src/cli.js query 本月API支出
node src/cli.js find OpenAI --last-week
时间段
支持的自然语言时间段:
- - 今天、昨天
- 本周、上周
- 本月、上月
- 本季度、上季度
- 今年、去年
- 最近30天、最近90天
数据导出
javascript
// 导出为CSV
await ledger.exportTransactions(csv, ./exports/transactions.csv);
// 导出为JSON
await ledger.exportTransactions(json, ./exports/transactions.json);
CLI快速参考
AI代理基本命令
bash
初始化(运行一次)
node src/cli.js init
记录交易(支持两种风格)
node src/cli.js log 29.99 OpenAI API积分 --category=API/服务
node src/cli.js log --amount=29.99 --vendor=OpenAI --description=API积分 --category=API/服务
查看当前支出
node src/cli.js summary # 本月
node src/cli.js summary --period=today # 仅今天
node src/cli.js summary --period=this-week # 本周
设置和检查预算
node src/cli.js budget set API/服务 500 # 设置月度预算
node src/cli.js budget status # 检查所有预算
生成详细报告
node src/cli.js report monthly
node src/cli.js report --type=category
node src/cli.js report --type=vendor
搜索交易
node src/cli.js find OpenAI # 按供应商搜索
node src/cli.js find API --category=API/服务 # 按类别搜索
node src/cli.js find --min-amount=50 # 查找大额支出
导出数据
node src/cli.js export csv # 导出为CSV
node src/cli.js export --format=json # 导出为JSON
自然语言查询
node src/cli.js query 我这个月在API上花了多少钱?
node src/cli.js query 那笔25美元的扣款是什么?
从Privacy.com导入
node src/cli.js import privacy ./privacy-export.json
文件存储
- - 交易记录:workspace/ledger/transactions.json
- 账户信息:workspace/ledger/accounts.json
- 预算设置:workspace/ledger/budgets.json
- 配置设置:workspace/ledger/settings.json
最佳实践
- 1. 立即记录 - 不要等待,每次购买后立即记录
- 添加上下文 - 解释购买的必要性
- 使用一致的类别 - 坚持使用预定义类别
- 包含收据 - 保存确认号和收据URL
- 设置预算 - 为每个类别设定支出限额
- 定期审查 - 生成月度报告以追踪支出模式
错误处理与边界情况
账本能优雅地处理常见错误:
输入验证
- - 负数金额:拒绝(仅使用正数金额)
- 缺少必填字段:清晰的错误信息及使用示例
- 无效货币:接受(无验证 - 假设用户知道自己在做什么)
- 过长描述:不截断处理
数据安全
- - 自动备份:每次保存操作前创建
- 损坏数据恢复:从.backup文件自动恢复
- 空时间段:优雅显示$0.00总额
- 多币种:在摘要和报告中正确分离
错误恢复示例
bash
如果看到无法加载交易记录消息:
系统会自动尝试从备份恢复
您的数据应自动恢复
手动检查备份
ls workspace/ledger/*.backup # 检查备份是否存在
安全与隐私
- - 仅本地存储:所有数据保存在workspace/ledger/JSON文件中
- 无外部API调用:核心功能离线工作
- 无敏感数据:绝不存储实际卡号或密码
- 账户别名:使用描述性ID,如privacy-card-1或company-amex
- 收据URL:存储收据链接,而非收据内容本身