Skip to content

[FEATURE] Metrics recorder and infer stats: tool outcomes, tokens, sessions #841

Description

@edenreich

Problem

There is no aggregated visibility into tool-call failures, token consumption, or session activity. The raw signals all exist — domain.ToolExecutionResult{Success, Duration, Error} is constructed in 137 places, per-request usage is captured, PricingService computes cost — but nothing collects them, so questions like "which tools fail most" or "tokens per model this week" are unanswerable.

Design

  • Recorder service in the container, delivered as a decorator around domain.ToolService.ExecuteTool — both execution paths funnel through it (chat internal/agent/agent.go:1021, headless cmd/agent.go:756). Records tool name, duration, outcome class (success / validation error / execution error / approval-rejected / timeout).
  • Token usage: tap the unified accumulator (see the accumulation-unification issue) — model, prompt/completion tokens, cost via the existing PricingService.
  • Lifecycle: session start (container.go:149), agent Start/Stop, headless agentrunner.Run (runner.go:71) — session id, mode, duration, completion state.
  • Sink, local-first: append JSONL under ~/.infer/metrics/ (rotate monthly), or the SQLite backend when configured. Never record prompt/response content — only counts, durations, model and tool names.
  • infer stats [--since 7d]: aggregate view — tool calls by name (count, failure rate, p50/p95 duration), tokens + cost by model, sessions by mode.
  • Config: metrics.enabled (default true, local-only), retention. Zero overhead when disabled (no-op recorder).

Acceptance criteria

  • Mock-gateway integration test: a scenario with a failing tool + the usage-report scenario → infer stats shows 1 failure and 142 tokens
  • infer stats renders cleanly on an empty store
  • Disabled mode adds no allocations on the tool path

Dependencies

Depends on #835 (single usage tap). Unblocks the OTLP exporter and the opt-in telemetry issues.

Activity

  1. added
    enhancement
    on Jul 10, 2026
  2. moved this to Todo in 2026on Jul 10, 2026
  3. moved this from Todo to In progress in 2026on Jul 12, 2026
  4. added a commit that references this issue on Jul 12, 2026
    b6bfb49
  5. moved this from In progress to Done in 2026on Jul 12, 2026
  6. inference-gateway-releaser commented on Jul 12, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.145.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementreleased

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions