Skip to content

Dev observability: emit agent turns as MLflow traces via OTLP (assembled prompt, tools, iterations) #1341

Closed

Description

@malibio

Overview

When debugging model behavior there is no way to see what the agent actually sent to the model. The daemon logs only the system prompt character length (system_prompt_len). The assembled prompt is built dynamically from graph nodes via PromptAssembler — it's non-trivial to reconstruct manually.

This gap came up concretely during the #1329 A/B trial: we needed to know whether 12B's failures were prompt or capability issues, but had to infer from source code rather than observing the actual assembled prompt and conversation sent to llama.cpp.

Problem Statement

  • agent_loop.rs logs system_prompt_len at INFO but never the content
  • The assembled prompt is composed from graph nodes at runtime — source code alone doesn't tell you what seeded nodes produced
  • Per-iteration ReAct messages, raw model output, and which response strippers fired are invisible
  • Without this, every model behavior investigation requires source archaeology rather than observation

Proposed Solution

Emit one MLflow trace per agent turn (opt-in, dev only) via OpenTelemetry OTLP. MLflow 3.x has a native OTLP HTTP receiver built into its tracking server.

Activation: NODESPACE_MLFLOW_URL=http://localhost:5000 env var. When unset: zero overhead, zero code paths exercised. No production behavior change.

Per-turn trace structure (nested spans):

  • Root span agent_turn: user message, session_id, model_id, total latency
    • Child span prompt_assembly: full assembled system prompt text, workspace_context injected, tool schemas offered (name + description)
    • Child span react_iteration_N per ReAct loop iteration: full message array sent to model, raw model output, tool calls parsed, tool results received
    • Child span response_processing: raw response in, normalized response out, which strippers fired and what they removed

Dev workflow this enables:

  • mlflow ui — browse traces visually, diff prompts across model runs
  • mlflow mcp run — query traces from Claude Code ("what did the agent see on turn 3 of the 12B run?")
  • mlflow traces search CLI — scripted analysis across a matrix run

Implementation Approach

Phase 1: Tracing infrastructure

  • Add opentelemetry, opentelemetry-otlp, opentelemetry-http to packages/agent/Cargo.toml, runtime-gated via env var check (no separate Cargo feature needed)
  • Add OtlpTracer helper in packages/agent/src/local_agent/ — initializes an OTLP exporter pointing at NODESPACE_MLFLOW_URL if set, otherwise returns a no-op tracer
  • Zero-cost when env var is absent: no-op tracer, span creation is elided at runtime

Phase 2: Instrument agent_loop.rs

  • Wrap execute_turn in a root agent_turn span: attributes user_message, session_id, model_id
  • After prompt assembly: emit prompt_assembly child span with system_prompt (full text), workspace_context, tools_offered (JSON array of {name, description})
  • Per ReAct iteration: emit react_iteration_N child span with messages_sent (full JSON), raw_response, tool_calls_parsed (JSON), tool_results (JSON)
  • After normalize_response: emit response_processing child span with raw_input, normalized_output, strippers_fired

Phase 3: Docs

  • Add scripts/mlflow-dev.md with start-server + run-daemon + view-traces workflow
  • Confirm mlflow mcp run can retrieve a trace's system_prompt attribute

Acceptance Criteria

  • With NODESPACE_MLFLOW_URL unset: daemon starts and runs with zero performance impact; no new network connections; cargo build succeeds without additional flags
  • With NODESPACE_MLFLOW_URL=http://localhost:5000 and a local mlflow server running: each aichat.ts send turn produces one trace in the MLflow UI containing the assembled system prompt, tools offered, per-iteration messages and raw response, and normalized final response
  • mlflow mcp run can retrieve and display the system_prompt attribute from a recent trace without manual JSON parsing
  • Span latencies match wall-clock latencies recorded by aichat-matrix.ts (within 5%)
  • No sensitive user data leaves the machine (all traces go to localhost only)

Technical Specifications

Reference Files

  • Agent loop: packages/agent/src/local_agent/agent_loop.rs — execute_turn is the instrumentation entry point; prompt assembly at ~line 178, ReAct loop at ~line 203
  • Prompt assembler: packages/agent/src/prompt_assembler.rs — assemble() returns AssembledPrompt { system_prompt, tool_schemas }
  • Response processing: packages/agent/src/local_agent/response_processing.rs — normalize_response() top-level entry; strip functions strip_channel_blocks, strip_tool_call_prose
  • Cargo manifest: packages/agent/Cargo.toml — add opentelemetry, opentelemetry-otlp, opentelemetry-http deps here

Key Design Decisions

  • Runtime-gated (env var) rather than compile-time feature flag: simpler to enable without rebuilding
  • OTLP over MLflow's proprietary REST trace API: uses the standard opentelemetry-otlp Rust crate rather than hand-rolled HTTP
  • MLflow chosen over bare OTLP collector: mlflow mcp run gives immediate MCP queryability from Claude Code; mlflow traces search gives CLI access; mlflow ui gives visual diff across model runs — all without additional infrastructure

Non-Goals

  • Production or release builds — tracing is always a no-op unless env var is set
  • Cloud MLflow or Databricks
  • Python autologging or MLflow Python SDK
  • Log rotation or trace retention (MLflow handles this natively)
  • Tracing the embedding pipeline or gRPC layer (agent turns only)

Related Issues

Activity

  1. malibio commented on Jun 5, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 134 files / 3844 tests passed

  2. self-assigned this
    on Jun 5, 2026
  3. added a commit that references this issue on Jun 22, 2026
    64d5451
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions