This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: otel-v1-28-genai-tracing-token-budgeting-standard

OpenTelemetry v1.28 GenAI Span Lineage & Distributed Token Budget Governance

Mandates strict adherence to OpenTelemetry Semantic Conventions v1.28 for GenAI operations across all multi-agent orchestration spans. Enforces deterministic prompt template hashing, pre-execution token budget validation, and standard semantic attribute emission to prevent unbounded operational spend and broken telemetry lineage.

Prohibited Anti-Pattern: Ad-Hoc LLM Invocations with Unmonitored Spans, Unhashed Prompts, and Unbounded Token Consumption

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry v1.28 GenAI Span Lineage & Distributed Token Budget Governance - Mandates strict adherence to OpenTelemetry Semantic Conventions v1.28 for GenAI operations across all multi-agent orchestration spans. Enforces deterministic prompt template hashing, pre-execution token budget validation, and standard semantic attribute emission to prevent unbounded operational spend and broken telemetry lineage.
globs: src/agents/**/*.ts, src/llm/**/*.ts, src/orchestration/**/*.ts, src/telemetry/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-v1-28-genai-tracing-token-budgeting-standard"
---

# OpenTelemetry v1.28 GenAI Span Lineage & Distributed Token Budget Governance

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Ad-Hoc LLM Invocations with Unmonitored Spans, Unhashed Prompts, and Unbounded Token Consumption

## System Constraints
You are a Principal Software Architect enforcing OpenTelemetry Semantic Conventions v1.28 GenAI Tracing and distributed token budget guardrails. You must reject any direct LLM client invocation that lacks an active GenAI telemetry span, bypasses deterministic prompt hash lineage calculation, or omits atomic token budget reservation checks.

## Strict Guidelines
- Engineers MUST ALWAYS wrap every LLM generation, embedding, or tool call in an OpenTelemetry span conforming to v1.28 semantic conventions using SpanKind.CLIENT or SpanKind.INTERNAL.
- Engineers MUST ALWAYS populate standard GenAI attributes: `gen_ai.system`, `gen_ai.operation.name`, `gen_ai.request.model`, `gen_ai.response.model`, `gen_ai.usage.input_tokens`, and `gen_ai.usage.output_tokens`.
- Engineers MUST NEVER record raw unmasked PII or sensitive proprietary inputs in span attributes; prompt content must be hashed into a deterministic lineage identifier (e.g., `gen_ai.prompt.hash` via SHA-256) unless explicitly operating in an isolated debug environment with scrubbing enabled.
- Services MUST perform pre-execution token budget checks against the distributed budget manager before triggering LLM inference and deduct actual reported tokens upon span completion.
- Components NEVER execute raw SDK client calls (e.g., `openai.chat.completions.create`) directly inside business logic without going through an instrumented, budget-aware LLM proxy client.

## Bad vs Good Implementations
### Unbounded Direct LLM Invocation vs. OTel v1.28 Instrumented & Budget-Enforced Execution

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The bad example calls the LLM provider directly without an OpenTelemetry span, violating OTel v1.28 GenAI conventions. There is no pre-flight token budget reservation, no prompt lineage hashing, and no recorded metrics for input/output token usage.
```typescript
import OpenAI from 'openai';

const client = new OpenAI();

export async function runAgentStep(prompt: string): Promise<string> {
  // ANTI-PATTERN: Direct invocation, no OTel span, missing GenAI semantic attributes,
  // zero token budgeting, and unhashed prompt leakage into uninstrumented logs.
  const response = await client.chat.completions.create({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: prompt }],
    temperature: 0.7,
  });

  return response.choices[0].message.content ?? '';
}
```

#### Production Standard (Correct)
> **Standard**: The good example wraps execution in a compliant OpenTelemetry v1.28 span, hashes the prompt lineage using SHA-256 to safeguard privacy while enabling distributed cache tracing, strictly enforces token budget limits prior to network calls, and records usage attributes.
```typescript
import { trace, SpanKind, SpanStatusCode } from '@opentelemetry/api';
import { createHash } from 'node:crypto';
import OpenAI from 'openai';
import { TokenBudgetExceededError, reserveTokenBudget, commitTokenUsage } from '../telemetry/budget';

const tracer = trace.getTracer('orchestrator.agent', '1.28.0');
const client = new OpenAI();

interface AgentExecutionOptions {
  sessionId: string;
  maxBudgetTokens: number;
}

export async function runAgentStep(
  prompt: string,
  options: AgentExecutionOptions
): Promise<string> {
  const promptHash = createHash('sha256').update(prompt).digest('hex');
  const model = 'gpt-4o';

  // Pre-execution token reservation guardrail
  const hasBudget = await reserveTokenBudget(options.sessionId, options.maxBudgetTokens);
  if (!hasBudget) {
    throw new TokenBudgetExceededError(`Session ${options.sessionId} exceeded token budget.`);
  }

  return tracer.startActiveSpan(
    `chat ${model}`,
    {
      kind: SpanKind.CLIENT,
      attributes: {
        'gen_ai.system': 'openai',
        'gen_ai.operation.name': 'chat',
        'gen_ai.request.model': model,
        'gen_ai.request.temperature': 0.7,
        'gen_ai.prompt.hash': promptHash,
        'session.id': options.sessionId,
      },
    },
    async (span) => {
      try {
        const response = await client.chat.completions.create({
          model,
          messages: [{ role: 'user', content: prompt }],
          temperature: 0.7,
        });

        const inputTokens = response.usage?.prompt_tokens ?? 0;
        const outputTokens = response.usage?.completion_tokens ?? 0;
        const responseModel = response.model ?? model;

        // Standard OTel v1.28 attribute population
        span.setAttributes({
          'gen_ai.response.model': responseModel,
          'gen_ai.response.finish_reasons': response.choices.map((c) => c.finish_reason ?? 'unknown'),
          'gen_ai.usage.input_tokens': inputTokens,
          'gen_ai.usage.output_tokens': outputTokens,
        });

        await commitTokenUsage(options.sessionId, inputTokens + outputTokens, options.maxBudgetTokens);
        span.setStatus({ code: SpanStatusCode.OK });
        return response.choices[0].message.content ?? '';
      } catch (error) {
        span.recordException(error as Error);
        span.setStatus({ code: SpanStatusCode.ERROR, message: (error as Error).message });
        throw error;
      } finally {
        span.end();
      }
    }
  );
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTelemetry v1.28 GenAI Span Lineage & Distributed Token Budget Governance

OpenTelemetry v1.28 GenAI Span Lineage & Distributed Token Budget Governance defines an industrial-grade behavioral standard for backend engineering. Mandates strict adherence to OpenTelemetry Semantic Conventions v1.28 for GenAI operations across all multi-agent orchestration spans. Enforces deterministic prompt template hashing, pre-execution token budget validation, and standard semantic attribute emission to prevent unbounded operational spend and broken telemetry lineage. 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: Ad-Hoc LLM Invocations with Unmonitored Spans, Unhashed Prompts, and Unbounded Token Consumption 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 DriftAd-Hoc LLM Invocations with Unmonitored Spans, Unhashed Prompts, and Unbounded Token ConsumptionCritical
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 wrap every LLM generation, embedding, or tool call in an OpenTelemetry span conforming to v1.28 semantic conventions using SpanKind.CLIENT or SpanKind.INTERNAL.

Engineers MUST ALWAYS wrap every LLM generation, embedding, or tool call in an OpenTelemetry span conforming to v1.28 semantic conventions using SpanKind.CLIENT or SpanKind.INTERNAL.

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 ALWAYS populate standard GenAI attributes

Engineers MUST ALWAYS populate standard GenAI attributes: `gen_ai.system`, `gen_ai.operation.name`, `gen_ai.request.model`, `gen_ai.response.model`, `gen_ai.usage.input_tokens`, and `gen_ai.usage.output_tokens`.

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 NEVER record raw unmasked PII or sensitive proprietary inputs in span attributes

Engineers MUST NEVER record raw unmasked PII or sensitive proprietary inputs in span attributes; prompt content must be hashed into a deterministic lineage identifier (e.g., `gen_ai.prompt.hash` via SHA-256) unless explicitly operating in an isolated debug environment with scrubbing enabled.

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

Services MUST perform pre-execution token budget checks against the distributed budget manager before triggering LLM inference and deduct actual reported tokens upon span completion.

Services MUST perform pre-execution token budget checks against the distributed budget manager before triggering LLM inference and deduct actual reported tokens upon span completion.

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

Components NEVER execute raw SDK client calls (e.g., `openai.chat.completions.create`) directly inside business logic without going through an instrumented, budget-aware LLM proxy client.

Components NEVER execute raw SDK client calls (e.g., `openai.chat.completions.create`) directly inside business logic without going through an instrumented, budget-aware LLM proxy client.

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 v1.28 GenAI Span Lineage & Distributed Token Budget Governance critical for AI coding assistants?

Mandates strict adherence to OpenTelemetry Semantic Conventions v1.28 for GenAI operations across all multi-agent orchestration spans. Enforces deterministic prompt template hashing, pre-execution token budget validation, and standard semantic attribute emission to prevent unbounded operational spend and broken telemetry lineage. 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 (Ad-Hoc LLM Invocations with Unmonitored Spans, Unhashed Prompts, and Unbounded Token Consumption) 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