Repository navigation
Description
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.rslogssystem_prompt_lenat 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_Nper 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
- Child span
Dev workflow this enables:
mlflow ui— browse traces visually, diff prompts across model runsmlflow mcp run— query traces from Claude Code ("what did the agent see on turn 3 of the 12B run?")mlflow traces searchCLI — scripted analysis across a matrix run
Implementation Approach
Phase 1: Tracing infrastructure
- Add
opentelemetry,opentelemetry-otlp,opentelemetry-httptopackages/agent/Cargo.toml, runtime-gated via env var check (no separate Cargo feature needed) - Add
OtlpTracerhelper inpackages/agent/src/local_agent/— initializes an OTLP exporter pointing atNODESPACE_MLFLOW_URLif 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_turnin a rootagent_turnspan: attributesuser_message,session_id,model_id - After prompt assembly: emit
prompt_assemblychild span withsystem_prompt(full text),workspace_context,tools_offered(JSON array of{name, description}) - Per ReAct iteration: emit
react_iteration_Nchild span withmessages_sent(full JSON),raw_response,tool_calls_parsed(JSON),tool_results(JSON) - After
normalize_response: emitresponse_processingchild span withraw_input,normalized_output,strippers_fired
Phase 3: Docs
- Add
scripts/mlflow-dev.mdwith start-server + run-daemon + view-traces workflow - Confirm
mlflow mcp runcan retrieve a trace'ssystem_promptattribute
Acceptance Criteria
- With
NODESPACE_MLFLOW_URLunset: daemon starts and runs with zero performance impact; no new network connections;cargo buildsucceeds without additional flags - With
NODESPACE_MLFLOW_URL=http://localhost:5000and a localmlflow serverrunning: eachaichat.ts sendturn 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 runcan retrieve and display thesystem_promptattribute 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_turnis the instrumentation entry point; prompt assembly at ~line 178, ReAct loop at ~line 203 - Prompt assembler:
packages/agent/src/prompt_assembler.rs—assemble()returnsAssembledPrompt { system_prompt, tool_schemas } - Response processing:
packages/agent/src/local_agent/response_processing.rs—normalize_response()top-level entry; strip functionsstrip_channel_blocks,strip_tool_call_prose - Cargo manifest:
packages/agent/Cargo.toml— addopentelemetry,opentelemetry-otlp,opentelemetry-httpdeps 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-otlpRust crate rather than hand-rolled HTTP - MLflow chosen over bare OTLP collector:
mlflow mcp rungives immediate MCP queryability from Claude Code;mlflow traces searchgives CLI access;mlflow uigives 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
- A/B test Gemma 4 12B (Q4_K_M) vs E4B for the local ai-chat agent #1329 (A/B trial that exposed this gap)
- Model-capability guardrails + larger-model prerequisites (anti-fabrication, KV-cache quant, per-model n_ctx, Gemma-12B tool parsing) #1332 (model capability prerequisites; tracing would help validate fixes there too)
Activity
malibio commentedon Jun 5, 2026
Frontend baseline: 134 files / 3844 tests passed
Dev observability: emit agent turns as MLflow traces via OTLP (assemb…