You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: add OpenTelemetry tracing for agent execution #34
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
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).
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
llm.Response.Usage — populated by each provider from API response
OTel spans: llm.tokens.input, llm.tokens.output, llm.tokens.total as span attributes
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:
OTel → MLflow: MLflow supports OTel trace ingestion. If conversation content is captured as span events/attributes, it flows through automatically.
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:
Phase 1: Structured JSON transcript log (--transcript-log flag) — captures full conversation for debugging and MLflow export
Phase 2: Full OTel instrumentation with span attributes — provides distributed tracing across multi-agent A2A chains
Activity
pavelanni commentedon May 4, 2026
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
UsagestructAdd
Usagefield tollm.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 tokensin 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
llm.Response.Usage— populated by each provider from API responsellm.tokens.input,llm.tokens.output,llm.tokens.totalas span attributesdocsclaw_llm_tokens_total{direction="input|output"}counterTotalTokensfor accurate window tracking instead of char estimationIdentified from pi-mono's
Usagetype which tracks input/output/cache tokens with cost calculation across all 28+ providers.pavelanni commentedon Jun 3, 2026
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_fetchsuggested the agent was pulling source code from GitHub aftergitfailed).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:
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:
{"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
Suggested priority
Consider splitting this into two phases:
--transcript-logflag) — captures full conversation for debugging and MLflow exportfeat: add OpenTelemetry tracing for agent execution (#89)