Skip to main content
Glama
temurkhan13

openclaw-cost-tracker-mcp

by temurkhan13

top_cost_drivers

Identify the top-spending agents and models in a configurable time window to reveal where your money is going.

Instructions

Highest-spend agents + models in the window — flat list, no per-provider or anomaly noise. Useful for 'where's our money going' digest format.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
window_hoursNo
top_nNo

Implementation Reference

  • Main handler function `build_top_drivers` that composes the `TopCostDriversReport` by computing total cost, top agents (via `_by_agent`), and top models (via `_by_model`).
    def build_top_drivers(
        entries: list[CostEntry],
        window_hours: int,
        top_n: int = 5,
    ) -> TopCostDriversReport:
        """Compose the `top_cost_drivers` response."""
        return TopCostDriversReport(
            window_hours=window_hours,
            total_cost_usd=sum(e.cost_usd for e in entries),
            top_agents=_by_agent(entries, top_n=top_n),
            top_models=_by_model(entries, top_n=top_n),
        )
  • Helper `_by_agent` aggregates cost entries by agent_id, sorting by total cost and returning top_n AgentCost records with share of total.
    def _by_agent(entries: list[CostEntry], top_n: int | None = None) -> list[AgentCost]:
        if not entries:
            return []
        total = sum(e.cost_usd for e in entries) or 1.0
        grouped: dict[str, list[CostEntry]] = defaultdict(list)
        for e in entries:
            agent = e.agent_id or "<unattributed>"
            grouped[agent].append(e)
        out: list[AgentCost] = []
        for agent, group in grouped.items():
            cost = sum(e.cost_usd for e in group)
            # Most-common provider/model in the group
            provider_counts = Counter(e.provider for e in group)
            model_counts = Counter((e.provider, e.model) for e in group)
            primary_provider = provider_counts.most_common(1)[0][0] if provider_counts else None
            primary_model = model_counts.most_common(1)[0][0][1] if model_counts else None
            out.append(
                AgentCost(
                    agent_id=agent,
                    total_cost_usd=cost,
                    request_count=len(group),
                    total_tokens=sum(e.total_tokens for e in group),
                    avg_cost_per_request_usd=cost / len(group),
                    primary_provider=primary_provider,
                    primary_model=primary_model,
                    share_of_total_pct=100.0 * cost / total,
                )
            )
        out.sort(key=lambda a: a.total_cost_usd, reverse=True)
        if top_n is not None:
            out = out[:top_n]
        return out
  • Helper `_by_model` aggregates cost entries by (provider, model) pair, sorting by total cost and returning top_n ModelCost records.
    def _by_model(entries: list[CostEntry], top_n: int | None = None) -> list[ModelCost]:
        if not entries:
            return []
        grouped: dict[tuple[Provider, str], list[CostEntry]] = defaultdict(list)
        for e in entries:
            grouped[(e.provider, e.model)].append(e)
        out: list[ModelCost] = []
        for (provider, model), group in grouped.items():
            cost = sum(e.cost_usd for e in group)
            out.append(
                ModelCost(
                    provider=provider,
                    model=model,
                    total_cost_usd=cost,
                    request_count=len(group),
                    avg_cost_per_request_usd=cost / len(group),
                    avg_tokens_per_request=sum(e.total_tokens for e in group) / len(group),
                )
            )
        out.sort(key=lambda m: m.total_cost_usd, reverse=True)
        if top_n is not None:
            out = out[:top_n]
        return out
  • Pydantic model `TopCostDriversReport` with window_hours, total_cost_usd, top_agents (list of AgentCost), and top_models (list of ModelCost).
    class TopCostDriversReport(BaseModel):
        """Response for `top_cost_drivers`."""
    
        window_hours: int
        total_cost_usd: float
        top_agents: list[AgentCost]
        top_models: list[ModelCost]
  • Tool registration with name 'top_cost_drivers', description, and inputSchema (window_hours, top_n).
    Tool(
        name="top_cost_drivers",
        description=(
            "Highest-spend agents + models in the window — flat list, no per-provider "
            "or anomaly noise. Useful for 'where's our money going' digest format."
        ),
        inputSchema={
            "type": "object",
            "properties": {
                "window_hours": {"type": "integer", "default": 168},
                "top_n": {"type": "integer", "default": 5},
            },
            "required": [],
        },
    ),
  • Dispatch logic in `call_tool` that reads arguments, calls `_entries_in_window` and delegates to `build_top_drivers`.
    if name == "top_cost_drivers":
        window_hours = max(1, min(int(arguments.get("window_hours", 168)), 720))
        top_n = max(1, min(int(arguments.get("top_n", 5)), 50))
        entries = await _entries_in_window(window_hours)
        return _serialize(build_top_drivers(entries, window_hours, top_n=top_n))
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations provided, so description carries full burden. It discloses that results are a flat list without per-provider or anomaly noise, which is sufficient for a simple query tool. Lacks mention of data freshness or side effects, but none expected.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

One concise sentence plus a usage suggestion; no filler words. Front-loaded with key information.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given low complexity (2 optional params, no output schema), description adequately covers purpose, format, and use case. Could add details on output fields or ordering, but not critical for a digest.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 0%, so description must compensate. It does not explicitly explain window_hours or top_n parameters, only implying a time window and top N via context. Defaults are in schema but not described.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Description explicitly states it identifies 'highest-spend agents + models' and contrasts with siblings by saying 'flat list, no per-provider or anomaly noise,' making purpose and differentiation clear.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Explicitly suggests use case: 'where's our money going' digest format, and implicitly advises against using when per-provider breakdowns or anomalies are needed, referencing sibling tools.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/temurkhan13/openclaw-cost-tracker-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server