Repository navigation
feat(ai): LLM token-usage metrics for ai/chat & ai/agent (ADR-0029 Phase A) - #93
Merged
Merged
Conversation
…ase A)
Surface per-call token usage as Prometheus counters so cost is aggregatable
across runs (previously usage was only in each node's output):
- fuse_llm_tokens_total{function,provider,model,type=prompt|completion}
- fuse_llm_calls_total{function,provider,model,status}
A narrow ai.UsageRecorder port keeps the ai package free of the prometheus
dependency; a metrics-backed adapter is injected via packages.NewInternal.
ai/chat records once per completion; ai/agent records per reasoning iteration.
Budget enforcement (Option B) and external metering (Option C) remain deferred.
ADR-0029 Proposed -> Accepted (Phase A shipped).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This was referenced Jun 4, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First of the "finish the agents" leaf PRs (quick wins). ADR-0029 Phase A — usage visibility.
What
Surface per-call LLM token usage as Prometheus counters, so cost is aggregatable across runs
(previously usage was only embedded in each node's
usageoutput):fuse_llm_tokens_total{function,provider,model,type=prompt|completion}fuse_llm_calls_total{function,provider,model,status}How
ai.UsageRecorderport (internal/packages/functions/ai/usage.go) keeps theaipackagefree of the prometheus dependency; a metrics-backed adapter
(
internal/packages/usage_recorder.go) is injected throughpackages.NewInternal(fx provides*FuseMetrics).ai/chatrecords once per completion;ai/agentrecords per reasoning iteration. Emitted fromthe async goroutine (node span already ended — fine for counters).
Scope
Budget enforcement (Option B, needs a pricing table) and external metering (Option C) remain
deferred. ADR-0029
Proposed → Accepted(Phase A).Verify
make lint0 ·make buildok ·make test701 pass. End-to-end:curl localhost:9090/metrics | grep fuse_llm_after running an ai node.Next leaf PRs: 0028 (context-assembly policy) then 0030 (structured output).
🤖 Generated with Claude Code