Skip to content

feat: add OpenTelemetry tracing for agent execution #34

Closed

Description

@pavelanni

Summary

Add OpenTelemetry (OTel) instrumentation to DocsClaw so that agent execution — LLM calls, tool invocations, skill loading, and A2A delegation — produces distributed traces that can be viewed in any OTel-compatible backend (Jaeger, Tempo, Datadog, etc.).

Motivation

Observability is a gap compared to frameworks like Google ADK that provide built-in tracing. Rather than adopting a framework-specific tracing solution, OTel provides a universal standard that works with any backend and any agent framework in a multi-agent system.

This matters for:

  • Debugging: understanding why an agent took 30 seconds (was it the LLM call? a tool timeout? skill loading?)
  • Multi-agent visibility: tracing A2A delegation chains across agents
  • Production readiness: operators need to understand agent behavior without reading logs

Proposed instrumentation points

Component Span name Key attributes
pkg/tools/loop.go agent.loop.iteration iteration number, tool calls made
internal/anthropic/ llm.complete model, token count, stop reason
pkg/tools/ tool.execute.<name> tool name, duration, success/error
pkg/skills/ skill.load skill name, source (OCI/ConfigMap)
internal/bridge/ a2a.delegate target agent, message ID

Implementation notes

  • Use go.opentelemetry.io/otel SDK — lightweight, no heavy dependencies
  • Export via OTLP (gRPC or HTTP) configured through standard OTEL_EXPORTER_* env vars
  • Traces should propagate through A2A calls via W3C Trace Context headers
  • Keep it optional — if no exporter is configured, tracing is a no-op

Related

Activity

  1. pavelanni commented on May 4, 2026

    @pavelanni
    CollaboratorAuthor

    Addition: Token usage and cost tracking

    Beyond distributed tracing, we need per-request token accounting in llm.Response. This feeds into OTel span attributes, Prometheus metrics, context compaction (#5 Phase 3), and the eval harness (#35).

    Proposed Usage struct

    type Usage struct {
        InputTokens      int
        OutputTokens     int
        CacheReadTokens  int
        CacheWriteTokens int
        TotalTokens      int
    }

    Add Usage field to llm.Response. Both the Anthropic and OpenAI providers already receive this data in API responses — we just need to propagate it.

    Cost calculation (optional, Phase 2)

    Model pricing as $/million tokens in a registry. Calculate per-request cost from usage × pricing. This is lower priority than raw token counts but valuable for enterprise cost attribution.

    Integration points

    Identified from pi-mono's Usage type which tracks input/output/cache tokens with cost calculation across all 28+ providers.

  2. pavelanni commented on Jun 3, 2026

    @pavelanni
    CollaboratorAuthor

    Conversation transcript capture — additional requirement

    Today's deployment test exposed a concrete observability gap: when DocsClaw ran a security scan, the logs showed which tool was called and token counts, but not what arguments were passed or what the LLM responded. Debugging required guessing from indirect signals (e.g., token count jumps from 3K→25K after web_fetch suggested the agent was pulling source code from GitHub after git failed).

    What's missing beyond OTel spans

    The OTel instrumentation proposed above covers latency and structure (which spans took how long, what called what). But for agent behavior analysis, we also need content capture — the full messages exchanged with the LLM:

    Data point Current state Needed
    Tool name called ✅ Logged ✅
    Tool arguments ❌ Not logged In span attributes or trace events
    Tool result (or error) Partial (errors only) Full result (truncated for large outputs)
    LLM prompt messages ❌ Not logged As span events or separate export
    LLM response text ❌ Not logged As span events or separate export
    Skill content loaded ❌ Not logged Skill name + content hash

    MLflow / experiment tracking integration

    For comparing agent behavior across models, prompts, and skill versions, the conversation transcript should be exportable to experiment tracking tools like MLflow. Two approaches:

    1. OTel → MLflow: MLflow supports OTel trace ingestion. If conversation content is captured as span events/attributes, it flows through automatically.
    2. Structured JSON log: Emit a {"type":"llm_exchange", ...} JSONL stream to stdout alongside regular logs (or to a separate file via --transcript-log). This is simpler to implement and works with any log aggregation pipeline.

    Option 2 is lower-effort and could land before full OTel instrumentation. They're not mutually exclusive.

    Concrete scenario that triggered this

    # Agent tried git, apt-get, which, python3 — all missing from the lean container
    # Then pivoted to web_fetch to pull source code from GitHub
    # We could only infer this from token count jumps in the logs
    # With transcript capture, the actual URLs fetched and LLM reasoning would be visible
    

    Suggested priority

    Consider splitting this into two phases:

    1. Phase 1: Structured JSON transcript log (--transcript-log flag) — captures full conversation for debugging and MLflow export
    2. Phase 2: Full OTel instrumentation with span attributes — provides distributed tracing across multi-agent A2A chains
  3. added a commit that references this issue on Jun 4, 2026
    bdd8048
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions