Agent Lens — 代理 Lens
v2.0.0追踪 AI 代理 API calls, analyze 令牌 usage, and 优化 costs. Use when user wants to 监控 LLM spending, 调试 API calls, 追踪 令牌 consumption, or...
运行时依赖
安装命令
点击复制本土化适配说明
Agent Lens — 代理 Lens 安装说明: 安装命令:["openclaw skills install agent-lens"]
技能文档
代理 Lens
追踪 every AI API call, analyze 令牌 usage, and 优化 costs.
When to Use
Activate this 技能 when the user:
Says "how much am I spending", "令牌 usage", "API costs" Wants to know which 模型 is most expensive Needs to 优化 prompt costs Wants to 追踪 API call latency or error rates Mentions "bud获取", "cost optimization", or "令牌 counting" Asks "why is my API bill so high" Quick 启动 # 安装 pip 安装 git+https://github.com/lrg913427-dot/代理-lens.git
# 生成 demo data and see it in action 代理-lens demo
# View stats 代理-lens stats 代理-lens cost 代理-lens recent
Three Ways to 追踪
- Decorator (easiest)
lens = 代理Lens(代理_name="my-代理")
@lens.追踪(模型="gpt-4o") def call_API(prompt): return 命令行工具ent.chat.completions.创建( 模型="gpt-4o", messages=[{"角色": "user", "content": prompt}], )
# 令牌 usage is auto-提取ed from OpenAI-style 响应s 结果 = call_API("Hello")
- 上下文 管理器 (flexible)
lens = 代理Lens(代理_name="my-代理")
with lens.追踪(模型="claude-3.5-sonnet") as t: 结果 = 命令行工具ent.chat.completions.创建(...) t.输入_令牌s = 结果.usage.prompt_令牌s t.输出_令牌s = 结果.usage.completion_令牌s
- Direct Record (manual)
lens = 代理Lens(代理_name="my-代理") lens.record( 模型="gpt-4o", 输入_令牌s=1500, 输出_令牌s=800, latency_ms=2300, )
Global Shortcuts from 代理_lens 导入 record, 追踪, 追踪
record(模型="gpt-4o", 输入_令牌s=100, 输出_令牌s=50)
with 追踪(模型="gpt-4o") as t: ...
@追踪(模型="gpt-4o") def my_func(): ...
命令行工具 Commands Command Description 代理-lens stats Overview: total calls, 令牌s, cost 代理-lens 报告 --by 模型 Breakdown by 模型/提供者/代理 代理-lens cost Cost ranking with percentage bars 代理-lens recent -n 10 Latest API calls 代理-lens top Most expensive calls 代理-lens 导出 --json 导出 to JSON 代理-lens 导出 -o data.csv 导出 to CSV 代理-lens 清理 --before 清理 old data 代理-lens demo 生成 sample data Cost Optimization 工作流
When user asks "how can I save money":
运行 cost 报告: 代理-lens cost Identify expensive 模型s: Which 模型s cost the most? 检查 令牌 efficiency: Are prompts too long? Suggest cheaper alternatives: gpt-4o → gpt-4o-mini (10x cheaper) claude-3.5-sonnet → claude-3.5-hAIku (4x cheaper) gpt-4 → gpt-4o (2x cheaper) 检查 caching: Are there repeated prompts? 检查 error rate: 代理-lens 报告 --by 状态 令牌 Counting 导入 tik令牌
def count_令牌s(text: str, 模型: str = "gpt-4o") -> int: """Count 令牌s for a given 模型.""" try: enc = tik令牌.encoding_for_模型(模型) except KeyError: enc = tik令牌.获取_encoding("cl100k_base") return len(enc.encode(text))
# 检查 before 发送ing prompt = "Your long prompt here..." 令牌s = count_令牌s(prompt) print(f"Prompt: {令牌s} 令牌s") print(f"Estimated cost: ${令牌s * 2.50 / 1_000_000:.4f}")
Supported 模型s
Pricing data for: OpenAI (GPT-4o, o1, o3), Anthropic (Claude 3.5/4), Google (Gemini 2.x), DeepSeek, Mistral, Qwen, GLM, MiMo.
Unknown 模型s are 追踪ed but cost shows "—".
Integration with Hermes # 追踪 Hermes 代理 API calls from 代理_lens 导入 代理Lens
lens = 代理Lens(代理_name="hermes-mAIn")
# In your 代理 loop: with lens.追踪(模型=config.模型) as t: 响应 = 代理.运行_conversation(message) t.输入_令牌s = 响应.获取("输入_令牌s", 0) t.输出_令牌s = 响应.获取("输出_令牌s", 0)
Data Storage
SQLite at ~/.代理-lens/追踪s.db. Fully local, no cloud 服务 needed.
Pitfalls 令牌 提取ion auto-works only for OpenAI-compatible 响应 格式化 For non-OpenAI 提供者s, manually 设置 t.输入_令牌s and t.输出_令牌s Cost estimates use 列出 prices; actual costs may differ with discounts Database grows over time; use 代理-lens 清理 periodically Verification 代理-lens demo # 生成 20 sample records 代理-lens stats # Should show 20 calls 代理-lens cost # Should show cost breakdown by 模型