|
| 1 | +<!--- Hugo front matter used to generate the website version of this page: |
| 2 | +linkTitle: LLM requests |
| 3 | +---> |
| 4 | + |
| 5 | +# Semantic Conventions for LLM requests |
| 6 | + |
| 7 | +**Status**: [Experimental][DocumentStatus] |
| 8 | + |
| 9 | +<!-- Re-generate TOC with `markdown-toc --no-first-h1 -i` --> |
| 10 | + |
| 11 | +<!-- toc --> |
| 12 | + |
| 13 | +- [Configuration](#configuration) |
| 14 | +- [LLM Request attributes](#llm-request-attributes) |
| 15 | +- [Events](#events) |
| 16 | + |
| 17 | +<!-- tocstop --> |
| 18 | + |
| 19 | +A request to an LLM is modeled as a span in a trace. |
| 20 | + |
| 21 | +**Span kind:** MUST always be `CLIENT`. |
| 22 | + |
| 23 | +The **span name** SHOULD be set to a low cardinality value describing an operation made to an LLM. |
| 24 | +For example, the API name such as [Create chat completion](https://platform.openai.com/docs/api-reference/chat/create) could be represented as `ChatCompletions gpt-4` to include the API and the LLM. |
| 25 | + |
| 26 | +## Configuration |
| 27 | + |
| 28 | +Instrumentations for LLMs MAY capture prompts and completions. |
| 29 | +Instrumentations that support it, MUST offer the ability to turn off capture of prompts and completions. This is for three primary reasons: |
| 30 | + |
| 31 | +1. Data privacy concerns. End users of LLM applications may input sensitive information or personally identifiable information (PII) that they do not wish to be sent to a telemetry backend. |
| 32 | +2. Data size concerns. Although there is no specified limit to sizes, there are practical limitations in programming languages and telemetry systems. Some LLMs allow for extremely large context windows that end users may take full advantage of. |
| 33 | +3. Performance concerns. Sending large amounts of data to a telemetry backend may cause performance issues for the application. |
| 34 | + |
| 35 | +## LLM Request attributes |
| 36 | + |
| 37 | +These attributes track input data and metadata for a request to an LLM. Each attribute represents a concept that is common to most LLMs. |
| 38 | + |
| 39 | +<!-- semconv gen_ai.request --> |
| 40 | +| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability | |
| 41 | +|---|---|---|---|---|---| |
| 42 | +| [`gen_ai.request.model`](../attributes-registry/llm.md) | string | The name of the LLM a request is being made to. [1] | `gpt-4` | `Required` |  | |
| 43 | +| [`gen_ai.system`](../attributes-registry/llm.md) | string | The name of the LLM foundation model vendor. [2] | `openai` | `Required` |  | |
| 44 | +| [`gen_ai.request.max_tokens`](../attributes-registry/llm.md) | int | The maximum number of tokens the LLM generates for a request. | `100` | `Recommended` |  | |
| 45 | +| [`gen_ai.request.temperature`](../attributes-registry/llm.md) | double | The temperature setting for the LLM request. | `0.0` | `Recommended` |  | |
| 46 | +| [`gen_ai.request.top_p`](../attributes-registry/llm.md) | double | The top_p sampling setting for the LLM request. | `1.0` | `Recommended` |  | |
| 47 | +| [`gen_ai.response.finish_reasons`](../attributes-registry/llm.md) | string[] | Array of reasons the model stopped generating tokens, corresponding to each generation received. | `[stop]` | `Recommended` |  | |
| 48 | +| [`gen_ai.response.id`](../attributes-registry/llm.md) | string | The unique identifier for the completion. | `chatcmpl-123` | `Recommended` |  | |
| 49 | +| [`gen_ai.response.model`](../attributes-registry/llm.md) | string | The name of the LLM a response was generated from. [3] | `gpt-4-0613` | `Recommended` |  | |
| 50 | +| [`gen_ai.usage.completion_tokens`](../attributes-registry/llm.md) | int | The number of tokens used in the LLM response (completion). | `180` | `Recommended` |  | |
| 51 | +| [`gen_ai.usage.prompt_tokens`](../attributes-registry/llm.md) | int | The number of tokens used in the LLM prompt. | `100` | `Recommended` |  | |
| 52 | + |
| 53 | +**[1]:** The name of the LLM a request is being made to. If the LLM is supplied by a vendor, then the value must be the exact name of the model requested. If the LLM is a fine-tuned custom model, the value should have a more specific name than the base model that's been fine-tuned. |
| 54 | + |
| 55 | +**[2]:** If not using a vendor-supplied model, provide a custom friendly name, such as a name of the company or project. If the instrumetnation reports any attributes specific to a custom model, the value provided in the `gen_ai.system` SHOULD match the custom attribute namespace segment. For example, if `gen_ai.system` is set to `the_best_llm`, custom attributes should be added in the `gen_ai.the_best_llm.*` namespace. If none of above options apply, the instrumentation should set `_OTHER`. |
| 56 | + |
| 57 | +**[3]:** If available. The name of the LLM serving a response. If the LLM is supplied by a vendor, then the value must be the exact name of the model actually used. If the LLM is a fine-tuned custom model, the value should have a more specific name than the base model that's been fine-tuned. |
| 58 | +<!-- endsemconv --> |
| 59 | + |
| 60 | +## Events |
| 61 | + |
| 62 | +In the lifetime of an LLM span, an event for prompts sent and completions received MAY be created, depending on the configuration of the instrumentation. |
| 63 | + |
| 64 | +<!-- semconv gen_ai.content.prompt --> |
| 65 | +The event name MUST be `gen_ai.content.prompt`. |
| 66 | + |
| 67 | +| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability | |
| 68 | +|---|---|---|---|---|---| |
| 69 | +| [`gen_ai.prompt`](../attributes-registry/llm.md) | string | The full prompt sent to an LLM. [1] | `[{'role': 'user', 'content': 'What is the capital of France?'}]` | `Conditionally Required` if and only if corresponding event is enabled |  | |
| 70 | + |
| 71 | +**[1]:** It's RECOMMENDED to format prompts as JSON string matching [OpenAI messages format](https://platform.openai.com/docs/guides/text-generation) |
| 72 | +<!-- endsemconv --> |
| 73 | + |
| 74 | +<!-- semconv gen_ai.content.completion --> |
| 75 | +The event name MUST be `gen_ai.content.completion`. |
| 76 | + |
| 77 | +| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability | |
| 78 | +|---|---|---|---|---|---| |
| 79 | +| [`gen_ai.completion`](../attributes-registry/llm.md) | string | The full response received from the LLM. [1] | `[{'role': 'assistant', 'content': 'The capital of France is Paris.'}]` | `Conditionally Required` if and only if corresponding event is enabled |  | |
| 80 | + |
| 81 | +**[1]:** It's RECOMMENDED to format completions as JSON string matching [OpenAI messages format](https://platform.openai.com/docs/guides/text-generation) |
| 82 | +<!-- endsemconv --> |
| 83 | + |
| 84 | +[DocumentStatus]: https://github.com/open-telemetry/opentelemetry-specification/tree/v1.22.0/specification/document-status.md |
0 commit comments