Agent Usage Tracker
Use this skill when you need local token accounting for AI agents.
What it provides
- - Real-time token usage persistence in SQLite
- Cost calculation based on per-model pricing
- Budget thresholds with persisted warning and blocking states
- Usage statistics grouped by time window, session, or model
Files to use
- -
src/UsageTracker.ts: ingestion, storage, and reporting - INLINECODE1 : pricing catalog and cost math
- INLINECODE2 : budget policy evaluation
- INLINECODE3 : end-to-end usage example
- INLINECODE4 : reference behavior for tracking, budgeting, and aggregation
Recommended workflow
- 1. Instantiate
UsageTracker with a SQLite path or :memory: for tests. - Register model pricing with
CostCalculator. - Record each agent interaction with prompt tokens, completion tokens, session id, and timestamp.
- Save reusable budgets with
BudgetManager.setBudget, then call evaluateBudget before or after new work to warn or stop when a limit is exceeded. - Use
UsageTracker.getUsageSummary or getTimeSeries for reporting.
Integration notes
- - Store token counts at the moment the provider returns usage metadata.
- Keep model ids normalized. Pricing lookup is exact by model id.
- Use
metadata for provider-specific fields such as request id or tool name. - For sliding-window budgets, query usage by
startTime and endTime before dispatching new work.
Output expectations
This skill ships as a local Node.js package with tests and examples. Extend CostCalculator if your provider pricing changes.
Agent 使用追踪器
当您需要对AI代理进行本地令牌核算时,请使用此技能。
提供的功能
- - SQLite中的实时令牌使用持久化存储
- 基于每模型定价的成本计算
- 具有持久化警告和阻止状态的预算阈值
- 按时间窗口、会话或模型分组的用量统计
使用的文件
- - src/UsageTracker.ts:数据采集、存储和报告
- src/CostCalculator.ts:定价目录和成本计算
- src/BudgetManager.ts:预算策略评估
- examples/basic-usage.ts:端到端使用示例
- tests/:追踪、预算和聚合的参考行为
推荐工作流程
- 1. 使用SQLite路径或:memory:(用于测试)实例化UsageTracker。
- 使用CostCalculator注册模型定价。
- 记录每次代理交互的提示令牌、完成令牌、会话ID和时间戳。
- 使用BudgetManager.setBudget保存可复用的预算,然后在新工作开始前或结束后调用evaluateBudget,当超出限制时发出警告或停止。
- 使用UsageTracker.getUsageSummary或getTimeSeries进行报告。
集成说明
- - 在提供商返回使用元数据时立即存储令牌计数。
- 保持模型ID规范化。定价查找通过模型ID精确匹配。
- 使用metadata存储提供商特定字段,如请求ID或工具名称。
- 对于滑动窗口预算,在分派新工作前通过startTime和endTime查询用量。
输出预期
此技能作为带有测试和示例的本地Node.js包发布。如果您的提供商定价发生变化,请扩展CostCalculator。