# OpenTelemetry GenAI Semantic Conventions (`gen_ai.*`) Cheat Sheet

Companion resource from **AI Agent Observability** by Dany Paredes ([danywalls.com/book](https://danywalls.com/book)).

---

## 1. Standard Span Names & Span Kinds

Use these standard span names so any OTel-compatible backend (Progress Observability Platform, LangSmith OTLP, Datadog, Honeycomb, Jaeger, Grafana Tempo) understands your AI agent natively:

| Operation | Standard Span Name | `SpanKind` | `gen_ai.operation.name` |
| :--- | :--- | :--- | :--- |
| Agent execution (root) | `invoke_agent {agent_name}` | `INTERNAL` or `SERVER` | `invoke_agent` |
| LLM / Chat completion | `chat {model_name}` | `CLIENT` | `chat` |
| Tool / Function call | `execute_tool {tool_name}` | `INTERNAL` | `execute_tool` |
| Vector / Embedding call | `embeddings {model_name}` | `CLIENT` | `embeddings` |

---

## 2. Core `gen_ai.*` Attributes

### Model & Request Attributes (Required / Recommended)
- `gen_ai.provider.name` (or `gen_ai.system`): `"openai"`, `"anthropic"`, `"google_genai"`, `"vertex_ai"`
- `gen_ai.operation.name`: `"invoke_agent"`, `"chat"`, `"execute_tool"`
- `gen_ai.request.model`: `"gpt-4o-mini"`, `"claude-3-7-sonnet"`, `"gemini-3-flash-preview"`
- `gen_ai.request.temperature`: `0` to `1` (use `0` for LLM-as-a-Judge)
- `gen_ai.request.max_tokens`: hard output token cap

### Response & Cost Attributes
- `gen_ai.response.model`: actual model version served
- `gen_ai.response.finish_reasons`: `["stop"]`, `["tool_calls"]`, `["length"]`
- `gen_ai.usage.input_tokens`: prompt + message history + tool definitions
- `gen_ai.usage.output_tokens`: generated text + tool call arguments
- `gen_ai.usage.cache_read.input_tokens`: tokens served from prompt prefix cache

### Agent, Thread & Tool Attributes
- `gen_ai.agent.name`: `"supportAgent"`
- `gen_ai.conversation.id`: `thread_id` (groups multi-turn user chat sessions)
- `gen_ai.tool.name`: `"getOrderStatus"`
- `gen_ai.tool.call.id`: unique tool call identifier

---

## 3. Cross-Stack Mapping: One Agent, Four Pillars

| Concept | Firebase Genkit | LangChain + LangSmith | OpenTelemetry (`gen_ai.*`) | Progress Observability (`@progress/observability`) |
| :--- | :--- | :--- | :--- | :--- |
| Agent Entry Point | `ai.defineFlow(...)` | `traceable(...)` / `createAgent` | `invoke_agent` root span | `instrument(...)` / OTLP root trace |
| LLM Call | `ai.generate(...)` | `ChatModel` (`llm` run) | `chat` child span | `chat` span + USD cost enrichment |
| Tool Execution | `ai.defineTool(...)` | `tool(...)` (`tool` run) | `execute_tool` child span | `execute_tool` span + latency breakdown |
| Internal Logic Step | `ai.run('stepName', ...)` | Nested `traceable(...)` | `tracer.startActiveSpan(...)` | Business span + custom attributes |
| Multi-turn Session | `sessionId` / metadata | `metadata.thread_id` | `gen_ai.conversation.id` | Session / thread grouping & FinOps view |

---

## 4. Production Privacy & Export Rules

1. **Opt-in message content:** Never capture raw prompts or completions in production unless explicitly masked. Set `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=false` by default and redact PII before export.
2. **Flag logical tool errors:** If a tool returns `{ applied: false, errorCode: "INVALID_CODE" }` instead of throwing, call `span.setStatus({ code: SpanStatusCode.ERROR, message: "INVALID_CODE" })` so dashboards do not report a false 0% error rate.
3. **Always use `BatchSpanProcessor` in production:** With `@opentelemetry/sdk-trace` v3+, pass the object configuration `new BatchSpanProcessor({ exporter, maxQueueSize: 2048, exportTimeoutMillis: 10000 })`, and flush pending spans on `SIGTERM`.
