Skip to content

feat(diagnostics-otel): export cache-write TTL buckets on generic usage keys - #184661

Open
ryan-dyer-sp wants to merge 6 commits into
openclaw:mainfrom
ryan-dyer-sp:port/anthropic-ttl-genai-keys
Open

ryan-dyer-sp wants to merge 6 commits into
openclaw:mainfrom
ryan-dyer-sp:port/anthropic-ttl-genai-keys

Conversation

@ryan-dyer-sp

@ryan-dyer-sp ryan-dyer-sp commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Backends that price gen_ai.usage.* usage types cannot price Anthropic's separately-reported cache-write TTL buckets: #171663 exported cache_creation.ephemeral_5m_input_tokens / ephemeral_1h_input_tokens only on the OpenClaw-specific vocabulary (openclaw.model_call.usage.*), where generic usage-type consumers never see them. That is already breaking live pricing, not just a missing feature: Langfuse's own default Claude pricing ships a distinct input_cache_creation_1h rate ($4.00 per MTok vs $2.50 for 5m/combined on claude-sonnet-5), and any deployment using 1-hour prompt caching today has those writes priced at the combined rate — a 37.5% undercount on the 1h tokens — because the reported bucket never reaches the pricing rows.

User Impact

model.call spans now also carry gen_ai.usage.cache_creation.5m_input_tokens / gen_ai.usage.cache_creation.1h_input_tokens, exported verbatim when the provider reports them (explicit zero included) and absent otherwise. The keys sit in the standard's own cache-write sub-namespace (gen_ai.usage.cache_creation.*), in the same dotted breakdown shape Langfuse natively maps (#19420, merged 2026-10-28, first release after v4.71.0): current Langfuse normalizes them to its input_cache_creation_5m/_1h usage types — the exact rows its Claude default prices define — prices each bucket at its own rate, and rebalances the combined key to its unattributed remainder, so each write prices exactly once regardless of the backend price table. On earlier deployments the breakdown keys land as unpriced pass-through buckets (the split is visible in dashboards) and the combined total prices exactly once, unchanged from today. No producer-side configuration is needed either way.

Why This Change Was Made

gen_ai.* is the OpenTelemetry GenAI semantic-conventions namespace (open-telemetry/semantic-conventions-genai; registry: docs/registry/attributes/gen-ai.md, all attributes Development status). The standard defines cache counters as subsets of one authoritative prompt total — gen_ai.usage.input_tokens "SHOULD include all types of input tokens, including cached tokens", gen_ai.usage.cache_creation.input_tokens "SHOULD be included in" it — but no TTL attributes (the 5m/1h split is Anthropic-specific). These two keys extend the standard's cache-write sub-namespace with the provider-reported breakdown, in the same nested shape the standard already uses.

#171663's review kept the TTL split OpenClaw-specific so generic consumers could not price a total and its breakdown together. Since then the major generic consumer fixed that exact concern natively: Langfuse #19420 rebalances dotted gen_ai.usage.cache_creation.*_input_tokens breakdown keys, normalizing them to the priced usage types its Claude default prices define. Emitting the breakdown in that shape means the decomposition is enforced by the consumer's own mapping (older consumers see unpriced extra buckets, never additive prices) rather than depending on producers to omit provider-reported facts. Draft status is intentional pending maintainer direction on that reversal.

Evidence

Real behavior proof, current head 61f09a29e64 (rebased onto upstream/main 4fc73d6995b; built CLI) against a self-hosted Langfuse v4 deployment running past #19420 (worker bundle verified to contain the rebalance code; containers recreated 2026-10-29). One real non-streaming Anthropic Messages call, provider-reported counts verbatim, secrets/hosts redacted. The span: openclaw.model.call GENERATION, model claude-sonnet-5-5, 2026-10-29 14:04:00 UTC, trace 1d75af08dc8b5968b8b35002a2119d6b, span ca9195870b43044b.

Dotted breakdown on a both-conventions price table: combined auto-zeroed, write priced exactly once

Provider reported a 9,170-token cache write (all 5m, ephemeral_1h = 0 explicitly) plus a 36,162-token cache read. The deployment's price table for the model carries both conventions (combined input_cache_creation $2.50 + input_cache_creation_5m $2.50 + input_cache_creation_1h $4.00 per MTok) — the configuration that double-prices a total-plus-breakdown emission.

As ingested (usage_details → cost_details):

token class usage tokens rate $/MTok charged
input 223 2.00 0.000446
output 1,135 10.00 0.011350
cache read 36,162 0.10 0.0036162
cache write, combined 36,162 → 0 (rebalanced remainder) 2.50 $0
cache write, 5m 9,170 2.50 0.022925
cache write, 1h 0 4.00 0
span total $0.0383372

The breakdown was normalized to input_cache_creation_5m: 9170 / _1h: 0 (priced against the TTL rows), and the combined key was rebalanced to its unattributed remainder: 0 — so the combined row contributed nothing despite existing. One 9,170-token write → $0.022925, exactly once, with no backend row hygiene.

Contrast: the flat spelling on the same upgraded deployment does not rebalance

A control call emitted the earlier flat spelling (gen_ai.usage.input_cache_creation_5m) through a pre-switch build: same deployment, same price table. Its span (2026-10-29 13:59:21 UTC, trace 0da97d95e3b2ea22d56a16119d39ff77, span e35db4463bc30e8c) shows input_cache_creation: 36,162 — the full total, not a remainder — priced $0.090405 by the combined row alongside _5m $0.090405: the same write priced twice. #19420's rebalance triggers on the dotted breakdown keys only; the flat spelling leaves the combined total intact. The dotted emission shape is what makes the consumer-side decomposition apply.

Tests and checks: extensions/diagnostics-otel/src/service.otlp-export.test.ts — one boundary test, exports cache-write TTL facts on generic and OpenClaw usage keys (single setup/event set), asserting both vocabularies verbatim: generic dotted TTL keys and OpenClaw TTL keys export mixed 1h/5m buckets, explicit zeros, and stay absent when unreported, with per-family key-set expectations. Base-red verified: with only service-genai-attributes.ts reverted to upstream/main, that test fails on the generic TTL assertions (keys absent on base) while the remaining 20 tests pass; restored, the suite passes. tsgo:extensions, formatting, scoped oxlint on the changed files, and git diff --check all pass on the rebased head. No attribute row other than the two new keys changes.

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime extensions: diagnostics-otel Extension: diagnostics-otel size: XS labels Oct 26, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:feature Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:feature Preliminary submission classification; full source review pending. labels Oct 26, 2026
@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge.

What this changes

This PR adds provider-reported five-minute and one-hour cache-write counts to generic GenAI span attributes.

Example: A model call reports 600,000 five-minute and 400,000 one-hour cache-write tokens.

  • Before: Generic usage attributes expose only the combined 1,000,000-token cache-write count.
  • After: The two additional dotted attributes let updated Langfuse price each TTL separately while retaining only the unattributed remainder in the combined bucket.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Focused implementation with relevant live output and no verified defect; readiness remains capped by the unresolved product decision.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The supplied built-CLI Anthropic call into updated self-hosted Langfuse exercises the changed mapper and ingestion path, showing the dotted five-minute bucket, explicit one-hour zero, and one cache-write charge; those changed files remain identical after the merge from main.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Feature · Worth it: Needs a maintainer decision · Fix scope: Complete
User problem: Generic telemetry pricing cannot distinguish one-hour cache writes from the combined cache-write bucket.
Reason: The user value is concrete, but this intentionally changes the export vocabulary selected by the earlier merged work.

Merge readiness

⛔ Blocked before merge - 1 item remains

Keep open: this PR provides useful TTL-aware telemetry with no verified code defect, but changing the deliberately chosen export vocabulary still needs owner direction.

Priority: P2
Reviewed head: 7dad6f5468c76f95c32c72c22077b5bb431fb17c
Owner decision: Required. See Decision needed.

Decision needed

  • Question: Should OpenClaw export these two nonstandard TTL attributes under gen_ai.usage now that Langfuse supports their dotted breakdown?
  • Recommendation: Approve the dotted TTL attributes: Accept the focused export extension based on verified downstream normalization, aggregate-price fallback, and supplied live behavior.
  • Why: The downstream implementation addresses the earlier pricing concern, but the deliberate OpenClaw-only placement in fix(diagnostics-otel): model call spans drop Anthropic cache-write TTL split #171663 has no recorded owner-approved reversal.

Before merge

  • Resolve maintainer decision - Resolve the maintainer decision shown above before merge.

Findings

None.

Agent review details

How this fits together

The diagnostics-otel plugin maps normalized model usage into span attributes, which OTLP exporters deliver to telemetry backends for analysis and pricing.

flowchart LR
  A[Provider usage] --> B[Diagnostic event]
  B --> C[Model recorder]
  C --> D[Usage attribute mapper]
  D --> E[OTLP exporter]
  E --> F[Backend usage mapping]
  F --> G[TTL pricing]
Loading

Technical review

Best possible solution:

With owner approval, retain the two additions in the existing mapper while preserving the combined totals and OpenClaw attributes.

Do we have a high-confidence way to reproduce the issue?

Current-main source exposes the combined generic cache-write count and OpenClaw TTL counts, but omits the dotted TTL attributes recognized by updated Langfuse; no target code was executed during this read-only review.

Is this the best way to solve the issue?

The proposed implementation is the smallest complete export extension: two mappings in the existing owner, matching documentation, and one extended boundary regression.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 2a16e47f90a4.

Provenance checked

  • TTL export vocabulary and documentation changes intended behavior with a stated reason (9999f71: Keep TTL subsets outside generic usage so consumers do not price the combined total and its decomposition twice.)
  • Consolidated export regression keeps the original intent (423ef7a: Consolidate boundary coverage and remove repeated setup; this PR extends the retained test rather than introducing another test layer.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 351c069 adds extensions/diagnostics-otel/src/service-genai-attributes.ts:131 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 351c069ed235; files: extensions/diagnostics-otel/src/service-genai-attributes.ts)
  • Ryan: Raw commit 9999f71 adds extensions/diagnostics-otel/src/service-genai-attributes.ts:123 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 9999f7159e7a; files: extensions/diagnostics-otel/src/service-genai-attributes.ts)

PR surface

Source +7, Tests +29, Docs +1. Total +37 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 8 1 +7
Tests 1 38 9 +29
Docs 1 2 1 +1
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 48 11 +37

Labels

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: ⏳ waiting on maintainer: Only explicit maintainer approvals or decisions remain; merge is still blocked.

Label justifications:

  • P2: TTL-aware downstream pricing is a useful, bounded telemetry improvement without a demonstrated core workflow outage.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: ⏳ waiting on maintainer: Only explicit maintainer approvals or decisions remain; merge is still blocked.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/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.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (5 earlier review cycles)
  • reviewed 2026-10-26T13:27:36.601Z sha fa34a6b29488f7915a467cfb90eb6499dfe305a7 :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-26T15:34:25.073Z sha 02857b099b5bb069a825b5bf768d65f626ceb443 :: needs real behavior proof before merge. :: [P1] Preserve additive consumer accounting when exporting TTL rows
  • reviewed 2026-10-26T17:01:49.274Z sha 02857b099b5bb069a825b5bf768d65f626ceb443 :: blocked before merge. :: [P1] Preserve additive consumer accounting when exporting TTL rows
  • reviewed 2026-10-26T19:17:34.319Z sha 02857b099b5bb069a825b5bf768d65f626ceb443 :: blocked before merge. :: [P1] Preserve additive consumer accounting when exporting TTL rows
  • reviewed 2026-10-29T16:39:25.521Z sha 61f09a29e64e417ee8afb1b9cbfc132cfaf5886b :: blocked before merge. :: none

Reviewed November 4, 2026, 8:34 AM ET / 13:34 UTC (Revision 6).

@ryan-dyer-sp

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:feature Preliminary submission classification; full source review pending. and removed clawsweeper:intake:feature Preliminary submission classification; full source review pending. labels Oct 26, 2026
@ryan-dyer-sp

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:feature Preliminary submission classification; full source review pending. proof: sufficient ClawSweeper judged the real behavior proof convincing. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. clawsweeper:intake:feature Preliminary submission classification; full source review pending. labels Oct 26, 2026
@ryan-dyer-sp

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review . You seem hung up on double pricing for downstream consumers, but are ignoring that they are already broken for any 1h caches currently.

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:feature Preliminary submission classification; full source review pending. and removed clawsweeper:intake:feature Preliminary submission classification; full source review pending. labels Oct 26, 2026
@ryan-dyer-sp
ryan-dyer-sp marked this pull request as ready for review October 26, 2026 19:35
@clawsweeper clawsweeper Bot removed proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Oct 28, 2026
@ryan-dyer-sp
ryan-dyer-sp force-pushed the port/anthropic-ttl-genai-keys branch from 4830b5d to 61f09a2 Compare October 29, 2026 14:17
@ryan-dyer-sp

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T16:38:18.788404Z 61f09a2 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ryan-dyer-sp

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added the clawsweeper:intake:feature Preliminary submission classification; full source review pending. label Oct 29, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 61f09a29e6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ryan-dyer-sp

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked. and removed clawsweeper:intake:feature Preliminary submission classification; full source review pending. labels Nov 4, 2026
…dition

Drop the stale OpenClaw-scoping statements superseded by the generic
keys (code comment, docs sentence, test title/absence assertion) and
cite the OTel GenAI conventions on the generic rows instead: the
standard defines no TTL attributes and keeps cache counters as
subsets of the authoritative gen_ai.usage.input_tokens blend.
Merge the split OpenClaw/generic cache-write TTL tests back into one
boundary test: one service setup, one event set, one pass, asserting
both usage vocabularies' values and key sets. Reviewer note: the
split duplicated the setup for no extra coverage.
…ted sub-namespace

Switch the generic TTL keys from the flat input_cache_creation_1h/_5m
spelling to gen_ai.usage.cache_creation.5m/1h_input_tokens, the dotted
breakdown shape Langfuse openclaw#19420 normalizes to priced
input_cache_creation_5m/_1h usage types while rebalancing the total to
its unattributed remainder. Each write prices exactly once on current
Langfuse; on earlier deployments the keys stay unpriced pass-through
buckets and the combined total prices once, unchanged.
@ryan-dyer-sp
ryan-dyer-sp force-pushed the port/anthropic-ttl-genai-keys branch from 7dad6f5 to b6d72e1 Compare November 10, 2026 14:06
…dget

The cache-write TTL boundary test grows net +29 test lines over the
base ceiling (exact git numstat delta of the export suite changes).
Allocate that growth for PR openclaw#184661 per the test-line cost budget.
…-specific references

Rewrap the emitter and test comments to the repo's wider comment width
and describe the decomposition contract in consumer-neutral terms.
Resize the test-line allocation record to the resulting exact net
delta (25).
@ryan-dyer-sp

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added the clawsweeper:intake:feature Preliminary submission classification; full source review pending. label Nov 10, 2026

This branch has not been deployed

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

Labels

clawsweeper:intake:feature Preliminary submission classification; full source review pending. clawtributors PR from a clawtributor docs Improvements or additions to documentation extensions: diagnostics-otel Extension: diagnostics-otel gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S status: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants