metering
Installation
SKILL.md
Metering
Turns agent spend into a measured cost per request, per model and per hour, with a budget alert. It builds on the observability module's stack (Alloy, Loki, Prometheus, Grafana, Alertmanager); without it, run /observability:stack-init first.
Method
Claude Code writes one api_request event per API call, with its cost, model and token counts. Sum those events. Claude Code's cumulative counters are not summed: parallel agent processes write the same series, which then reads as a stream of counter resets, and any sum over it is wrong. Identity attributes (email, account and organisation ids) are deleted in Alloy before storage; the observability template already does this.
Steps
- Send telemetry. Copy
${CLAUDE_PLUGIN_ROOT}/templates/ai-ops/metering/claude-code.env.exampletoai-ops/metering/, set<stack-host>, and put the keys into theenvblock of.claude/settings.json(shared) or each developer's shell. Check before use: the variable names against the Claude Code monitoring documentation. - Check the events arrive. In Grafana Explore, query
{service_name=~"claude-code.*"} | event_name="api_request"after a short agent session. No events: check the endpoint, the port (4317 gRPC) and that Alloy receives OTLP. - Add the dashboard. Copy
templates/observability/grafana/specs/agent-spend.jsonintoobservability/grafana/specs/and runpython3 observability/grafana/build.py. - Add the budget alert. Merge
templates/ai-ops/metering/loki-ruler.ymlintoobservability/loki/loki.yml, mountobservability/loki/rulesat/loki/rules, copytemplates/observability/loki/rules/fake/agent-spend.yml, and replace<hourly budget in USD>with the budget the user sets. Addobservability/loki/rules/**/*.ymlto theglobsofobservability.alert-routesingate/gate.json, so the rule needs a route and a runbook like every other. - Write the cost page. Copy
templates/docs/fin/agent-spend.md, fill the budget table and the owner, and setcost_periodandcost_sourcefor any figure on the page (thefinops.cost-provenancecheck reads them). - Reconcile once a month. Compare the summed events with the provider invoice for the same period. A gap above a few percent means a missing event source (another machine, another tool); find it before trusting the dashboard.