LangSmith Rust
A production-ready Rust crate for manual tracing to LangSmith, providing similar ergonomics to the Python and TypeScript SDKs. Designed for building observable AI agent systems with LangGraph-like architectures.
Features
- Automatic Configuration - Environment-based setup with
.envsupport - Hierarchical Tracing - Parent-child run relationships with automatic context propagation
- Agent Message Types - Full support for tool calls, tool messages, AI messages, and human messages
- Thread Management - Conversation and session tracking
- Metrics Support - Token counting and cost tracking
- Non-blocking Async - All tracing operations are async and non-blocking
- Decorator Pattern - Automatic node tracing with
trace_nodehelper - Design Patterns - Strategy, Factory, and Observer patterns for extensibility
- Type Safety - Full Rust type safety with compile-time guarantees
Installation
Add to your Cargo.toml:
[]
= { = "../langsmith-rust" }
Or from a git repository:
[]
= { = "https://github.com/your-org/langsmith-rust" }
Quick Start
1. Configuration
Create a .env file in your project root:
LANGSMITH_TRACING=true
LANGSMITH_ENDPOINT=https://api.smith.langchain.com
LANGSMITH_API_KEY=<your-api-key>
LANGSMITH_PROJECT=<your-project-name>
LANGSMITH_TENANT_ID=<workspace-id> # Optional
2. Initialize
use langsmith_rust;
// Initialize (loads .env automatically)
init;
3. Basic Usage
Manual Tracing
use ;
use json;
async
Automatic Node Tracing (Recommended)
use ;
use json;
async
// Wrap your node function with trace_node
let result = trace_node.await?;
How it works:
- Before execution: Serializes function parameters as
inputsand posts to LangSmith - Executes the function
- After execution: Serializes return value as
outputsand patches the run - Error handling: Automatically captures and traces errors
Architecture
This crate follows SOLID principles and uses several design patterns:
- Strategy Pattern - Different tracing strategies (async/sync)
- Factory Pattern - TracerFactory for creating tracers with different configurations
- Observer Pattern - Observable nodes for LangGraph integration
See ARCHITECTURE.md for detailed architecture documentation.
Integration with LangGraph
This crate is designed to integrate seamlessly with LangGraph-style node execution:
use ;
use json;
// In your graph node implementation
See INTEGRATION.md for detailed integration guide.
API Reference
Core Types
Tracer
Main structure for creating and managing runs.
// Create a new tracer
let tracer = new;
// Configure tracer
let tracer = tracer
.with_thread_id
.with_client;
// Create child run
let child = tracer.create_child;
// Post and patch
tracer.post.await?;
tracer.end;
tracer.patch.await?;
TracerFactory
Factory for creating tracers with different configurations:
// Create root tracer
let root = create_root;
// Create with thread context
let tracer = create_with_thread;
// Create for graph node
let tracer = create_for_node;
Helper Functions
trace_node(name, run_type, inputs, f)- Wrap async function with tracingtrace_node_sync(name, run_type, inputs, f)- Wrap sync function with tracing
Run Types
RunType::Chain- Chain execution (orchestrator)RunType::Llm- LLM callRunType::Tool- Tool executionRunType::Retriever- Retrieval operationRunType::Embedding- Embedding generationRunType::Prompt- Prompt executionRunType::Runnable- Generic runnableRunType::Custom(String)- Custom run type
Examples
See the examples/ directory for complete examples:
test_llm_tracing.rs- Basic LLM tracing exampledecorator_example.rs- Using trace_node with multiple nodesobservable_graph.rs- Observable graph nodes with Observer pattern
Run examples with:
Testing
Run all tests:
Run specific test suites:
Error Handling
All tracing errors are logged to stderr but never break your application. If tracing fails, your code continues to execute normally. This ensures tracing is truly non-intrusive.
// Even if LangSmith is down, your code continues
let result = trace_node.await?;
// Your application continues normally
Performance
- Non-blocking: All HTTP requests are async and don't block execution
- Lazy initialization: Configuration is loaded only when needed
- Efficient serialization: Uses
serde_jsonfor fast serialization - Minimal overhead: Tracing adds <1ms overhead per node
Contributing
Contributions are welcome! Please read our contributing guidelines before submitting PRs.
License
MIT
Documentation
- ARCHITECTURE.md - Detailed architecture and design patterns
- INTEGRATION.md - Integration guide for LangGraph
- docs.md - Comprehensive documentation for LLMs
- READING_GUIDE.md - Guide to understanding the codebase