#observability #llm #tracing

langfuse

A simple Rust client for the Langfuse.com observability platform

7 releases

Uses new Rust 2024

new 0.1.6 Mar 22, 2025
0.1.5 Mar 22, 2025

#549 in Debugging

Download history

141 downloads per month

MIT license

23KB
438 lines

langfuse Rust Client

Crates.io Documentation Github License: MIT

A non official open-source Rust client for tracking simple interactions with Langfuse (https://langfuse.com) the open-source LLM observability platform.

Features

  • Track user interactions with LangFuse
  • Handle both successful and error cases
  • Configure LangFuse API settings
  • Support for custom trace names
  • Track tokens used and model name
  • Track processing time
  • Flexible metadata and observation tracking

Installation

Type this inside your project directory

cargo add langfuse

Or if you prefer to use a Cargo.toml file: Add this to your Cargo.toml:

[dependencies]
Langfuse = "0.1.4"

Usage

Basic example:

use langfuse::{LangFuseConfig, send_interaction};

#[tokio::main]
async fn main() {
    let config = LangFuseConfig::new(
        "your-public-key",
        "your-secret-key",
        "https://cloud.langfuse.com",
    );

    let result = send_interaction(
        &config,
        "request-id-123",
        Some("user-1"), 
        Some("session-1"),
        "Hello, who are you?", 
        "I'm an AI assistant, ready to help you",
        "here should be full raw response", 
        100, 
        false,
        Some("model-x"), 
        Some(TokenUsage {
            input_tokens: 100,
            output_tokens: 900,
            total_tokens: 1000,
        }),                           // Detailed token usage
        Some("json_endpoint_request_trace"), 
    ).await;

    match result {
        Ok(_) => println!("Successfully tracked interaction"),
        Err(e) => eprintln!("Error tracking interaction: {}", e),
    }
}

API Documentation

LangFuseConfig

Configuration for LangFuse API credentials and endpoint.

pub struct LangFuseConfig {
    pub public_key: String,
    pub secret_key: String,
    pub base_url: String,
}

impl LangFuseConfig {
    pub fn new(public_key: &str, secret_key: &str, base_url: &str) -> Self;
}

send_interaction

Track an interaction with LangFuse.

pub async fn send_interaction(
    config: &LangFuseConfig,
    request_id: &str,
    user_id: Option<&str>,
    session_id: Option<&str>,
    input: &str,
    output: &str,
    raw_response: Option<&str>,
    processing_time_ms: u128,
    is_error: bool,
    model_name: Option<&str>,
    token_usage: Option<TokenUsage>,
    trace_name: Option<&str>,
) -> Result<(), LangFuseTrackerError>;

// TokenUsage structure for detailed token tracking
pub struct TokenUsage {
    pub input_tokens: u32,
    pub output_tokens: u32,
    pub total_tokens: u32,
}

LangFuseTrackerError

Error types for LangFuse tracking operations.

pub enum LangFuseTrackerError {
    InvalidCredentials,
    NetworkError(reqwest::Error),
    InvalidResponseFormat,
    JsonParseError(serde_json::Error),
    InvalidTimestampFormat,
    InvalidBaseUrlFormat,
    Unknown(String),
}

Project Structure

langfuse_tracker/
├── src/
│   ├── lib.rs
│   ├── client.rs
│   ├── error.rs
│   ├── types.rs
│   └── utils/
│       └── mod.rs
├── example/
│   └── usage.rs
├── tests/
│   └── integration.rs

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details. If you have any questions or suggestions, please contact open an issue or a pull request on https://github.com/adolfousier/langfuse-rust

License

This project is licensed under the MIT License - see the LICENSE file for details.

Dependencies

~5–18MB
~246K SLoC