Added tracing metadata for LiteLLM provider#86
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
Claude finished @Daedaelius's task —— View job PR Code ReviewReviewing PR #86: "Added tracing metadata for LiteLLM provider" Review Checklist:
SummaryThis PR implements tracing metadata support for the LiteLLM provider to enable Langfuse integration. The changes establish a metadata flow from the evaluation framework through the API server, browser agent server, and frontend LLM client to the LiteLLM provider. ✅ Positive Aspects1. Clean Architecture
2. Robust Implementation
3. Environment Variable Support
🔍 Issues Found1. Code Quality Issues
const getEndpoint = (tierConfig) => {
const explicitEndpoint = tierConfig?.endpoint || requestBody.model.endpoint;
if (explicitEndpoint) return explicitEndpoint;
// Use env var default for litellm provider
if (tierConfig?.provider === 'litellm') return defaultLiteLLMEndpoint;
return undefined;
};Recommendation: Extract this logic into a separate method 2. Potential Bugs
3. Security Concerns
if (options?.tracingMetadata) {
payloadBody.metadata = options.tracingMetadata;
}Concern: Tracing metadata is passed directly to external LiteLLM service without validation or sanitization. Could potentially expose sensitive information. 4. Missing Error Handling
🏗️ Architecture Considerations1. Performance
2. Maintainability
🧪 Test CoverageMissing Test Cases:
📋 RecommendationsHigh Priority:
Medium Priority:
Low Priority:
🎯 Overall AssessmentThis is a well-architected addition that properly implements tracing metadata support. The code follows existing patterns and handles edge cases reasonably well. The main concerns are around security (metadata sanitization) and consistency (mixed explicit/context-based patterns). Recommendation: Approve with minor changes - Address the metadata sanitization security concern before merging. |
📝 WalkthroughWalkthroughThe changes introduce end-to-end tracing metadata propagation from server-side API request handling through client-side LLM calls and agent evaluation flows. Server-side enhancements add environment-driven fallback mechanisms for LiteLLM configuration (endpoint and API key). Tracing metadata is extracted, forwarded, and preserved across protocol layers, evaluation requests, and tool invocations. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant APIServer
participant LLMClient
participant LLMProvider
participant TracingService
Client->>APIServer: POST with tracing metadata<br/>(requestBody.metadata)
Note over APIServer: Extract tracingMetadata<br/>from requestBody
APIServer->>APIServer: createDynamicRequestNested<br/>(input, config, tracingMetadata)
Note over APIServer: Read fallback endpoints<br/>from LITELLM_ENDPOINT<br/>Read fallback API key<br/>from LITELLM_API_KEY
APIServer->>APIServer: Create EvaluationRequest<br/>with tracing field
APIServer->>LLMClient: call(request with<br/>tracingMetadata)
alt Explicit tracingMetadata
LLMClient->>LLMClient: Use request.tracingMetadata
else No explicit metadata
LLMClient->>TracingService: getCurrentTracingContext()
TracingService-->>LLMClient: Return global context
end
Note over LLMClient: Merge metadata into<br/>options.tracingMetadata
LLMClient->>LLMProvider: callWithMessages<br/>(payload with metadata)
Note over LLMProvider: Include metadata in<br/>provider payload
LLMProvider-->>LLMClient: Response
LLMClient-->>APIServer: Result with tracing
APIServer-->>Client: Response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (4)
agent-server/nodejs/src/api-server.js (2)
551-556: Consider guardingrequestBody.metadatashape before treating it as tracing metadataDeriving
const tracingMetadata = requestBody.metadata || {}works whenmetadatais an object, but if callers accidentally send a non-object (string/number), that value will be forwarded intotracingand down into evaluation/tracing code. A lightweight guard such astypeof requestBody.metadata === 'object' && requestBody.metadata !== nullwould keep the protocol robust against malformed inputs.
585-617: LiteLLM endpoint/API key fallbacks mostly correct; a couple of edge cases worth tighteningThe new
defaultLiteLLMEndpointandgetEndpointhelper correctly prefer tier- or model-level endpoints and then fall back toLITELLM_ENDPOINTwhen the tier config explicitly hasprovider: 'litellm'. Likewise,extractModelTierConfignow sensibly falls back toLITELLM_API_KEYfor litellm tier objects, andcreateDefaultModelConfigadds an endpoint for litellm based on defaults orLITELLM_ENDPOINT.Two small follow‑ups to consider:
- For nested configs where provider is only specified at the top level (
model.provider === 'litellm') and tier entries are plain strings,getEndpoint(tierConfig)sees only the string and will never look at the provider, so litellm tiers won’t get theLITELLM_ENDPOINTfallback unless an endpoint is explicitly provided. You could extendgetEndpointto also considerrequestBody.model.providerwhentierConfiglacks aproviderfield.- Similarly, the string-tier path in
extractModelTierConfigalways usesOPENAI_API_KEY, even if the effective provider for that tier is litellm via defaults. If you expect string tiers under litellm, it might be worth branching on the resolved provider and usingLITELLM_API_KEYin that case.These are edge cases; the main happy paths for object-based litellm configs are handled well.
Also applies to: 637-654, 689-692
front_end/panels/ai_chat/tracing/TracingProvider.ts (1)
21-30: LGTM! Metadata structure aligns with evaluation protocol.The metadata field properly extends TracingContext for Langfuse integration. The structure mirrors
EvaluationParams.tracing, enabling seamless propagation through the evaluation pipeline.Consider extracting this type to a shared location to avoid duplication with
EvaluationProtocol.ts, but this is a minor cleanup for future work.front_end/panels/ai_chat/LLM/LLMClient.ts (1)
212-242: Consider using DEBUG level for verbose tracing logs.The tracing metadata propagation logic is correct. However, the logging at lines 219-225, 230-234, and 239 outputs detailed tracing information at INFO level. In production, this may generate excessive log volume.
🔎 Suggested change to use DEBUG level
- logger.info('LLMClient.call() - Checking tracing context (fallback):', { + logger.debug('LLMClient.call() - Checking tracing context (fallback):', { hasContext: !!tracingContext, hasMetadata: !!tracingContext?.metadata, metadataKeys: tracingContext?.metadata ? Object.keys(tracingContext.metadata) : [], sessionId: tracingContext?.metadata?.session_id, traceId: tracingContext?.metadata?.trace_id });Apply similar changes to lines 230-234 and 239.
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (14)
agent-server/nodejs/src/api-server.jsagent-server/nodejs/src/lib/BrowserAgentServer.jsfront_end/panels/ai_chat/LLM/LLMClient.tsfront_end/panels/ai_chat/LLM/LLMTypes.tsfront_end/panels/ai_chat/LLM/LiteLLMProvider.tsfront_end/panels/ai_chat/agent_framework/AgentRunner.tsfront_end/panels/ai_chat/core/AgentNodes.tsfront_end/panels/ai_chat/core/AgentService.tsfront_end/panels/ai_chat/evaluation/EvaluationAgent.tsfront_end/panels/ai_chat/evaluation/EvaluationProtocol.tsfront_end/panels/ai_chat/evaluation/remote/EvaluationAgent.tsfront_end/panels/ai_chat/evaluation/remote/EvaluationProtocol.tsfront_end/panels/ai_chat/tools/LLMTracingWrapper.tsfront_end/panels/ai_chat/tracing/TracingProvider.ts
🧰 Additional context used
📓 Path-based instructions (1)
agent-server/nodejs/src/api-server.js
📄 CodeRabbit inference engine (agent-server/nodejs/CLAUDE.md)
agent-server/nodejs/src/api-server.js: Expose REST endpoint POST /v1/responses that accepts task input, URL, timeout, and model configuration, and returns OpenAI-compatible response with metadata
Use formatResponse() method to convert agent responses to OpenAI-compatible format and include metadata with clientId and tabId for screenshot capture
Model configuration must use canonical nested format with main_model, mini_model, and nano_model tiers, each containing provider, model, and api_key fields
POST /page/screenshot endpoint must accept clientId and tabId, use CDP Page.captureScreenshot, and return base64-encoded PNG with metadata and timestamp
POST /page/content endpoint must accept clientId, tabId, format (html or text), and includeIframes parameters; recursively capture iframe content when includeIframes is true
POST /page/execute endpoint must accept clientId, tabId, expression, returnByValue, and awaitPromise; use CDP Runtime.evaluate and return result with type and value
Accept POST /v1/responses input as either string format (simple message) or conversation array format with role and content fields; enforce at least one user message and max 100 messages/10,000 characters per message
Files:
agent-server/nodejs/src/api-server.js
🧠 Learnings (9)
📚 Learning: 2025-12-07T00:27:56.465Z
Learnt from: CR
Repo: BrowserOperator/browser-operator-core PR: 0
File: agent-server/nodejs/CLAUDE.md:0-0
Timestamp: 2025-12-07T00:27:56.465Z
Learning: Applies to agent-server/nodejs/src/lib/EvalServer.js : Use Chrome DevTools Protocol (CDP) for direct browser communication including screenshot capture via Page.captureScreenshot, page content via Runtime.evaluate, and tab management via Target.createTarget/closeTarget
Applied to files:
front_end/panels/ai_chat/evaluation/remote/EvaluationAgent.tsagent-server/nodejs/src/lib/BrowserAgentServer.jsfront_end/panels/ai_chat/evaluation/EvaluationAgent.ts
📚 Learning: 2025-12-07T00:27:56.465Z
Learnt from: CR
Repo: BrowserOperator/browser-operator-core PR: 0
File: agent-server/nodejs/CLAUDE.md:0-0
Timestamp: 2025-12-07T00:27:56.465Z
Learning: Applies to agent-server/nodejs/src/api-server.js : POST /page/execute endpoint must accept clientId, tabId, expression, returnByValue, and awaitPromise; use CDP Runtime.evaluate and return result with type and value
Applied to files:
front_end/panels/ai_chat/evaluation/remote/EvaluationAgent.tsfront_end/panels/ai_chat/evaluation/EvaluationAgent.ts
📚 Learning: 2025-12-07T00:27:56.465Z
Learnt from: CR
Repo: BrowserOperator/browser-operator-core PR: 0
File: agent-server/nodejs/CLAUDE.md:0-0
Timestamp: 2025-12-07T00:27:56.465Z
Learning: Applies to agent-server/nodejs/src/rpc-client.js : Implement JSON-RPC 2.0 protocol for bidirectional communication with request/response correlation using unique IDs, timeout handling, and error conditions
Applied to files:
agent-server/nodejs/src/lib/BrowserAgentServer.js
📚 Learning: 2025-12-07T00:27:56.465Z
Learnt from: CR
Repo: BrowserOperator/browser-operator-core PR: 0
File: agent-server/nodejs/CLAUDE.md:0-0
Timestamp: 2025-12-07T00:27:56.465Z
Learning: Applies to agent-server/nodejs/src/lib/EvalServer.js : Implement WebSocket server for browser agent connections with client lifecycle management (connect, ready, disconnect)
Applied to files:
agent-server/nodejs/src/lib/BrowserAgentServer.js
📚 Learning: 2025-12-07T00:27:56.465Z
Learnt from: CR
Repo: BrowserOperator/browser-operator-core PR: 0
File: agent-server/nodejs/CLAUDE.md:0-0
Timestamp: 2025-12-07T00:27:56.465Z
Learning: Applies to agent-server/nodejs/src/api-server.js : Use formatResponse() method to convert agent responses to OpenAI-compatible format and include metadata with clientId and tabId for screenshot capture
Applied to files:
agent-server/nodejs/src/lib/BrowserAgentServer.js
📚 Learning: 2025-12-07T00:27:56.465Z
Learnt from: CR
Repo: BrowserOperator/browser-operator-core PR: 0
File: agent-server/nodejs/CLAUDE.md:0-0
Timestamp: 2025-12-07T00:27:56.465Z
Learning: Applies to agent-server/nodejs/src/client-manager.js : Track WebSocket client connections and manage tab-level connections using composite clientId:tabId identifiers
Applied to files:
agent-server/nodejs/src/lib/BrowserAgentServer.js
📚 Learning: 2025-12-07T00:27:56.465Z
Learnt from: CR
Repo: BrowserOperator/browser-operator-core PR: 0
File: agent-server/nodejs/CLAUDE.md:0-0
Timestamp: 2025-12-07T00:27:56.465Z
Learning: Applies to agent-server/nodejs/src/logger.js : Implement structured logging using Winston with separate log files for combined, error, and API events in JSON format for easy parsing
Applied to files:
agent-server/nodejs/src/lib/BrowserAgentServer.js
📚 Learning: 2025-12-07T00:27:56.465Z
Learnt from: CR
Repo: BrowserOperator/browser-operator-core PR: 0
File: agent-server/nodejs/CLAUDE.md:0-0
Timestamp: 2025-12-07T00:27:56.465Z
Learning: Applies to agent-server/nodejs/src/api-server.js : Model configuration must use canonical nested format with main_model, mini_model, and nano_model tiers, each containing provider, model, and api_key fields
Applied to files:
agent-server/nodejs/src/api-server.js
📚 Learning: 2025-12-07T00:27:56.465Z
Learnt from: CR
Repo: BrowserOperator/browser-operator-core PR: 0
File: agent-server/nodejs/CLAUDE.md:0-0
Timestamp: 2025-12-07T00:27:56.465Z
Learning: Applies to agent-server/nodejs/src/api-server.js : Expose REST endpoint POST /v1/responses that accepts task input, URL, timeout, and model configuration, and returns OpenAI-compatible response with metadata
Applied to files:
agent-server/nodejs/src/api-server.js
🧬 Code graph analysis (2)
front_end/panels/ai_chat/LLM/LLMClient.ts (1)
front_end/panels/ai_chat/tracing/TracingConfig.ts (1)
getCurrentTracingContext(299-301)
agent-server/nodejs/src/api-server.js (2)
front_end/panels/ai_chat/LLM/LiteLLMProvider.ts (1)
getEndpoint(48-64)front_end/panels/ai_chat/core/LLMConfigurationManager.ts (1)
getEndpoint(222-229)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: claude-review
🔇 Additional comments (14)
agent-server/nodejs/src/lib/BrowserAgentServer.js (1)
730-745: Tracing metadata propagation in RPC params looks consistent and safeForwarding
request.tracing || {}intoparams.tracingand logging only presence/keys cleanly aligns this server with the updatedEvaluationParams.tracingprotocol, without impacting existing callers that omit tracing.agent-server/nodejs/src/api-server.js (1)
763-815: Dynamic request tracing field is wired correctly into EvaluationRequestExtending
createDynamicRequestNestedwith atracingMetadataparameter (default{}) and surfacing it astracingon the returned evaluation object keeps the nested-model flow in sync with the updatedEvaluationParams.tracingschema, while leaving existing metadata/tags behavior unchanged.front_end/panels/ai_chat/agent_framework/AgentRunner.ts (1)
744-754: AgentRunner LLM calls now correctly forward tracing metadataPassing
tracingMetadata: tracingContext?.metadataintollm.callaligns AgentRunner generations with the broader tracing pipeline so Langfuse sees consistent session/trace identifiers without affecting behavior when no tracing context is present.front_end/panels/ai_chat/tools/LLMTracingWrapper.ts (1)
88-91: Tracing wrapper correctly injects context metadata while allowing overridesIncluding
tracingMetadata: tracingContext?.metadatain the call, followed by...llmCallConfig.options, means tools get Langfuse metadata by default, while callers that explicitly setoptions.tracingMetadatacan still override it if needed. This is a good balance between convention and flexibility.front_end/panels/ai_chat/LLM/LLMTypes.ts (1)
205-212: LLMCallOptions.tracingMetadata type matches the tracing payload being propagatedAdding the structured
tracingMetadatafield (session_id/trace_id/generation_name/tags plus an open map) toLLMCallOptionscleanly documents what providers can expect and lines up with the metadata coming fromTracingContext, without impacting existing callers.front_end/panels/ai_chat/LLM/LiteLLMProvider.ts (1)
212-216: LiteLLM payload metadata wiring aligns with Langfuse expectationsConditionally assigning
payloadBody.metadata = options.tracingMetadatais a minimal, backward‑compatible way to forward tracing data to LiteLLM/Langfuse without affecting calls that don’t opt in to tracing.front_end/panels/ai_chat/evaluation/remote/EvaluationProtocol.ts (1)
107-116: EvaluationParams.tracing extension cleanly matches the new RPC payload shapeThe optional
tracingblock onEvaluationParamsmatches what the server now sends (session_id/trace_id/eval_id/etc.), letting TypeScript track tracing metadata through remote evaluation calls without changing existing fields.front_end/panels/ai_chat/core/AgentNodes.ts (1)
242-244: Stategraph AgentNode now participates in the shared tracing metadata flowForwarding
state.context?.tracingContext?.metadataastracingMetadatain the LLM call keeps AgentNode generations aligned with AgentRunner and tool-level tracing, giving Langfuse end‑to‑end visibility for these orchestrated calls.front_end/panels/ai_chat/evaluation/EvaluationProtocol.ts (1)
94-103: LGTM! Tracing metadata extension is well-structured.The optional
tracingfield properly supports Langfuse session grouping with appropriate optional fields. The snake_case naming aligns with Langfuse API conventions, and the index signature provides extensibility for future metadata needs.front_end/panels/ai_chat/LLM/LLMClient.ts (1)
52-52: LGTM!The optional
tracingMetadatafield extendsLLMCallRequestcorrectly, enabling explicit tracing metadata to be passed through LLM calls.front_end/panels/ai_chat/evaluation/EvaluationAgent.ts (1)
324-341: LGTM! Tracing context creation properly handles incoming metadata.The implementation correctly:
- Extracts tracing metadata from request params with safe default
- Generates fallback IDs when not provided
- Includes the full metadata object in the TracingContext for downstream propagation
front_end/panels/ai_chat/core/AgentService.ts (2)
279-288: LGTM! AUTOMATED_MODE bypass for API key validation is correct.The change appropriately allows missing API keys in
AUTOMATED_MODEwhere credentials come dynamically from request bodies. The empty string fallback at line 287 ensures the provider config structure remains valid.
600-603: LGTM! Metadata propagation through tracingContext.Forwarding
existingContext?.metadataenables Langfuse session grouping metadata to flow through the agent execution path, connecting evaluation framework tracing with downstream LLM calls.front_end/panels/ai_chat/evaluation/remote/EvaluationAgent.ts (1)
429-448: LGTM! Tracing metadata handling is consistent with EvaluationAgent.ts.The implementation correctly extracts tracing metadata from incoming requests and constructs a TracingContext with the metadata field for Langfuse integration. The fallback ID generation ensures evaluation tracing works even without upstream tracing data.
The pattern mirrors
front_end/panels/ai_chat/evaluation/EvaluationAgent.ts(lines 324-341), which maintains consistency across evaluation paths.
Summary by CodeRabbit
Release Notes
New Features
Improvements
✏️ Tip: You can customize this high-level summary in your review settings.