Metrics
Anvio's in-process Prometheus-style metrics registry — token usage, cost, and MCP restarts, exposed at GET /api/metrics.
3 min read
Anvio exposes an in-process, Prometheus-text-format metrics registry (packages/observability/src/metrics-registry.ts, MetricsRegistry) at GET /api/metrics. It's deliberately minimal — a counter/gauge map, not a full Prometheus client library — and was shipped in Phase P9–P10 (docs/63-phase-p10-priorities.md).
Endpoint
GET /api/metricsReturns Prometheus exposition-format text:
anvio_tokens_input_total{provider="anthropic",model="claude-sonnet-4-20250514",channel="cli"} 1523
anvio_tokens_output_total{provider="anthropic",model="claude-sonnet-4-20250514",channel="cli"} 812
anvio_tokens_total{provider="anthropic",model="claude-sonnet-4-20250514",channel="cli"} 2335
anvio_token_cost_usd_total{provider="anthropic",model="claude-sonnet-4-20250514",channel="cli"} 0.041
anvio_mcp_restarts_total{server="github"} 2Point a standard Prometheus scrape config at this path if you want to pull metrics into an existing Prometheus/Grafana stack.
What's tracked today
MetricsRegistry implements exactly two primitives:
incrementCounter(name: string, labels: Labels = {}, value = 1): void
setGauge(name: string, labels: Labels, value: number): voidPlus two purpose-built helpers that call incrementCounter under the hood:
recordTokenUsage()
Called on every model completion. Emits four counters, each labeled by provider, model, and channel (defaulting to "unknown" for any missing dimension):
| Counter | Meaning |
|---|---|
anvio_tokens_input_total | Input tokens consumed |
anvio_tokens_output_total | Output tokens generated |
anvio_tokens_total | Input + output |
anvio_token_cost_usd_total | Estimated USD cost (only emitted when estimatedCostUsd is provided) |
recordMcpRestart()
recordMcpRestart(serverId: string): void
// → anvio_mcp_restarts_total{server="<serverId>"}Incremented whenever an MCP server process is restarted (crash recovery, health-check failure) — a useful signal for flaky MCP server integrations.
Global registry lifecycle
export function getMetricsRegistry(): MetricsRegistry // lazily creates a singleton
export function resetMetricsRegistry(): void // clears + drops the singleton (test isolation)The registry lives entirely in process memory — restarting the API or worker process resets all counters to zero. There is no persistence layer for metrics themselves (as opposed to the durable audit ledgers below).
Because the registry is in-memory and per-process, running multiple api/worker replicas (as in a future Kubernetes deployment — see Kubernetes) means GET /api/metrics on any one instance only reflects that instance's local counters. A production Prometheus setup should scrape every replica and let Prometheus itself sum across instance labels, rather than expecting a single aggregated endpoint.
Durable token audit ledger (not the same as the metrics registry)
Beyond the in-memory counters, token usage is separately written to a durable, file-based audit ledger:
workspace/audit/tokens.jsonl{"ts":"2026-06-19T08:00:00Z","pool":"anthropic","credId":"key2","tokens":1523,"status":"ok"}Query it via the CLI instead of the metrics endpoint when you want historical aggregation rather than a live gauge:
anvio usage stats
anvio usage stats --json
anvio usage stats --last 24hThis ledger survives process restarts (it's a file), unlike the in-memory MetricsRegistry counters — use it for cost accounting and GET /api/metrics for live scrape-based dashboards.
MCP health (a related but separate surface)
anvio mcp healthReturns per-server transport, connected state, restart count, and tool count — the human-readable counterpart to the anvio_mcp_restarts_total counter above.
Dashboarding
Anvio ships a ready-made Langfuse dashboard template at configs/observability/langfuse-dashboard.json, with PromQL-style queries assuming these exact metric names:
{
"title": "Agent run latency (p95)",
"query": "histogram_quantile(0.95, sum(rate(anvio_agent_run_duration_bucket[5m])) by (le))"
},
{
"title": "Tool calls by surface",
"query": "sum by (tool_prefix) (rate(anvio_tool_calls_total[5m]))",
"labels": ["anvio_tools", "anvio_mcp", "anvio_channel"]
},
{
"title": "Token usage by agent",
"query": "sum by (agent_name) (increase(anvio_tokens_total[24h]))"
}The dashboard JSON references anvio_agent_run_duration_bucket, anvio_tool_calls_total, and anvio_harness_* metrics that come from OTel span/metric export (see Tracing), not from MetricsRegistry directly. MetricsRegistry's GET /api/metrics endpoint currently only implements the token-usage and MCP-restart counters documented above — treat the dashboard's other panels as targets for the OTel pipeline, not the Prometheus endpoint.
TODO: The dashboard JSON's notes field says "Panel queries assume Prometheus metric names exported from
@anvio/observability" — butanvio_agent_run_duration_bucket,anvio_harness_channel_reply_total,anvio_harness_inbound_total, andanvio_mcp_approval_wait_seconds_bucketwere not found as emitted metrics inpackages/observability/srcat this pass. Confirm whether these are emitted via OTel metrics instrumentation elsewhere before wiring dashboards against them.
Last updated on