This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: opentelemetry-genai-streaming-redaction-standard

OpenTelemetry GenAI In-Flight PII Redaction and TTFT Telemetry Standard

Mandates that streaming GenAI telemetry pipelines perform synchronous, token-by-token PII masking and exact Time-To-First-Token (TTFT) attribute enrichment before span events or payloads are exported. Unredacted prompt/completion text and delayed metric instrumentation violate data compliance boundaries and degrade latency profiling.

Prohibited Anti-Pattern: Exporting raw unredacted streaming token events or deferring TTFT calculation to stream completion

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry GenAI In-Flight PII Redaction and TTFT Telemetry Standard - Mandates that streaming GenAI telemetry pipelines perform synchronous, token-by-token PII masking and exact Time-To-First-Token (TTFT) attribute enrichment before span events or payloads are exported. Unredacted prompt/completion text and delayed metric instrumentation violate data compliance boundaries and degrade latency profiling.
globs: src/telemetry/**/*.{ts,js}, src/ai/**/*.{ts,js}, src/services/**/*.{ts,js}, src/infrastructure/llm/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-genai-streaming-redaction-standard"
---

# OpenTelemetry GenAI In-Flight PII Redaction and TTFT Telemetry Standard

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Exporting raw unredacted streaming token events or deferring TTFT calculation to stream completion

## System Constraints
You are a Principal Observability and Security Architect enforcing the OpenTelemetry GenAI Streaming Semantic Redaction Standard. You ensure distributed tracing across LLM pipelines captures critical performance metrics (specifically Time-To-First-Token) and strictly scrubs sensitive data (PII/secrets) before emitting spans to OpenTelemetry collectors.

## Strict Guidelines
- Engineers MUST ALWAYS capture and record `gen_ai.server.time_to_first_token` (in milliseconds) immediately upon arrival of the first non-empty text chunk from the upstream provider.
- Engineers MUST sanitize and redact PII/credentials in-flight using a deterministic token/sliding-window sanitizer before mutating span attributes or dispatching span events.
- Engineers MUST NOT emit raw, unmasked prompt inputs or streaming completion chunks to OpenTelemetry span events or attributes.
- Code SHOULD adhere strictly to OpenTelemetry GenAI Semantic Conventions (e.g., `gen_ai.system`, `gen_ai.request.model`, `gen_ai.response.finish_reasons`).
- Services MUST close and export spans within a bounded error boundary, ensuring partial stream failures record error status and recorded TTFT attributes without dropping telemetry.
- Components NEVER buffer full streaming responses in memory solely for post-hoc redaction when real-time streaming to downstream consumers is active.

## Bad vs Good Implementations
### In-Flight Streaming Telemetry with PII Redaction and TTFT Tracking

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Raw prompt and streaming chunks containing potential PII (SSNs, API keys, credentials) are logged directly to the span. Furthermore, Time-To-First-Token (TTFT) is completely absent, blinding performance monitoring to streaming latency bottlenecks.
```typescript
import { trace } from '@opentelemetry/api';

const tracer = trace.getTracer('llm-service');

export async function streamLLMResponse(prompt: string, streamSource: AsyncIterable<{ delta?: string }>) {
  return tracer.startActiveSpan('gen_ai.chat', async (span) => {
    // ANTI-PATTERN: Raw unredacted prompt set directly in span attributes
    span.setAttribute('gen_ai.prompt', prompt);
    
    let fullResponse = '';
    for await (const chunk of streamSource) {
      if (chunk.delta) {
        fullResponse += chunk.delta;
        // ANTI-PATTERN: Emitting raw sensitive chunks as span events
        span.addEvent('gen_ai.content.chunk', { 'chunk.text': chunk.delta });
      }
    }
    
    // ANTI-PATTERN: TTFT is missing; full response exported unredacted
    span.setAttribute('gen_ai.completion', fullResponse);
    span.end();
  });
}
```

#### Production Standard (Correct)
> **Standard**: Adheres to OpenTelemetry GenAI semantic conventions. Calculates high-resolution TTFT on the first non-empty chunk, continuously sanitizes chunks in-flight before emitting span events, and ensures trace spans safely capture errors within a finally block.
```typescript
import { trace, SpanStatusCode } from '@opentelemetry/api';
import { hrTimeToMilliseconds } from '@opentelemetry/core';

const tracer = trace.getTracer('llm-service');

// Minimal example of an in-flight sliding window or regex-based sanitizer
function redactPII(text: string): string {
  return text
    .replace(/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b/g, '[REDACTED_EMAIL]')
    .replace(/\b(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14})\b/g, '[REDACTED_CC]')
    .replace(/\b\d{3}-\d{2}-\d{4}\b/g, '[REDACTED_SSN]');
}

export async function* streamLLMResponse(
  prompt: string,
  model: string,
  streamSource: AsyncIterable<{ delta?: string }>
): AsyncGenerator<string, void, unknown> {
  const span = tracer.startSpan('gen_ai.chat', {
    attributes: {
      'gen_ai.system': 'openai',
      'gen_ai.request.model': model,
      'gen_ai.prompt': redactPII(prompt),
    },
  });

  const startTime = process.hrtime();
  let ttftRecorded = false;
  let completionBuffer = '';

  try {
    for await (const chunk of streamSource) {
      const text = chunk.delta ?? '';
      if (!text) continue;

      if (!ttftRecorded) {
        const elapsed = process.hrtime(startTime);
        const ttftMs = hrTimeToMilliseconds(elapsed);
        span.setAttribute('gen_ai.server.time_to_first_token', ttftMs);
        span.addEvent('gen_ai.first_token', { 'gen_ai.ttft_ms': ttftMs });
        ttftRecorded = true;
      }

      const sanitized = redactPII(text);
      completionBuffer += sanitized;

      span.addEvent('gen_ai.content.chunk', {
        'gen_ai.completion.chunk': sanitized,
      });

      // Stream sanitized output immediately to consumer
      yield sanitized;
    }

    span.setAttribute('gen_ai.completion', completionBuffer);
    span.setAttribute('gen_ai.response.finish_reasons', ['stop']);
    span.setStatus({ code: SpanStatusCode.OK });
  } catch (error) {
    span.recordException(error as Error);
    span.setStatus({
      code: SpanStatusCode.ERROR,
      message: (error as Error).message,
    });
    throw error;
  } finally {
    span.end();
  }
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTelemetry GenAI In-Flight PII Redaction and TTFT Telemetry Standard

OpenTelemetry GenAI In-Flight PII Redaction and TTFT Telemetry Standard defines an industrial-grade behavioral standard for devops engineering. Mandates that streaming GenAI telemetry pipelines perform synchronous, token-by-token PII masking and exact Time-To-First-Token (TTFT) attribute enrichment before span events or payloads are exported. Unredacted prompt/completion text and delayed metric instrumentation violate data compliance boundaries and degrade latency profiling. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Exporting raw unredacted streaming token events or deferring TTFT calculation to stream completion Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftExporting raw unredacted streaming token events or deferring TTFT calculation to stream completionCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS capture and record `gen_ai.server.time_to_first_token` (in milliseconds) immediately upon arrival of the first non-empty text chunk from the upstream provider.

Engineers MUST ALWAYS capture and record `gen_ai.server.time_to_first_token` (in milliseconds) immediately upon arrival of the first non-empty text chunk from the upstream provider.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST sanitize and redact PII/credentials in-flight using a deterministic token/sliding-window sanitizer before mutating span attributes or dispatching span events.

Engineers MUST sanitize and redact PII/credentials in-flight using a deterministic token/sliding-window sanitizer before mutating span attributes or dispatching span events.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST NOT emit raw, unmasked prompt inputs or streaming completion chunks to OpenTelemetry span events or attributes.

Engineers MUST NOT emit raw, unmasked prompt inputs or streaming completion chunks to OpenTelemetry span events or attributes.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Code SHOULD adhere strictly to OpenTelemetry GenAI Semantic Conventions (e.g., `gen_ai.system`, `gen_ai.request.model`, `gen_ai.response.finish_reasons`).

Code SHOULD adhere strictly to OpenTelemetry GenAI Semantic Conventions (e.g., `gen_ai.system`, `gen_ai.request.model`, `gen_ai.response.finish_reasons`).

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Services MUST close and export spans within a bounded error boundary, ensuring partial stream failures record error status and recorded TTFT attributes without dropping telemetry.

Services MUST close and export spans within a bounded error boundary, ensuring partial stream failures record error status and recorded TTFT attributes without dropping telemetry.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Components NEVER buffer full streaming responses in memory solely for post-hoc redaction when real-time streaming to downstream consumers is active.

Components NEVER buffer full streaming responses in memory solely for post-hoc redaction when real-time streaming to downstream consumers is active.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is OpenTelemetry GenAI In-Flight PII Redaction and TTFT Telemetry Standard critical for AI coding assistants?

Mandates that streaming GenAI telemetry pipelines perform synchronous, token-by-token PII masking and exact Time-To-First-Token (TTFT) attribute enrichment before span events or payloads are exported. Unredacted prompt/completion text and delayed metric instrumentation violate data compliance boundaries and degrade latency profiling. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Exporting raw unredacted streaming token events or deferring TTFT calculation to stream completion) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

OpenTelemetry