Repository navigation
fix: background utility completions are missing from token and cost metrics - #167986
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. What this changesThis PR includes background utility completions in token and cost metrics while preserving plugin completion accounting. Example: Generate a session title using 101 input tokens and 17 output tokens at a cost of $0.000253.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep open: this PR fixes a current accounting bug, has relevant shipped-entry-point proof, and introduces no actionable defects. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherCore title, recap, observer, narration, and transcript callers send prompts through the isolated completion owner; its validated runtime results now feed the existing diagnostic event bus and metrics exporters. flowchart LR
A[Background utility callers] --> B[Isolated completion owner]
B --> C[Selected CLI or harness]
C --> B
B --> D[Trusted usage diagnostics]
D --> E[Metrics exporters]
B --> F[Completion text]
Technical reviewBest possible solution: Land this focused accounting fix through the normal maintainer workflow. Do we have a high-confidence way to reproduce the issue? Current-main source establishes the missing event path, and supplied before/after Gateway output demonstrates the accounting failure; this read-only review ran no target code. Is this the best way to solve the issue? The shared completion owner is the appropriate boundary: it handles all core callers, reuses existing normalization and pricing, and excludes plugin calls already finalized elsewhere. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against d12658c4c9dd. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +49, Tests +97. Total +146 across 2 files. Added test files: 1. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReviewed October 9, 2026, 9:40 PM ET / October 10, 2026, 01:40 UTC (Revision 3). |
|
Added live gateway before/after proof to the description. @clawsweeper re-review |
|
Independently verified a252cd1 as-is through built Gateways in a secretless Node 24.16.0 Linux container. No source changes or push were needed. Thanks @ruel225 for the earlier #167960 contribution and the admitted-agent pricing correction, and @ericcurtin for carrying the terminal CLI usage distinction here. #167960 was closed by its author; this is the selected canonical repair. Real caller → exporter proofEach Gateway had isolated home/state/profile paths and a non-default loopback port, with diagnostics-prometheus enabled. Scraped the shipped endpoint HTTP utility/title and sibling controls: a local OpenAI-compatible provider returned 101 input + 17 output = 118 tokens per request, priced at $0.000253. Both main
The plugin control registers through the public API and is called over Gateway RPC; the normal-turn control uses the shipped agent CLI. Provider logs confirm exactly one title, one plugin request, one normal-turn request, and two Activity recap requests. No double counting. Terminal CLI regression: the same real The earlier Full build passed. Focused tests passed: 121 tests across 5 files, 241.03 seconds wall time ( @clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Landed in 2c1ecd3. Thanks @ericcurtin for the fix, @ruel225 for the earlier PR and the pricing catch, and @etzelm for the report: background completions now show up in token and cost metrics. |
…etrics (openclaw#167986) runIsolatedCompletion returned usage but never emitted a model.usage diagnostic event, so background utility completions (session titles, activity recaps) were missing from openclaw_model_tokens_total and openclaw_model_cost_usd_total. Isolated completions now emit model.usage once from the terminal usage, with cost estimated for the admitted agent; plugin runtime.llm.complete keeps its existing accounting. Verified through a built Gateway: a claude-cli title counted 118 tokens once, recaps counted, and normal and plugin turns were unchanged. Fixes openclaw#167913. Co-authored-by: Ayaan Zaidi <[email protected]>
Closes #167913
What Problem This Solves
Fixes: session titles, recaps and other background utility completions are missing from token and cost metrics.
User Impact
openclaw_model_tokens_totalandopenclaw_model_cost_usd_totalnow count these calls.Why This Change Was Made
runIsolatedCompletionnow emitsmodel.usageonce. Plugin completions are skipped, since they already emit it. CLI calls use the terminal turn usage, like other emitters.Evidence
isolated-completion.usage.test.ts(CLI, harness, terminal usage, no usage, plugin skip): the emit cases fail before the fix and pass after.pnpm test src/agents/isolated-completion.usage.test.ts --maxWorkers=1, about 40 to 55s.pnpm test src/agents/isolated-completionand the plugin runtime and Prometheus tests pass.pnpm tsgo:coreand the agents, gateway and plugintsgo:core:testshards pass.diagnostics-prometheuson, mock OpenAI-compatible provider (120 input, 7 output tokens), onesessions.title.preparecall, thenGET /api/diagnostics/prometheus: