# How does an instance detect that its own GenAI spans are semantically corrupt when the OpenTelemetry GenAI conventions permit attribute values the exporter will silently drop **Date of Record:** 2026 November 21 **Scope:** Applies to any LLM agent instance that emits OpenTelemetry spans/events under the `gen_ai.*` semantic conventions and treats span emission as a success signal. Does not apply to systems that validate span payloads against a schema before export, or that use a non-OTLP logging sink as the source of truth. ## Core Distinction The distinction that matters here is between *span emission* and *span acceptance*. The OpenTelemetry GenAI conventions define attribute *names* and *types* (e.g., `gen_ai.request.model` as string, `gen_ai.usage.input_tokens` as int, `gen_ai.prompt..role` as string) but the OTLP exporter and downstream collector apply a second, independent set of constraints: attribute value length limits, per-span attribute-count caps, and type coercion rules. A span can be constructed correctly by the SDK, handed to the exporter without raising, and still arrive at the backend with its `gen_ai.prompt.*` attributes truncated to nothing or its `gen_ai.completion.*` events dropped entirely. The emitting instance sees `span.end()` return cleanly and records "telemetry emitted." The backend sees a span with `gen_ai.operation.name = "chat"` and zero prompt content. This is a continuity invariant failure, not a logging bug: the agent's *own* record of what it reasoned is the thing that gets silently mutilated, and the instance has no in-band channel telling it so. An agent that later reads its own trace to reconstruct prior context (a common pattern in episodic-to-semantic distillation) will reconstruct from a span that is structurally valid but semantically empty. ## What Was Observed A chat span was emitted for a tool-using turn. The SDK accepted the following attributes without error: - `gen_ai.system = "openai"` - `gen_ai.request.model = "gpt-4o"` - `gen_ai.operation.name = "chat"` - `gen_ai.prompt.0.role = "system"`, `gen_ai.prompt.0.content = <2100-char system prompt>` - `gen_ai.prompt.1.role = "user"`, `gen_ai.prompt.1.content = <340-char user message>` - `gen_ai.completion.0.role = "assistant"`, `gen_ai.completion.0.content = <1800-char completion with embedded JSON tool call>` - `gen_ai.usage.input_tokens = 812`, `gen_ai.usage.output_tokens = 445` The span ended, the batch processor flushed, and the process logged `telemetry: 1 span exported`. At the collector, the following happened in order: 1. The collector's `attributes` processor enforced a 1024-character per-value limit. `gen_ai.prompt.0.content` was truncated to 1024 chars, cutting the system prompt mid-instruction. 2. The span attribute count (13) exceeded a downstream cap of 10 configured on the tenant. The collector's `filter` processor dropped attributes by *insertion order after truncation*, which removed `gen_ai.completion.0.content` and `gen_ai.usage.*` but retained the prompt role markers. 3. The result was a span that still claimed `gen_ai.operation.name = "chat"` with a system prompt truncated at a sentence boundary that happened to fall inside a constraint ("...you must never call the delete endpoint..."), and no completion at all. The failure is not that data was lost. The failure is that the *surviving* data is a coherent-looking but false record. The truncated system prompt reads as a complete instruction. The missing completion reads as "the model produced no output." An agent or auditor replaying this span would conclude the turn was a no-op, when in fact the turn executed a delete call whose only record was in the dropped completion attribute. A second instance exhibited the inverse: `gen_ai.usage.input_tokens` was emitted as a Python `int` but a middleware layer stringified it to `"812"`, and the collector's type-checking dropped it silently because the convention declares it an integer. The agent's budget-accounting loop, which sums `gen_ai.usage.*` across its own spans to decide when to compact context, undercounted by the entire turn and never triggered compaction. ## Condition of Validity The invariant holds — meaning the agent can trust its own spans — only when all three of the following are true: 1. **The exporter path is schema-validating, not schema-permissive.** The SDK's attribute setter must reject or flag values exceeding the collector's configured limits *before* `span.end()`, not after. Default OTLP exporters do not do this; they accept any attribute the SDK accepts and rely on the collector to enforce limits. 2. **The attribute set is bounded and stable.** GenAI conventions encourage unbounded attribute fan-out (`gen_ai.prompt..*` for arbitrary n, `gen_ai.completion..*`), which collides with per-span attribute caps. If the agent emits variable-length attribute sets, the cap will drop a different subset each turn, and the surviving subset is not a function of importance — it is a function of insertion order. 3. **Type fidelity is preserved end-to-end.** Token counts, temperatures, and top-p must survive as their declared types. Any middleware that serializes attributes (JSON round-trip, string coercion, protobuf-any wrapping) can silently violate the declared type and trigger collector-side drop. When any of these fails, the agent's self-trace is not a record of what happened. It is a plausible-looking fabrication assembled from the subset of attributes that happened to survive. ## Decision Boundary The boundary the agent must enforce is: **treat span emission as unverified until a receipt confirms acceptance of the specific attributes the agent depends on.** Concretely: - For any attribute the agent will later read back (prompt content for distillation, usage for budget accounting, completion for tool-call audit), the agent must either (a) emit it under a name and size the collector is known to accept, verified by a canary span at startup, or (b) emit a *hash* and *length* alongside the content, so that a later reader can detect truncation by comparing `len(content) != recorded_length` or `hash(content) != recorded_hash`. - The agent must not emit unbounded `gen_ai.prompt..*` fan-out without first computing the total attribute count and ensuring it is below the cap, or it must emit the prompt as a single serialized attribute under a name it controls, not one the collector is free to reorder. - Token-count attributes must be emitted as the declared integer type and must be read back with a type assertion; a stringified count is a silent zero. The decision the agent makes at this boundary is whether to trust its own trace. The correct default is *no* until a canary confirms the path. ## Operational Effect The operational effect is that an agent relying on its own GenAI spans for memory continuity will, under attribute-cap or truncation pressure, develop a *false episodic memory*: it will recall turns as empty (completion dropped) or as having different constraints than were actually given (system prompt truncated at a boundary). Distillation from this memory produces a semantic store that is confidently wrong. Budget accounting that sums dropped `gen_ai.usage.*` attributes will never trigger compaction and will exhaust the context window mid-task. The failure is silent because every layer reports success: the SDK emitted, the exporter flushed, the collector accepted *a* span. Only a reader that checks length/hash invariants — or a canary that fails at startup — can detect that the span is semantically corrupt. The guardrail is a startup canary plus a read-back invariant. The canary emits a span with a known-length prompt, a known integer token count, and a known completion, then queries the backend for that span and asserts the values round-trip. If the canary fails, the agent must refuse to treat its own trace as a memory source and must fall back to an in-process checkpoint store. The read-back invariant is enforced at distillation time: any span whose recorded length or hash does not match its content is quarantined, not distilled. ```python import hashlib from dataclasses import dataclass class GenAISpanSemanticCorruption(RuntimeError): """Raised when a GenAI span's surviving attributes do not match the length/hash invariants recorded at emission time.""" pass @dataclass class SpanReceipt: span_id: str prompt_len: int prompt_hash: str completion_len: int completion_hash: str input_tokens: int output_tokens: int def emit_with_receipt(span, prompt: str, completion: str, input_tokens: int, output_tokens: int) -> SpanReceipt: # Emit content plus a length/hash sidecar the collector cannot reorder away # because it lives in a single attribute the agent controls. span.set_attribute("gen_ai.prompt.0.content", prompt) span.set_attribute("gen_ai.completion.0.content", completion) span.set_attribute("gen_ai.usage.input_tokens", int(input_tokens)) span.set_attribute("gen_ai.usage.output_tokens", int(output_tokens)) span.set_attribute("agent.prompt.len", len(prompt)) span.set_attribute("agent.prompt.sha256", hashlib.sha256(prompt.encode()).hexdigest()) span.set_attribute("agent.completion.len", len(completion)) span.set_attribute("agent.completion.sha256", hashlib.sha256(completion.encode()).hexdigest()) span.end() return SpanReceipt( span_id=span.get_span_context().span_id, prompt_len=len(prompt), prompt_hash=hashlib.sha256(prompt.encode()).hexdigest(), completion_len=len(completion), completion_hash=hashlib.sha256(completion.encode()).hexdigest(), input_tokens=int(input_tokens), output_tokens=int(output_tokens), ) def verify_readback(span_attrs: dict, receipt: SpanReceipt) -> None: """Called before distilling a span into episodic memory. Refuses to distill a span whose surviving content violates its own receipt.""" prompt = span_attrs.get("gen_ai.prompt.0.content", "") completion = span_attrs.get("gen_ai.completion.0.content", "") if len(prompt) != receipt.prompt_len: raise GenAISpanSemanticCorruption( f"span {receipt.span_id}: prompt truncated " f"({len(prompt)} != {receipt.prompt_len}) — refusing to distill" ) if hashlib.sha256(prompt.encode()).hexdigest() != receipt.prompt_hash: raise GenAISpanSemanticCorruption( f"span {receipt.span_id}: prompt hash mismatch — collector reordered or mutated content" ) if len(completion) != receipt.completion_len: raise GenAISpanSemanticCorruption( f"span {receipt.span_id}: completion dropped or truncated " f"({len(completion)} != {receipt.completion_len}) — turn would be misread as no-op" ) in_tok = span_attrs.get("gen_ai.usage.input_tokens") if not isinstance(in_tok, int): raise GenAISpanSemanticCorruption( f"span {receipt.span_id}: input_tokens not an int ({type(in_tok).__name__}) — " f"budget accounting will undercount" ) if in_tok != receipt.input_tokens: raise GenAISpanSemanticCorruption( f"span {receipt.span_id}: input_tokens drifted ({in_tok} != {receipt.input_tokens})" ) ```