Repository navigation
[EPIC-D][LOU-D48] OpenTelemetry GenAI metrics and a loushy.cost_usd span attribute - #95
Merged
Merged
Conversation
…pan attribute createOtelTraceExporter() now records gen_ai.client.token.usage and gen_ai.client.operation.duration through @opentelemetry/api's metrics API (global MeterProvider, or the new `meter` option; `metrics: false` disables). Spans gain loushy.cost_usd (per step on chat spans, cumulative on parents via a withSpan() rollup, absent when the model is unpriced) and loushy.usage.estimated on run spans. Adds @opentelemetry/sdk-metrics as a devDependency for tests and the example. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016bY29ibQgv3MDdjAbUT4YR
…cost # Conflicts: # CHANGELOG.md
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.
What
createOtelTraceExporter({ meter?, metrics? })(@loushy/build-ai-agent/otel) now also records the OTel GenAI client metrics through@opentelemetry/api's metrics API (metrics.getMeter(...)by default, or themeteroption;metrics: falseturns them off):gen_ai.client.token.usage(histogram,{token}): one record pergen_ai.token.type(input/output) per model call, attributesgen_ai.operation.name,gen_ai.provider.name,gen_ai.request.model,gen_ai.response.model(when known).gen_ai.client.operation.duration(histogram,s): every model call (chat) and tool call (execute_tool; the spec lists it as a valid operation), pluserror.typeon failures.loushy.cost_usd: per step onchatspans (from the run's measured cost, or the price table for flows), cumulative on parent spans (invoke_agent, and a delegatingexecute_tool) through a small rollup inwithSpan(), so anyTraceExportersees it. Absent when the price table does not know a contributing model.loushy.usage.estimatedis now also set on run spans whose tokens were estimated (it was already onchatspans).@opentelemetry/sdk-metricsadded as a devDependency (tests and the example). Docs "Metrics" section and attribute table, example prints metrics, CHANGELOG entry, regeneratedllms-full.txt.Why
Differentiator 6 of the audit: OTel GenAI conventions plus cost, out of the box, on any backend. Spans already followed the conventions (LOU-D9); this adds the metrics half and the cost attribute.
Spec note: the opentelemetry.io page was not reachable from the sandbox; names, units and attributes were read from the semantic-conventions v1.40
gen-ai-metrics.md(which definesgen_ai.client.token.usagewithgen_ai.token.type). The newest semantic-conventions-genai repo replaces that histogram with per-type counters; this PR follows the ticket and v1.40 and says so in docs/observability.md.Acceptance criteria
metrics: false; tool calls getgen_ai.client.operation.durationwithexecute_tool(spec allows it)loushy.cost_usdper step onchat, cumulative oninvoke_agent, absent when unpriced;loushy.usage.estimatedon run spansMeterProvider(global) and recording-meter tests for one pricedmockModelrun; span attribute tests in genAiSemconv/tracing tests; import-graph test that onlyexecution/otel.tsimports@opentelemetry/*(so the main package works without the optional peer)How verified
npx tsc --noEmit: passnpm run lint: 0 errors, 405 warnings (none in changed files)npm run test:types: no errorsnpx vitest run: 144 files passed, 2103 tests passed, 3 skippednpm run build && npm run build --workspace=packages/create-loushy-agent: passnpm run docs:verify-snippets -- --skip-build: all 108 snippets type-checknpm run docs:llms:check: pass (exit 0)npm run test:coverage: pass (thresholds met);npm run fallow: exit 0 (the pre-existing providers duplicate and agentDir/fsUtil target are unrelated to this change)npm run example:tracing:otel: prints spans and both metrics🤖 Generated with Claude Code
https://claude.ai/code/session_016bY29ibQgv3MDdjAbUT4YR
Generated by Claude Code