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 check

From Leeroopedia
Knowledge Sources
Domains Cost_Monitoring, Budget_Enforcement, Analytics
Last Updated 2026-10-29 00:00 GMT

Overview

Concrete tool for checking current API spending against budget limits via Langfuse trace data, with per-model cost breakdown and budget-exceeded warnings.

Description

The cost_check handler (handle_check) queries the Langfuse traces API for costs within a time period (today/hour/week), compares against persisted budget limits, and returns a detailed breakdown with warnings.

Key internal components:

  • _query_langfuse_costs(from_timestamp) — queries /api/public/traces?fromTimestamp=... with Basic auth, aggregating per-model costs from trace.usage.totalCost, input/output tokens, and call counts.
  • _load_budget() / _save_budget(budget) — read/write budget config from ~/.hermes/cost-budget.json (defaults: daily $1.00, per-task $0.25).
  • _check_langfuse_config() — returns an error dict if any of LANGFUSE_HOST, LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY env vars are missing.

Budget enforcement: the handler computes pct_used = total_cost / daily_budget × 100 and adds warnings at ≥80% ("Approaching budget limit") and ≥100% ("BUDGET EXCEEDED! Use only free/local models").

Usage

Call this tool before starting expensive operations to check remaining budget. Registered as cost_check on the evey_cost_guard toolset.

Code Reference

Source Location

  • Repository: hermes-plugins
  • File: evey-cost-guard/__init__.py
  • Lines: L28-229 (CHECK_SCHEMA L28-46, _check_langfuse_config L77-95, _get_auth_header L98-101, _load_budget L104-114, _save_budget L117-120, _query_langfuse_costs L123-172, handle_check L175-229)

Signature

def handle_check(args: dict, **kwargs) -> str:
    """
    Args:
        args: {period: str - 'today' (default), 'hour', or 'week'}
    Returns:
        JSON: {period, total_cost, daily_budget, remaining, percent_used, traces,
               tokens: {input, output}, models: {name: {calls, cost, input_tokens, output_tokens}}, warning?}
    """

def _query_langfuse_costs(from_timestamp: str) -> dict:
    """Query Langfuse for traces since timestamp. Returns cost breakdown dict."""

def _load_budget() -> dict:
    """Load budget config from disk (defaults: daily $1.00, task $0.25)."""

def _save_budget(budget: dict) -> None:
    """Save budget config to disk."""

Import

from evey_cost_guard import handle_check
# Or accessed via Hermes tool call: cost_check

I/O Contract

Inputs

Name Type Required Description
period str No "today" (default), "hour", or "week"
(env) various — LANGFUSE_HOST, LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY
(file) dict — cost-budget.json with daily_budget and task_budget

Outputs

Name Type Description
period str Time period checked
total_cost str Dollar amount spent (e.g. "$0.4500")
daily_budget str Configured daily limit (e.g. "$1.00")
remaining str Remaining budget (e.g. "$0.5500")
percent_used str Percentage used (e.g. "45.0%")
traces int Number of traces analyzed
tokens dict {input: int, output: int}
models dict Per-model: {calls, cost, input_tokens, output_tokens}
warning str Present at ≥80% or ≥100% budget usage

Usage Examples

Check Today's Spending

result = tool_call("cost_check", {"period": "today"})
# {"period":"today","total_cost":"$0.4500","daily_budget":"$1.00",
#  "remaining":"$0.5500","percent_used":"45.0%","traces":42,
#  "tokens":{"input":12500,"output":8200},
#  "models":{"claude-sonnet-4-20250514":{"calls":15,"cost":0.35,"input_tokens":8000,"output_tokens":5000},
#            "local-model":{"calls":27,"cost":0.10,"input_tokens":4500,"output_tokens":3200}}}

Budget Warning at 80%

result = tool_call("cost_check", {})
# {"total_cost":"$0.8500","daily_budget":"$1.00","percent_used":"85.0%",
#  "warning":"Approaching budget limit (80%+). Switch to free models for remaining tasks."}

Budget Exceeded

result = tool_call("cost_check", {})
# {"total_cost":"$1.2000","daily_budget":"$1.00","percent_used":"120.0%",
#  "warning":"BUDGET EXCEEDED! You've spent more than your daily limit. Use only free/local models until tomorrow. Consider pausing non-essential delegations."}

Related Pages

Implements Principle

Requires Environment

Page Connections

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