Advanced LangChain Integration
Advanced patterns for LangChain tracing including agents, tools, error handling, and complete workflows
This guide covers advanced LangChain integration patterns with Noveum Trace, including agents with tools, error handling, and complete multi-pattern workflows.
🎯 What You'll Learn
- Agent Tracing: Track agent decision-making and tool usage
- Tool Execution: Monitor custom tool calls and results
- Error Handling: Capture and trace errors automatically
- Complete Workflows: Combine multiple patterns in production applications
📋 Prerequisites
Set your environment variables:
🤖 Agent with Tool Usage
Agents use LLMs to decide which tools to use and in what order. Here's how to trace them:
What Gets Traced
When running the agent, Noveum Trace automatically captures:
- Agent Reasoning: The thought process and decision-making
- Tool Selection: Which tools the agent chooses and why
- Tool Execution: Input parameters and output results
- Iteration Steps: Each step in the agent's reasoning loop
- Performance Metrics: Timing for each tool and LLM call
- Error Handling: Any errors during tool execution
🔍 Error Handling
Errors are automatically captured and traced with full context:
Error Trace Details
When an error occurs, the trace includes:
- Error Type: Exception class name
- Error Message: Full error description
- Stack Trace: Complete traceback
- Context: Input data and state when error occurred
- Span Status: Marked as error with details
🎯 Complete Integration Example
Here's a production-ready example combining multiple patterns:
🔧 Callback Configuration
Two Approaches to Pass Callbacks
1. Config-Based (Recommended)
Benefits:
- Callbacks automatically propagate through all components
- Works with LCEL (LangChain Expression Language)
- Best for agents, chains, and complex workflows
- Cleaner code separation
2. Direct to Component (Alternative)
When to use:
- Simple single-component use cases
- When callbacks should persist across multiple invocations
- Quick prototypes
Custom Parent-Child Span Relationships
When you need to set explicit parent-child relationships between spans (e.g., connecting LangChain operations to external traces), you have two options:
Option 1: Via Metadata (Recommended for Simple Cases)
Set parent-child relationships using metadata:
Option 2: Manual Lifecycle Control (Required for Explicit Parent-Child Control)
⚠️ IMPORTANT: Manual trace lifecycle control is ABSOLUTELY necessary when you need to explicitly set parent-child relationships across different trace contexts or connect LangChain operations to external parent spans.
Use this ONLY when you need explicit parent-child span control. For all other use cases, the automatic callback-based approach is sufficient and recommended.
When to use manual lifecycle control:
- Connecting LangChain operations to external tracing systems
- Linking multiple LangChain workflows under a single parent trace
- Integrating with non-LangChain components that already have trace context
- Bridging different tracing frameworks
When NOT to use:
- Standard LangChain workflows (callbacks handle this automatically)
- Simple agent or chain operations
- Normal production use cases
📊 What You'll See in the Dashboard
After running these examples:
Trace View
- Complete workflow execution
- Agent reasoning steps
- Tool calls and results
- Error details with context
Span Details
- Individual operation timing
- Input/output data
- Token usage and costs
- Error stack traces
Analytics
- Success/failure rates
- Tool usage patterns
- Performance bottlenecks
- Cost analysis
💡 Best Practices
- Always use callbacks: Attach callback handler to all LangChain components
- Add context: Use custom attributes for business-specific data
- Handle errors gracefully: Errors are automatically traced with full context
- Monitor costs: Track spending across different models and operations
- Use descriptive names: Make traces easy to identify and search
- Flush on exit: Call
noveum_trace.flush()before application exit - Don't share handlers: Create a separate
NoveumTraceCallbackHandlerinstance for each concurrent/parallel operation (threads, async tasks). Handlers are not thread-safe
🚀 Next Steps
- Basic LLM Tracing - Start with simple patterns
- Chain Tracing - Multi-step workflows
- LangGraph Integration - Complex agent workflows
Need Help?
- Documentation: Browse our comprehensive guides
- Community: Join our Discord for support
- Support: Contact our team for enterprise support
Get Early Access to Noveum.ai Platform
Be the first one to get notified when we open Noveum Platform to more users. All users get access to Observability suite for free, early users get free eval jobs and premium support for the first year.