Skip to content

Added tracing metadata for LiteLLM provider#85

Closed
olesho wants to merge 1 commit intomainfrom
feat/litellm-tracing
Closed

Added tracing metadata for LiteLLM provider#85
olesho wants to merge 1 commit intomainfrom
feat/litellm-tracing

Conversation

@olesho
Copy link
Copy Markdown
Contributor

@olesho olesho commented Dec 26, 2025

Summary by CodeRabbit

Release Notes

  • New Features
    • Tracing metadata is now automatically captured and propagated across all LLM calls and evaluation workflows.
    • Session and trace ID tracking provides enhanced request visibility and comprehensive observability.
    • Dynamic model configuration now supports automatic endpoint and API key resolution mechanisms.
    • Metadata propagation through agent execution pipelines enables complete end-to-end traceability and audit trails.

✏️ Tip: You can customize this high-level summary in your review settings.

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@olesho olesho closed this Dec 26, 2025
@claude
Copy link
Copy Markdown

claude bot commented Dec 26, 2025

Claude finished @olesho's task —— View job


Code Review: Added tracing metadata for LiteLLM provider

I've completed a comprehensive review of this PR that adds tracing metadata for the LiteLLM provider across 14 files. Here's my analysis:

Strengths

1. Consistent Implementation Pattern

The changes follow a consistent pattern across all components:

  • Hierarchical tracing context propagation (TracingContext interface)
  • Proper parent-child relationships in trace observations
  • Consistent metadata structure with execution levels (stategraph, agentrunner, tool)

2. Good Separation of Concerns

  • LLMTracingWrapper.ts: Clean abstraction for LLM call tracing
  • Evaluation components: Proper trace context initialization and propagation
  • Agent framework: Well-structured hierarchical tracing for nested agent operations

3. Robust Error Handling

// AgentRunner.ts:802-818
if (generationId && tracingContext?.traceId) {
  await tracingProvider.updateObservation(generationId, {
    endTime: new Date(),
    error: error.message || String(error),
    // ...proper error metadata
  });
}

Error cases are properly traced without breaking execution flow.


🔍 Code Quality Issues

1. Excessive Console Logging (Critical)

Location: AgentRunner.ts:671-678, AgentNodes.ts, multiple files

console.log(`[HIERARCHICAL_TRACING] AgentRunner: Starting LLM generation for ${agentName}:`, {
  hasTracingContext: !!tracingContext,
  traceId: tracingContext?.traceId,
  // ... large objects being logged
});

Issues:

  • Performance Impact: Console logging in production can be expensive
  • Information Leakage: Potentially sensitive data (API keys, user inputs) logged to console
  • Code Pollution: Debug logs mixed with production code

Recommendation: Replace with proper logger calls:

logger.debug('Starting LLM generation', { agentName, traceId });

2. Inconsistent Error Handling

Location: AgentNodes.ts:198-201

} catch (err) {
  logger.warn('Failed to update generation observation with tools list', err);
}

Issue: Silent failure of tracing operations could mask infrastructure issues.

Recommendation: Add error classification:

} catch (err) {
  if (err.name === 'NetworkError') {
    logger.error('Tracing infrastructure unavailable', err);
  } else {
    logger.warn('Non-critical tracing update failed', err);
  }
}

3. Magic String Usage

Location: Multiple files

executionLevel: 'agentrunner' as const,

Recommendation: Define constants:

export const EXECUTION_LEVELS = {
  STATEGRAPH: 'stategraph',
  AGENTRUNNER: 'agentrunner', 
  TOOL: 'tool'
} as const;

4. Large Object Construction in Hot Paths

Location: AgentRunner.ts:689-713

Issue: Heavy metadata objects created for every LLM call.

Recommendation: Pre-compute static metadata and merge at runtime.


🚨 Security Concerns

1. Data Leakage in Tracing

Location: LLMTracingWrapper.ts:65-68

input: {
  systemPrompt: llmCallConfig.systemPrompt, // Could contain sensitive context
  messages: llmCallConfig.messages,         // User data, API responses
  messageCount: llmCallConfig.messages.length
}

Risk: User inputs, system prompts, and LLM responses may contain sensitive information being sent to tracing infrastructure.

Recommendation:

  • Implement data sanitization before tracing
  • Add configuration for PII scrubbing
  • Truncate large payloads:
input: {
  systemPrompt: truncateAndSanitize(llmCallConfig.systemPrompt, 500),
  messageCount: llmCallConfig.messages.length
  // Don't include full messages by default
}

2. Unconstrained Metadata Size

Location: Throughout tracing calls

Issue: No limits on metadata size could lead to DoS or excessive storage costs.

Recommendation: Implement size limits and validation.


Performance Considerations

1. Synchronous Tracing Operations

Location: Multiple await tracingProvider.createObservation() calls

Issue: Each tracing call adds latency to the critical path.

Recommendation: Consider async queuing:

const tracingQueue = new AsyncQueue();
tracingQueue.add(() => tracingProvider.createObservation(...));

2. String Concatenation in Hot Paths

Location: ID generation patterns

generationId = `gen-agentrunner-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;

Recommendation: Pre-allocate ID components or use UUID libraries.


🧪 Test Coverage

Major Gap: No test files were modified in this PR.

Recommendations:

  1. Unit Tests: Mock tracing provider to verify observation creation
  2. Integration Tests: Test trace hierarchy correctness
  3. Performance Tests: Measure tracing overhead
  4. Error Scenario Tests: Verify graceful degradation when tracing fails

Example test structure needed:

describe('LLMTracingWrapper', () => {
  it('should create generation observation with proper metadata', async () => {
    const mockProvider = createMockTracingProvider();
    // Test implementation
  });

  it('should handle tracing failures gracefully', async () => {
    // Test error scenarios
  });
});

📝 Minor Issues

1. TypeScript Type Safety

Location: AgentService.ts:612

try { (state as any).context.apiKey = this.#apiKey || ''; } catch {}

Issue: Unsafe type assertion with silent failure.

2. Inconsistent Naming

  • Some observation IDs use gen-, others use tool-call-
  • Consider standardized naming convention

3. Missing Documentation

The TracingProvider.ts interface lacks JSDoc comments explaining the hierarchical tracing model.


🎯 Overall Assessment

Verdict: ✅ Approve with Recommendations

This PR successfully implements comprehensive tracing for LiteLLM provider operations with good architectural design. The hierarchical tracing model is well-thought-out and the implementation is consistent across components.

Priority fixes before merge:

  1. Replace console.log statements with proper logger calls
  2. Add data sanitization for sensitive information in traces
  3. Implement size limits for trace metadata

Post-merge recommendations:

  1. Add comprehensive test coverage
  2. Consider performance optimizations for high-volume scenarios
  3. Add monitoring for tracing infrastructure health

The code quality is good overall, and the tracing capabilities will provide valuable observability into the LLM operations. The implementation correctly handles the complex nested execution model of the agent framework.


@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Dec 26, 2025

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

This pull request introduces end-to-end Langfuse tracing integration across the agent and evaluation pipeline. Tracing metadata extracted from API requests is propagated through request creation, evaluation flows, and LLM calls. The changes also add environment variable fallbacks for LiteLLM endpoint and API key configuration. Type definitions are extended throughout to carry tracing context across the stack.

Changes

Cohort / File(s) Summary
Backend API & Request Handling
agent-server/nodejs/src/api-server.js, agent-server/nodejs/src/lib/BrowserAgentServer.js
Extracts tracingMetadata from request bodies and passes it through dynamic request creation. Added tracing field to EvaluationRequest. BrowserAgentServer now includes tracing in RPC request metadata with debug logging. LiteLLM endpoint and API key now support LITELLM_ENDPOINT and LITELLM_API_KEY environment variable fallbacks.
LLM Client Layer
front_end/panels/ai_chat/LLM/LLMClient.ts, front_end/panels/ai_chat/LLM/LLMTypes.ts, front_end/panels/ai_chat/LLM/LiteLLMProvider.ts
Added tracingMetadata to LLMCallRequest and LLMCallOptions interfaces. LLMClient resolves tracing metadata from explicit request data or global context, with fallback logic and logging. LiteLLMProvider injects metadata field into provider request payload when present.
Agent Framework & Orchestration
front_end/panels/ai_chat/agent_framework/AgentRunner.ts, front_end/panels/ai_chat/core/AgentNodes.ts, front_end/panels/ai_chat/core/AgentService.ts
AgentRunner and AgentNodes now propagate tracingMetadata to LLM calls. AgentService preserves pre-existing evaluation metadata in tracing context and updates API key requirement logic for AUTOMATED_MODE.
Evaluation Layer
front_end/panels/ai_chat/evaluation/EvaluationAgent.ts, front_end/panels/ai_chat/evaluation/EvaluationProtocol.ts, front_end/panels/ai_chat/evaluation/remote/EvaluationAgent.ts, front_end/panels/ai_chat/evaluation/remote/EvaluationProtocol.ts
Added optional tracing property to EvaluationParams. Evaluation agents now read tracing metadata from request, derive or fallback trace/session IDs, and propagate context through evaluation flow with enhanced logging.
Tracing Infrastructure
front_end/panels/ai_chat/tools/LLMTracingWrapper.ts, front_end/panels/ai_chat/tracing/TracingProvider.ts
LLMTracingWrapper passes tracingMetadata to LLM calls. TracingContext extended with optional metadata field containing evaluation framework metadata (session_id, trace_id, eval_id, eval_name, category, tags).

Sequence Diagram(s)

sequenceDiagram
    actor Client
    participant APIServer
    participant EvalAgent as Evaluation Agent
    participant AgentRunner
    participant LLMClient
    participant LiteLLMProvider

    Client->>APIServer: POST /responses (with tracing metadata)
    APIServer->>APIServer: Extract tracingMetadata from request
    APIServer->>EvalAgent: createDynamicRequestNested(input, config, tracingMetadata)
    APIServer->>EvalAgent: EvaluationRequest + tracing field
    
    EvalAgent->>EvalAgent: Read tracing from params.tracing
    EvalAgent->>EvalAgent: Derive/fallback traceId, sessionId
    EvalAgent->>EvalAgent: Initialize tracingContext with metadata
    
    EvalAgent->>AgentRunner: Execute agent with tracingContext
    AgentRunner->>AgentRunner: Extract tracingMetadata from context
    AgentRunner->>LLMClient: LLM call + tracingMetadata
    
    LLMClient->>LLMClient: Resolve tracingMetadata (explicit or global)
    LLMClient->>LiteLLMProvider: callWithMessages(options.tracingMetadata)
    
    LiteLLMProvider->>LiteLLMProvider: Inject metadata into request payload
    LiteLLMProvider->>LiteLLMProvider: Send to LLM with tracing context
    
    LiteLLMProvider-->>LLMClient: Response
    LLMClient-->>AgentRunner: LLM result
    AgentRunner-->>EvalAgent: Agent execution complete
    EvalAgent-->>APIServer: Evaluation result
    APIServer-->>Client: Response with traced execution
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • tysonthomas9

Poem

🐇 Traces now flow from request to call,
Metadata hops through layers tall,
LiteLLM whispers with fallback grace,
Each eval run finds its tracing place,
Langfuse logs the agent's dance! ✨

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/litellm-tracing

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 197f88f and d6bf633.

📒 Files selected for processing (14)
  • agent-server/nodejs/src/api-server.js
  • agent-server/nodejs/src/lib/BrowserAgentServer.js
  • front_end/panels/ai_chat/LLM/LLMClient.ts
  • front_end/panels/ai_chat/LLM/LLMTypes.ts
  • front_end/panels/ai_chat/LLM/LiteLLMProvider.ts
  • front_end/panels/ai_chat/agent_framework/AgentRunner.ts
  • front_end/panels/ai_chat/core/AgentNodes.ts
  • front_end/panels/ai_chat/core/AgentService.ts
  • front_end/panels/ai_chat/evaluation/EvaluationAgent.ts
  • front_end/panels/ai_chat/evaluation/EvaluationProtocol.ts
  • front_end/panels/ai_chat/evaluation/remote/EvaluationAgent.ts
  • front_end/panels/ai_chat/evaluation/remote/EvaluationProtocol.ts
  • front_end/panels/ai_chat/tools/LLMTracingWrapper.ts
  • front_end/panels/ai_chat/tracing/TracingProvider.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@olesho olesho deleted the feat/litellm-tracing branch December 26, 2025 04:16
@Daedaelius Daedaelius restored the feat/litellm-tracing branch December 26, 2025 05:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant