Skip to content

feat(telemetry): normalise cache and reasoning token fields in GenerationMetadata across providers #1074

Closed

Description

@planetf1

Problem

record_token_usage in mellea/telemetry/backend_instrumentation.py extracts cache and reasoning token counts using get_value, which does not walk nested paths. As a result:

  • Anthropic: cache_read_input_tokens and cache_creation_input_tokens are top-level in the usage object — extracted correctly today.
  • OpenAI / LiteLLM: cache tokens live at prompt_tokens_details.cached_tokens and reasoning tokens at completion_tokens_details.reasoning_tokens — silently absent from spans because get_value cannot traverse the nesting.

This means gen_ai.usage.cache_read.input_tokens and gen_ai.usage.reasoning.output_tokens are never emitted for OpenAI or LiteLLM backends, with no warning to the user.

A secondary concern: the attribute names themselves (gen_ai.usage.cache_read.input_tokens etc.) are Anthropic-specific and not yet defined in the upstream OTel GenAI semconv — see #1054 for the naming audit.

Why not fix in the current extraction layer

Patching get_value for dotted-path walking or adding provider-specific branches in record_token_usage is short-lived work: after #1045, record_token_usage will read from mot.generation (GenerationMetadata) rather than raw provider response objects, making any provider-specific path logic obsolete.

Approach

During or after #1045, ensure GenerationMetadata (or the per-backend post-processing that populates it) normalises cache and reasoning token fields to a flat, provider-agnostic shape. record_token_usage then reads the normalised struct without any provider-specific path walking.

Specifically:

  • OpenAI backend post_processing(): flatten prompt_tokens_details.cached_tokens → mot.usage["cache_read_input_tokens"]
  • OpenAI backend post_processing(): flatten completion_tokens_details.reasoning_tokens → mot.usage["reasoning_tokens"]
  • Verify LiteLLM normalisation path does the same (LiteLLM may already flatten this)
  • Confirm attribute names align with OTel GenAI semconv at the time of implementation (coordinate with test: backend coverage parity audit post-refactor #1054)

Prerequisites

Related

Activity

  1. added
    area/backends
    area/telemetry
    enhancement
    on May 20, 2026
  2. added
    p2
    on Jun 18, 2026
  3. ajbozarth commented on Jul 15, 2026

    @ajbozarth
    Member

    When implementing this, name the normalised fields to match semconv from the start rather than adding them and renaming later. The naming authority for these is #1054 (parity audit), which cross-checks gen_ai.usage.cache_read.input_tokens / gen_ai.usage.reasoning.output_tokens against the OTel GenAI semconv and renames-or-flags Anthropic-specific extensions. Pull that check forward: settle the final attribute names while adding the extraction in post_processing(), so #1054 becomes a verification rather than a rename.

  4. planetf1 commented on Oct 5, 2026

    @planetf1
    ContributorAuthor

    Fixed.

    Verified against upstream/main (2026-10-05):

    • Normalisation: set_usage_attrs (mellea/telemetry/_tracing_helpers.py:119-124) now prefers the top-level counts and falls back to the nested prompt_tokens_details.cached_tokens / completion_tokens_details.reasoning_tokens paths, so the OpenAI/LiteLLM fields are no longer silently dropped and gen_ai.usage.cache_read.input_tokens / gen_ai.usage.reasoning.output_tokens emit for all providers.
    • Naming (secondary concern): resolved by 67bfc7487 (feat(telemetry): align spans and metrics with OTel GenAI semantic conventions #1551), which aligned the usage attribute names with the OTel GenAI semconv per the test: backend coverage parity audit post-refactor #1054 audit — taken forward as ajbozarth's comment suggested rather than renamed after the fact.
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

    area/backendsarea/telemetryenhancementp2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions