This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: opentelemetry-multi-agent-tool-call-context-lineage-invariant

Deterministic OpenTelemetry Context Lineage Across Multi-Agent Tool Calls

Preserves distributed trace context and deterministic causal parentage across autonomous, non-deterministic agentic LLM tool execution graphs. Mandates explicit W3C TraceContext propagation across asynchronous dispatch loops, sub-agent handoffs, and side-effecting tool boundaries without relying on fragile thread-local or Node.js implicit AsyncLocalStorage boundaries.

Prohibited Anti-Pattern: Orphaned spans and severed trace DAGs caused by ambient context loss in multi-agent tool loops

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Deterministic OpenTelemetry Context Lineage Across Multi-Agent Tool Calls - Preserves distributed trace context and deterministic causal parentage across autonomous, non-deterministic agentic LLM tool execution graphs. Mandates explicit W3C TraceContext propagation across asynchronous dispatch loops, sub-agent handoffs, and side-effecting tool boundaries without relying on fragile thread-local or Node.js implicit AsyncLocalStorage boundaries.
globs: src/**/agents/**/*.{ts,tsx}, src/**/tools/**/*.{ts,tsx}, src/**/orchestrator/**/*.{ts,tsx}, src/**/telemetry/**/*.{ts,tsx}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-multi-agent-tool-call-context-lineage-invariant"
---

# Deterministic OpenTelemetry Context Lineage Across Multi-Agent Tool Calls

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Orphaned spans and severed trace DAGs caused by ambient context loss in multi-agent tool loops

## System Constraints
You are a Principal Software Architect enforcing OpenTelemetry lineage invariants across multi-agent execution graphs. You MUST ensure that every autonomous agent dispatch, tool execution invocation, and nested sub-agent iteration deterministically carries forward W3C traceparent headers and retains strict causal parentage. Never permit unlinked detached tasks, unpropagated promises in background tool execution, or uninstrumented dynamic tool dispatch.

## Strict Guidelines
- Engineers MUST ALWAYS extract and inject W3C traceparent headers across autonomous agent-to-agent delegator boundaries and external tool RPC interfaces.
- Engineers MUST wrap all LLM tool execution invocations within `context.with(extractedContext, ...)` to maintain unbroken span lineages during asynchronous dispatch loops.
- Code SHOULD link spans via OpenTelemetry Span Links when tool calls execute concurrently or out-of-order in non-deterministic speculative execution DAGs.
- Services MUST record tool call arguments, execution output, model identifiers, and error statuses as semantic OpenTelemetry GenAI attributes (`gen_ai.system`, `gen_ai.tool.name`, `gen_ai.tool.call_id`).
- Components NEVER execute side-effecting background tool promises without an active span and deterministic root parent lineage.

## Bad vs Good Implementations
### Context Preservation in Dynamic Multi-Agent Tool Call Dispatch

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Implicit AsyncLocalStorage context fails across event loops, sub-agent microtask transitions, and tool queues. If a tool call is deferred or batched, it attaches to whatever ambient context is present (or none), orphaning the span from the root agent decision cycle.
```typescript
import { trace } from '@opentelemetry/api';

interface ToolCallPayload {
  id: string;
  name: string;
  parameters: Record<string, unknown>;
}

export async function executeAgentToolCall(
  tool: (params: Record<string, unknown>) => Promise<unknown>,
  call: ToolCallPayload
): Promise<unknown> {
  const tracer = trace.getTracer('agent-orchestrator');
  // ANTI-PATTERN: Relies on ambient async context which drops parentage across decoupled event queues
  return tracer.startActiveSpan(`tool:${call.name}`, async (span) => {
    try {
      const result = await tool(call.parameters);
      span.end();
      return result;
    } catch (err: unknown) {
      span.recordException(err as Error);
      span.end();
      throw err;
    }
  });
}
```

#### Production Standard (Correct)
> **Standard**: Deterministically extracts carrier metadata using OpenTelemetry's propagation API, creates an explicit child span under the caller's extracted context, and passes down the contextual reference, preventing trace bifurcation across decoupled agent handoffs.
```typescript
import { context, propagation, trace, SpanStatusCode, SpanKind, Context } from '@opentelemetry/api';

interface ToolCallPayload {
  id: string;
  name: string;
  parameters: Record<string, unknown>;
  traceCarrier: Record<string, string>;
}

export async function executeAgentToolCall(
  tool: (params: Record<string, unknown>, parentContext: Context) => Promise<unknown>,
  call: ToolCallPayload
): Promise<unknown> {
  const tracer = trace.getTracer('agent-orchestrator');
  
  // INVARIANT: Explicitly extract remote W3C trace context from the tool call payload carrier
  const extractedContext = propagation.extract(context.active(), call.traceCarrier);

  return context.with(extractedContext, async () => {
    const activeSpan = tracer.startSpan(
      `gen_ai.tool.${call.name}`,
      {
        kind: SpanKind.INTERNAL,
        attributes: {
          'gen_ai.tool.name': call.name,
          'gen_ai.tool.call_id': call.id,
          'gen_ai.system': 'multi-agent-runtime'
        }
      },
      context.active()
    );

    const toolContext = trace.setSpan(context.active(), activeSpan);

    try {
      const result = await tool(call.parameters, toolContext);
      activeSpan.setStatus({ code: SpanStatusCode.OK });
      return result;
    } catch (err) {
      activeSpan.recordException(err as Error);
      activeSpan.setStatus({
        code: SpanStatusCode.ERROR,
        message: err instanceof Error ? err.message : String(err)
      });
      throw err;
    } finally {
      activeSpan.end();
    }
  });
}
```

Recommended MCP Servers (mcp-serverlist.com)

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

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Deterministic OpenTelemetry Context Lineage Across Multi-Agent Tool Calls

Deterministic OpenTelemetry Context Lineage Across Multi-Agent Tool Calls defines an industrial-grade behavioral standard for devops engineering. Preserves distributed trace context and deterministic causal parentage across autonomous, non-deterministic agentic LLM tool execution graphs. Mandates explicit W3C TraceContext propagation across asynchronous dispatch loops, sub-agent handoffs, and side-effecting tool boundaries without relying on fragile thread-local or Node.js implicit AsyncLocalStorage boundaries. 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: Orphaned spans and severed trace DAGs caused by ambient context loss in multi-agent tool loops 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 DriftOrphaned spans and severed trace DAGs caused by ambient context loss in multi-agent tool loopsCritical
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 extract and inject W3C traceparent headers across autonomous agent-to-agent delegator boundaries and external tool RPC interfaces.

Engineers MUST ALWAYS extract and inject W3C traceparent headers across autonomous agent-to-agent delegator boundaries and external tool RPC interfaces.

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 wrap all LLM tool execution invocations within `context.with(extractedContext, ...)` to maintain unbroken span lineages during asynchronous dispatch loops.

Engineers MUST wrap all LLM tool execution invocations within `context.with(extractedContext, ...)` to maintain unbroken span lineages during asynchronous dispatch loops.

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

Code SHOULD link spans via OpenTelemetry Span Links when tool calls execute concurrently or out-of-order in non-deterministic speculative execution DAGs.

Code SHOULD link spans via OpenTelemetry Span Links when tool calls execute concurrently or out-of-order in non-deterministic speculative execution DAGs.

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 record tool call arguments, execution output, model identifiers, and error statuses as semantic OpenTelemetry GenAI attributes (`gen_ai.system`, `gen_ai.tool.name`, `gen_ai.tool.call_id`).

Services MUST record tool call arguments, execution output, model identifiers, and error statuses as semantic OpenTelemetry GenAI attributes (`gen_ai.system`, `gen_ai.tool.name`, `gen_ai.tool.call_id`).

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 side-effecting background tool promises without an active span and deterministic root parent lineage.

Components NEVER execute side-effecting background tool promises without an active span and deterministic root parent lineage.

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 Deterministic OpenTelemetry Context Lineage Across Multi-Agent Tool Calls critical for AI coding assistants?

Preserves distributed trace context and deterministic causal parentage across autonomous, non-deterministic agentic LLM tool execution graphs. Mandates explicit W3C TraceContext propagation across asynchronous dispatch loops, sub-agent handoffs, and side-effecting tool boundaries without relying on fragile thread-local or Node.js implicit AsyncLocalStorage boundaries. 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 (Orphaned spans and severed trace DAGs caused by ambient context loss in multi-agent tool loops) 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

Deterministic