Skip to main content

Overview

Scalable trace upload is critical for production agent observability. This guide covers patterns for uploading traces efficiently, handling parent-child relationships, and managing large volumes.

Complete Upload Implementation

Here’s a production-ready implementation for uploading traces to LangSmith:
upload_traces.py

Key Implementation Patterns

1. Timestamp Shifting

When uploading historical or synthetic traces, shift timestamps to make them appear recent:
Ensure your datetime objects are timezone-aware or consistently naive. Mixing the two causes errors.

2. ID Mapping with UUID7

Preserve time-ordering while generating fresh IDs:
Why uuid7? Unlike uuid4, uuid7 preserves temporal ordering, making trace analysis and debugging easier.

3. Trace Grouping and Sorting

Group runs by trace and sort to ensure parent runs are processed before children:

4. Building the Run Tree

Construct the hierarchical trace structure:

5. Ending Runs in Reverse Order

End child runs before parent runs:

6. Uploading and Flushing

Post the complete trace tree and flush at the end:
Always call client.flush() before your script exits. Otherwise, traces may be lost due to background operations not completing.

Usage Example

Production Considerations

Rate Limiting

Implement exponential backoff for API rate limits:

Batch Processing

For large datasets, process in batches:

Error Recovery

Log failed traces for retry:

Performance Optimization

Parallel Upload

Use thread pools for concurrent uploads:
Be cautious with parallelization. Too many concurrent uploads can trigger rate limits or exhaust connection pools.

Next Steps

Online Evaluation

Set up continuous evaluation on uploaded traces

Production Overview

Learn more about production deployment strategies