Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:42 evey Hermes plugins cost analytics

From Leeroopedia
Knowledge Sources
Domains Cost_Analytics, Model_Optimization, Recommendations
Last Updated 2026-10-29 00:00 GMT

Overview

Concrete tool for detailed per-model cost analytics from Langfuse generation observations, with token ratios, top-consumer identification, and cost-saving recommendations.

Description

The cost_analytics handler (handle_analytics) queries the Langfuse observations API (/api/public/observations?type=GENERATION) for generation-level cost data, providing deeper analysis than the trace-level cost_check.

Key internal components:

  • _compute_ratio(input_tokens, output_tokens) — formats the input:output token ratio (e.g. "3:1" or "1:2"). Returns "N/A" if both zero.
  • _format_tokens(n) — human-readable token count (e.g. "1.2K", "3.5M").
  • LOCAL_MODELS — a set of known local model names (qwen35-4b, qwen35-9b, hermes3:8b, dolphin3:8b, dolphin-mistral:7b) used for recommendations.

The recommendation engine identifies the most expensive non-local model and suggests switching to qwen35-4b for simple tasks (avg input <2000 tokens) or reviewing whether all calls need that model's capability.

Usage

Call this tool for deep dives into spending patterns and model optimization recommendations. Registered as cost_analytics on the evey_cost_guard toolset.

Code Reference

Source Location

  • Repository: hermes-plugins
  • File: evey-cost-guard/__init__.py
  • Lines: L265-392 (LOCAL_MODELS L265, ANALYTICS_SCHEMA L267-289, _format_tokens L292-297, _compute_ratio L300-304, handle_analytics L307-392)

Signature

def handle_analytics(args: dict, **kwargs) -> str:
    """
    Args:
        args: {
            period: str - 'today', '24h' (default), or 'week',
            limit: number - max observations (default 100, max 500)
        }
    Returns:
        JSON: {period, observations_analyzed, total_cost, total_input_tokens,
               total_output_tokens, input_output_ratio,
               models: list[{name, calls, input, output, cost, avg_input, io_ratio}],
               recommendation: str}
    """

def _compute_ratio(input_tokens: int, output_tokens: int) -> str:
    """Format input:output token ratio."""

def _format_tokens(n: int) -> str:
    """Human-readable token count (K/M)."""

Import

from evey_cost_guard import handle_analytics
# Or accessed via Hermes tool call: cost_analytics

I/O Contract

Inputs

Name Type Required Description
period str No "today", "24h" (default), or "week"
limit number No Max observations (default 100, max 500)

Outputs

Name Type Description
period str Time period analyzed
observations_analyzed int Number of observations processed
total_cost float Total cost across all models
total_input_tokens int Sum of input tokens
total_output_tokens int Sum of output tokens
input_output_ratio str Overall token ratio
models list[dict] Per-model: {name, calls, input, output, cost, avg_input, io_ratio}
recommendation str Cost-saving recommendation

Usage Examples

24h Analytics

result = tool_call("cost_analytics", {"period": "24h", "limit": 200})
# {"period":"24h","observations_analyzed":85,"total_cost":0.72,
#  "total_input_tokens":45000,"total_output_tokens":15000,
#  "input_output_ratio":"3:1",
#  "models":[
#    {"name":"claude-sonnet-4-20250514","calls":20,"input":30000,"output":10000,
#     "cost":0.55,"avg_input":1500,"io_ratio":"3:1"},
#    {"name":"local-model","calls":65,"input":15000,"output":5000,
#     "cost":0.17,"avg_input":230,"io_ratio":"3:1"}],
#  "recommendation":"claude-sonnet-4-20250514 has small prompts (avg 1.5K input). Consider using qwen35-4b for simple tasks to save costs."}

Weekly Deep Dive

result = tool_call("cost_analytics", {"period": "week", "limit": 500})
# Returns up to 500 observations from the past week with per-model breakdown

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment