Skip to content

genai: define cached tokens attributes#3163

Merged
joaopgrassi merged 10 commits intoopen-telemetry:mainfrom
verdie-g:gen-ai-cached-tokens
Jan 27, 2026
Merged

genai: define cached tokens attributes#3163
joaopgrassi merged 10 commits intoopen-telemetry:mainfrom
verdie-g:gen-ai-cached-tokens

Conversation

@verdie-g
Copy link
Copy Markdown
Contributor

@verdie-g verdie-g commented Dec 2, 2025

Fixes open-telemetry/semantic-conventions-genai#23

Changes

This PR adds two new GenAI attributes to represent provider-level prompt caching:

  • gen_ai.usage.cache_read_input_tokens
  • gen_ai.usage.cache_creation_input_tokens

It also updates the description of gen_ai.usage.input_tokens to state that it must include cached tokens. OpenAI and Vertex AI already count cached tokens in input_tokens, while Anthropic excludes them.

Merge requirement checklist

  • CONTRIBUTING.md guidelines followed.
  • Change log entry added, according to the guidelines in When to add a changelog entry.
    • If your PR does not need a change log, start the PR title with [chore]
  • Links to the prototypes or existing instrumentations (when adding or changing conventions)

Comment thread model/gen-ai/spans.yaml Outdated
@eddyerburgh
Copy link
Copy Markdown

eddyerburgh commented Dec 17, 2025

@lmolkova are there any concerns here or do you think this can be merged? Would be great to get these added to the spec

@alexmojaki
Copy link
Copy Markdown
Contributor

@eddyerburgh see my review comment, that's a required change

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jan 3, 2026

This PR has been labeled as stale due to lack of activity. It will be automatically closed if there is no further activity over the next 7 days.

@github-actions github-actions Bot added the Stale label Jan 3, 2026
@verdie-g
Copy link
Copy Markdown
Contributor Author

verdie-g commented Jan 3, 2026

@lmolkova could you have a look at this one before it gets automatically closed 🙏

@github-actions github-actions Bot removed the Stale label Jan 4, 2026
@github-actions
Copy link
Copy Markdown

This PR has been labeled as stale due to lack of activity. It will be automatically closed if there is no further activity over the next 7 days.

@github-actions github-actions Bot added the Stale label Jan 19, 2026
@alexmojaki
Copy link
Copy Markdown
Contributor

There's merge conflicts now

@verdie-g verdie-g force-pushed the gen-ai-cached-tokens branch from c2e4498 to b86579e Compare January 19, 2026 10:20
Comment thread model/gen-ai/registry.yaml Outdated
Comment thread model/gen-ai/registry.yaml Outdated
Comment thread model/gen-ai/registry.yaml Outdated
Comment thread model/gen-ai/registry.yaml Outdated
Comment thread model/gen-ai/spans.yaml Outdated
@verdie-g verdie-g force-pushed the gen-ai-cached-tokens branch from fc9db01 to 1c01bce Compare January 21, 2026 15:31
@verdie-g verdie-g requested a review from lmolkova January 21, 2026 15:40
@github-project-automation github-project-automation Bot moved this from Awaiting codeowners approval to Needs More Approval in Semantic Conventions Triage Jan 26, 2026
@lmolkova
Copy link
Copy Markdown
Member

@verdie-g thanks for the contribution, everything looks great. I think you need to regenerate markdown tables and it'd be good to go!

@lmolkova lmolkova moved this from Needs More Approval to Ready to be Merged in Semantic Conventions Triage Jan 26, 2026
@joaopgrassi joaopgrassi added this pull request to the merge queue Jan 27, 2026
Merged via the queue into open-telemetry:main with commit 9d7d97a Jan 27, 2026
18 checks passed
note: Add cache token attributes and provider-specific normalization guidance for GenAI usage metrics
issues: [1959]
subtext: |
- Add `gen_ai.usage.cache_read.input_tokens` attribute for tokens served from provider cache
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be recorded in metrics as well? Assuming yes, what should the gen_ai.token.type be?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephentoub not yet, there is no metric definition for it (no type defined). There are probably some design flaws we'll need to fix to make it happen. Created https://github.com/open-telemetry/semantic-conventions/issues/3341 so we don't forget

rogeriochaves added a commit to langwatch/langwatch that referenced this pull request Feb 8, 2026
…ventions

Add support for extracting cache_read_input_tokens, cache_creation_input_tokens,
and reasoning_tokens from OTEL span attributes in both the Elasticsearch
ingestion path (otel.traces.ts) and the ClickHouse-to-ES mapper (span.mapper.ts).

- Extract `gen_ai.usage.cache_read.input_tokens` and
  `gen_ai.usage.cache_creation.input_tokens` per official OTEL GenAI semconv
  (open-telemetry/semantic-conventions#3163)
- Extract `gen_ai.usage.reasoning_tokens` (Traceloop/OpenLLMetry convention)
- Aggregate cache and reasoning tokens at trace level in metrics computation
- Add Elasticsearch schema fields and migration for the new token metrics
- Clean up redundant `gen_ai.usage.total_tokens` from params
- Remove leftover debug console.logs from otel.metrics.ts
rogeriochaves added a commit to langwatch/langwatch that referenced this pull request Feb 9, 2026
* feat: support gen_ai.input/output.messages and parts pattern in OTEL traces

Map gen_ai.input.messages and gen_ai.output.messages (OTEL GenAI semantic
conventions) to LangWatch input/output fields. Previously these ended up
in params with input: null and output: null.

- Extract gen_ai.input/output.messages directly as chat_messages without
  Zod validation to avoid stripping provider-specific content blocks
  (Anthropic tool_use/tool_result, pi-ai parts, etc.)
- Support the "parts" pattern (Vercel AI SDK / pi-ai) natively in
  ChatMessage type alongside "content"
- Handle content blocks with {type:"text", content:"..."} in addition
  to {type:"text", text:"..."} for text extraction
- Widen LLM type detection to include CLIENT span kind (OTEL GenAI spec)
- Increase params truncation from 32KB to 128KB
- Extract system instructions from pre-existing gen_ai.input.messages
  in ClickHouse pipeline

* feat: extract cache and reasoning tokens from OTEL GenAI semantic conventions

Add support for extracting cache_read_input_tokens, cache_creation_input_tokens,
and reasoning_tokens from OTEL span attributes in both the Elasticsearch
ingestion path (otel.traces.ts) and the ClickHouse-to-ES mapper (span.mapper.ts).

- Extract `gen_ai.usage.cache_read.input_tokens` and
  `gen_ai.usage.cache_creation.input_tokens` per official OTEL GenAI semconv
  (open-telemetry/semantic-conventions#3163)
- Extract `gen_ai.usage.reasoning_tokens` (Traceloop/OpenLLMetry convention)
- Aggregate cache and reasoning tokens at trace level in metrics computation
- Add Elasticsearch schema fields and migration for the new token metrics
- Clean up redundant `gen_ai.usage.total_tokens` from params
- Remove leftover debug console.logs from otel.metrics.ts

* fix: add missing monitors.getById mock in OnlineEvaluationDrawer test helpers

The useContext mock was missing monitors.getById.invalidate, causing
3 unhandled TypeError exceptions during update mutation tests.

* fix: use long type for cache token fields in ES schema and migration

The field was already mapped as long in ES (from OTEL intValue ingestion),
so the migration must use long to avoid illegal_argument_exception.

* feat: stop deleting token attrs from params and display cache tokens in drawer

- Stop deleting gen_ai.usage.* token fields from span params so they
  remain visible in the trace details
- Display cache_read and cache_creation tokens in the SpanDetails drawer
  alongside prompt/completion/reasoning tokens
sergioestebance pushed a commit to langwatch/langwatch that referenced this pull request Feb 9, 2026
* feat: support gen_ai.input/output.messages and parts pattern in OTEL traces

Map gen_ai.input.messages and gen_ai.output.messages (OTEL GenAI semantic
conventions) to LangWatch input/output fields. Previously these ended up
in params with input: null and output: null.

- Extract gen_ai.input/output.messages directly as chat_messages without
  Zod validation to avoid stripping provider-specific content blocks
  (Anthropic tool_use/tool_result, pi-ai parts, etc.)
- Support the "parts" pattern (Vercel AI SDK / pi-ai) natively in
  ChatMessage type alongside "content"
- Handle content blocks with {type:"text", content:"..."} in addition
  to {type:"text", text:"..."} for text extraction
- Widen LLM type detection to include CLIENT span kind (OTEL GenAI spec)
- Increase params truncation from 32KB to 128KB
- Extract system instructions from pre-existing gen_ai.input.messages
  in ClickHouse pipeline

* feat: extract cache and reasoning tokens from OTEL GenAI semantic conventions

Add support for extracting cache_read_input_tokens, cache_creation_input_tokens,
and reasoning_tokens from OTEL span attributes in both the Elasticsearch
ingestion path (otel.traces.ts) and the ClickHouse-to-ES mapper (span.mapper.ts).

- Extract `gen_ai.usage.cache_read.input_tokens` and
  `gen_ai.usage.cache_creation.input_tokens` per official OTEL GenAI semconv
  (open-telemetry/semantic-conventions#3163)
- Extract `gen_ai.usage.reasoning_tokens` (Traceloop/OpenLLMetry convention)
- Aggregate cache and reasoning tokens at trace level in metrics computation
- Add Elasticsearch schema fields and migration for the new token metrics
- Clean up redundant `gen_ai.usage.total_tokens` from params
- Remove leftover debug console.logs from otel.metrics.ts

* fix: add missing monitors.getById mock in OnlineEvaluationDrawer test helpers

The useContext mock was missing monitors.getById.invalidate, causing
3 unhandled TypeError exceptions during update mutation tests.

* fix: use long type for cache token fields in ES schema and migration

The field was already mapped as long in ES (from OTEL intValue ingestion),
so the migration must use long to avoid illegal_argument_exception.

* feat: stop deleting token attrs from params and display cache tokens in drawer

- Stop deleting gen_ai.usage.* token fields from span params so they
  remain visible in the trace details
- Display cache_read and cache_creation tokens in the SpanDetails drawer
  alongside prompt/completion/reasoning tokens
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

This PR contains changes to area(s) that do not have an active SIG/project and will be auto-closed:

  • gen-ai

Such changes may be rejected or put on hold until a new SIG/project is established.

Please refer to the Semantic Convention Areas
document to see the current active SIGs and also to learn how to kick start a new one.

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

Development

Successfully merging this pull request may close these issues.

More detailed token usage span attributes and metrics

6 participants