Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .chloggen/genai_cache_tokens.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
change_type: enhancement
component: gen-ai
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

- Add `gen_ai.usage.cache_creation.input_tokens` attribute for tokens written to provider cache
- Add provider-specific token handling notes to OpenAI span
- Add Anthropic span with computation guidance for `gen_ai.usage.input_tokens`
3 changes: 2 additions & 1 deletion docs/gen-ai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ Semantic conventions for Generative AI operations are defined for the following

Technology specific semantic conventions are defined for the following GenAI system:

* [Anthropic](./anthropic.md): Semantic Conventions for Anthropic.
* [Azure AI Inference](./azure-ai-inference.md): Semantic Conventions for Azure AI Inference.
* [OpenAI](./openai.md): Semantic Conventions for OpenAI.
* [AWS Bedrock](./aws-bedrock.md): Semantic Conventions for AWS Bedrock.
* [OpenAI](./openai.md): Semantic Conventions for OpenAI.

See also:

Expand Down
246 changes: 246 additions & 0 deletions docs/gen-ai/anthropic.md

Large diffs are not rendered by default.

45 changes: 28 additions & 17 deletions docs/gen-ai/aws-bedrock.md

Large diffs are not rendered by default.

33 changes: 22 additions & 11 deletions docs/gen-ai/azure-ai-inference.md

Large diffs are not rendered by default.

45 changes: 28 additions & 17 deletions docs/gen-ai/gen-ai-agent-spans.md

Large diffs are not rendered by default.

33 changes: 22 additions & 11 deletions docs/gen-ai/gen-ai-events.md

Large diffs are not rendered by default.

68 changes: 42 additions & 26 deletions docs/gen-ai/gen-ai-spans.md

Large diffs are not rendered by default.

30 changes: 19 additions & 11 deletions docs/gen-ai/openai.md

Large diffs are not rendered by default.

37 changes: 24 additions & 13 deletions docs/registry/attributes/gen-ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ This document defines the attributes used to describe telemetry in the context o
| <a id="gen-ai-tool-description" href="#gen-ai-tool-description">`gen_ai.tool.description`</a> | ![Development](https://img.shields.io/badge/-development-blue) | string | The tool description. | `Multiply two numbers` |
| <a id="gen-ai-tool-name" href="#gen-ai-tool-name">`gen_ai.tool.name`</a> | ![Development](https://img.shields.io/badge/-development-blue) | string | Name of the tool utilized by the agent. | `Flights` |
| <a id="gen-ai-tool-type" href="#gen-ai-tool-type">`gen_ai.tool.type`</a> | ![Development](https://img.shields.io/badge/-development-blue) | string | Type of the tool utilized by the agent [15] | `function`; `extension`; `datastore` |
| <a id="gen-ai-usage-input-tokens" href="#gen-ai-usage-input-tokens">`gen_ai.usage.input_tokens`</a> | ![Development](https://img.shields.io/badge/-development-blue) | int | The number of tokens used in the GenAI input (prompt). | `100` |
| <a id="gen-ai-usage-cache-creation-input-tokens" href="#gen-ai-usage-cache-creation-input-tokens">`gen_ai.usage.cache_creation.input_tokens`</a> | ![Development](https://img.shields.io/badge/-development-blue) | int | The number of input tokens written to a provider-managed cache. [16] | `25` |
| <a id="gen-ai-usage-cache-read-input-tokens" href="#gen-ai-usage-cache-read-input-tokens">`gen_ai.usage.cache_read.input_tokens`</a> | ![Development](https://img.shields.io/badge/-development-blue) | int | The number of input tokens served from a provider-managed cache. [17] | `50` |
| <a id="gen-ai-usage-input-tokens" href="#gen-ai-usage-input-tokens">`gen_ai.usage.input_tokens`</a> | ![Development](https://img.shields.io/badge/-development-blue) | int | The number of tokens used in the GenAI input (prompt). [18] | `100` |
| <a id="gen-ai-usage-output-tokens" href="#gen-ai-usage-output-tokens">`gen_ai.usage.output_tokens`</a> | ![Development](https://img.shields.io/badge/-development-blue) | int | The number of tokens used in the GenAI response (completion). | `180` |

**[1] `gen_ai.data_source.id`:** Data sources are used by AI agents and RAG applications to store grounding data. A data source may be an external database, object store, document collection, website, or any other storage system used by the GenAI agent or application. The `gen_ai.data_source.id` SHOULD match the identifier used by the GenAI system rather than a name specific to the external storage, such as a database or object store. Semantic conventions referencing `gen_ai.data_source.id` MAY also leverage additional attributes, such as `db.*`, to further identify and describe the data source.
Expand Down Expand Up @@ -193,6 +195,15 @@ Function: A tool executed on the client-side, where the agent generates paramete
Client-side operations are actions taken on the user's end or within the client application.
Datastore: A tool used by the agent to access and query structured or unstructured external data for retrieval-augmented tasks or knowledge updates.

**[16] `gen_ai.usage.cache_creation.input_tokens`:** The value SHOULD be included in `gen_ai.usage.input_tokens`.

**[17] `gen_ai.usage.cache_read.input_tokens`:** The value SHOULD be included in `gen_ai.usage.input_tokens`.

**[18] `gen_ai.usage.input_tokens`:** This value SHOULD include all types of input tokens, including cached tokens.
Instrumentations SHOULD make a best effort to populate this value, using a total
provided by the provider when available or, depending on the provider API,
by summing different token types parsed from the provider output.

---

`gen_ai.operation.name` has the following list of well-known values. If one of them applies, then the respective value MUST be used; otherwise, a custom value MAY be used.
Expand Down Expand Up @@ -231,21 +242,21 @@ Datastore: A tool used by the agent to access and query structured or unstructur
| `azure.ai.openai` | [Azure OpenAI](https://azure.microsoft.com/products/ai-services/openai-service/) | ![Development](https://img.shields.io/badge/-development-blue) |
| `cohere` | [Cohere](https://cohere.com/) | ![Development](https://img.shields.io/badge/-development-blue) |
| `deepseek` | [DeepSeek](https://www.deepseek.com/) | ![Development](https://img.shields.io/badge/-development-blue) |
| `gcp.gemini` | [Gemini](https://cloud.google.com/products/gemini) [16] | ![Development](https://img.shields.io/badge/-development-blue) |
| `gcp.gen_ai` | Any Google generative AI endpoint [17] | ![Development](https://img.shields.io/badge/-development-blue) |
| `gcp.vertex_ai` | [Vertex AI](https://cloud.google.com/vertex-ai) [18] | ![Development](https://img.shields.io/badge/-development-blue) |
| `gcp.gemini` | [Gemini](https://cloud.google.com/products/gemini) [19] | ![Development](https://img.shields.io/badge/-development-blue) |
| `gcp.gen_ai` | Any Google generative AI endpoint [20] | ![Development](https://img.shields.io/badge/-development-blue) |
| `gcp.vertex_ai` | [Vertex AI](https://cloud.google.com/vertex-ai) [21] | ![Development](https://img.shields.io/badge/-development-blue) |
| `groq` | [Groq](https://groq.com/) | ![Development](https://img.shields.io/badge/-development-blue) |
| `ibm.watsonx.ai` | [IBM Watsonx AI](https://www.ibm.com/products/watsonx-ai) | ![Development](https://img.shields.io/badge/-development-blue) |
| `mistral_ai` | [Mistral AI](https://mistral.ai/) | ![Development](https://img.shields.io/badge/-development-blue) |
| `openai` | [OpenAI](https://openai.com/) | ![Development](https://img.shields.io/badge/-development-blue) |
| `perplexity` | [Perplexity](https://www.perplexity.ai/) | ![Development](https://img.shields.io/badge/-development-blue) |
| `x_ai` | [xAI](https://x.ai/) | ![Development](https://img.shields.io/badge/-development-blue) |

**[16]:** Used when accessing the 'generativelanguage.googleapis.com' endpoint. Also known as the AI Studio API.
**[19]:** Used when accessing the 'generativelanguage.googleapis.com' endpoint. Also known as the AI Studio API.

**[17]:** May be used when specific backend is unknown.
**[20]:** May be used when specific backend is unknown.

**[18]:** Used when accessing the 'aiplatform.googleapis.com' endpoint.
**[21]:** Used when accessing the 'aiplatform.googleapis.com' endpoint.

---

Expand Down Expand Up @@ -282,21 +293,21 @@ Describes deprecated `gen_ai` attributes.
| `azure.ai.openai` | Azure OpenAI | ![Development](https://img.shields.io/badge/-development-blue) |
| `cohere` | Cohere | ![Development](https://img.shields.io/badge/-development-blue) |
| `deepseek` | DeepSeek | ![Development](https://img.shields.io/badge/-development-blue) |
| `gcp.gemini` | Gemini [19] | ![Development](https://img.shields.io/badge/-development-blue) |
| `gcp.gen_ai` | Any Google generative AI endpoint [20] | ![Development](https://img.shields.io/badge/-development-blue) |
| `gcp.vertex_ai` | Vertex AI [21] | ![Development](https://img.shields.io/badge/-development-blue) |
| `gcp.gemini` | Gemini [22] | ![Development](https://img.shields.io/badge/-development-blue) |
| `gcp.gen_ai` | Any Google generative AI endpoint [23] | ![Development](https://img.shields.io/badge/-development-blue) |
| `gcp.vertex_ai` | Vertex AI [24] | ![Development](https://img.shields.io/badge/-development-blue) |
| `groq` | Groq | ![Development](https://img.shields.io/badge/-development-blue) |
| `ibm.watsonx.ai` | IBM Watsonx AI | ![Development](https://img.shields.io/badge/-development-blue) |
| `mistral_ai` | Mistral AI | ![Development](https://img.shields.io/badge/-development-blue) |
| `openai` | OpenAI | ![Development](https://img.shields.io/badge/-development-blue) |
| `perplexity` | Perplexity | ![Development](https://img.shields.io/badge/-development-blue) |
| `xai` | xAI | ![Development](https://img.shields.io/badge/-development-blue) |

**[19]:** This refers to the 'generativelanguage.googleapis.com' endpoint. Also known as the AI Studio API. May use common attributes prefixed with 'gcp.gen_ai.'.
**[22]:** This refers to the 'generativelanguage.googleapis.com' endpoint. Also known as the AI Studio API. May use common attributes prefixed with 'gcp.gen_ai.'.

**[20]:** May be used when specific backend is unknown. May use common attributes prefixed with 'gcp.gen_ai.'.
**[23]:** May be used when specific backend is unknown. May use common attributes prefixed with 'gcp.gen_ai.'.

**[21]:** This refers to the 'aiplatform.googleapis.com' endpoint. May use common attributes prefixed with 'gcp.gen_ai.'.
**[24]:** This refers to the 'aiplatform.googleapis.com' endpoint. May use common attributes prefixed with 'gcp.gen_ai.'.

## Deprecated OpenAI GenAI Attributes

Expand Down
19 changes: 19 additions & 0 deletions model/gen-ai/registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,26 @@ groups:
stability: development
type: int
brief: The number of tokens used in the GenAI input (prompt).
note: |
This value SHOULD include all types of input tokens, including cached tokens.
Instrumentations SHOULD make a best effort to populate this value, using a total
provided by the provider when available or, depending on the provider API,
by summing different token types parsed from the provider output.
examples: [100]
- id: gen_ai.usage.cache_read.input_tokens
stability: development
type: int
brief: The number of input tokens served from a provider-managed cache.
note: >
The value SHOULD be included in `gen_ai.usage.input_tokens`.
examples: [50]
- id: gen_ai.usage.cache_creation.input_tokens
stability: development
type: int
brief: The number of input tokens written to a provider-managed cache.
note: >
The value SHOULD be included in `gen_ai.usage.input_tokens`.
examples: [25]
- id: gen_ai.usage.output_tokens
stability: development
type: int
Expand Down
36 changes: 36 additions & 0 deletions model/gen-ai/spans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ groups:
requirement_level: recommended
- ref: gen_ai.usage.input_tokens
requirement_level: recommended
- ref: gen_ai.usage.cache_read.input_tokens
requirement_level: recommended
- ref: gen_ai.usage.cache_creation.input_tokens
requirement_level: recommended
- ref: gen_ai.usage.output_tokens
requirement_level: recommended
- ref: gen_ai.conversation.id
Expand Down Expand Up @@ -176,6 +180,12 @@ groups:
attributes:
- ref: gen_ai.request.model
requirement_level: required
- ref: gen_ai.usage.input_tokens
note: >
The total input token count is returned by `usage.input_tokens` or a similar property in the model response.
- ref: gen_ai.usage.cache_read.input_tokens
note: >
Corresponds to `usage.input_tokens_details.cached_tokens` or a similar property in the model response.
- ref: openai.request.service_tier
requirement_level:
conditionally_required: if the request includes a service_tier and the value is not 'auto'
Expand Down Expand Up @@ -432,3 +442,29 @@ groups:
requirement_level: required
- ref: aws.bedrock.knowledge_base.id
requirement_level: recommended

- id: span.anthropic.inference.client
Comment thread
verdie-g marked this conversation as resolved.
extends: attributes.gen_ai.inference.client
stability: development
span_kind: client
type: span
brief: >
Semantic Conventions for [Anthropic](https://www.anthropic.com/) client spans extend
and override the semantic conventions for [Gen AI Spans](gen-ai-spans.md).
note: |
`gen_ai.provider.name` MUST be set to `"anthropic"` and SHOULD be provided **at span creation time**.

**Span name** SHOULD be `{gen_ai.operation.name} {gen_ai.request.model}`.
attributes:
- ref: gen_ai.usage.input_tokens
note: >
Anthropic `input_tokens` excludes cached tokens. Compute:
`gen_ai.usage.input_tokens = input_tokens + cache_read_input_tokens + cache_creation_input_tokens`
- ref: gen_ai.usage.cache_read.input_tokens
note: >
Anthropic reports this separately from `input_tokens`.
This value MUST be added to the Anthropic `input_tokens` to compute `gen_ai.usage.input_tokens`.
- ref: gen_ai.usage.cache_creation.input_tokens
note: >
Anthropic reports this separately from `input_tokens`.
This value MUST be added to the Anthropic `input_tokens` to compute `gen_ai.usage.input_tokens`.
Loading