langfuse-rs
A Rust client library for Langfuse with both auto-generated API bindings and an ergonomic interface powered by the Bon builder pattern library.
This client provides full access to the Langfuse Public API, enabling you to integrate observability and monitoring into your Rust-based LLM applications.
Project Structure
This project consists of two crates:
langfuse-client-base- Auto-generated client from the Langfuse OpenAPI specificationlangfuse-ergonomic- User-friendly wrapper with builder patterns for better developer experience
Features
- ✅ Full API coverage via OpenAPI generation
- ✅ Type-safe builder patterns using Bon
- ✅ Async/await support with Tokio
- ✅ Comprehensive error handling
- ✅ Support for traces, observations (spans/generations), and scores
- ✅ Flexible authentication (API keys, basic auth)
Supported API Operations
This client provides access to all Langfuse Public API endpoints:
- Traces: Create and manage traces for LLM interactions
- Observations: Track spans, generations, and events within traces
- Scores: Evaluate trace quality with numeric, categorical, or binary scores
- Sessions: Group related traces into sessions
- Prompts: Manage and version prompts (when available)
- Projects: Access project configurations and settings
Quick Start
Installation
Add to your Cargo.toml:
[]
= { = "langfuse-ergonomic" }
= { = "1", = ["full"] }
= "1"
Basic Usage
use LangfuseClient;
use json;
async
Langfuse API
Authentication
The Langfuse Public API uses Basic Authentication with project-specific API keys:
- Public Key: Used as the username
- Secret Key: Used as the password
You can obtain these keys from your Langfuse project settings.
API Endpoints
The client connects to Langfuse Cloud by default (https://cloud.langfuse.com), but you can configure it for different regions or self-hosted instances:
- US Cloud:
https://us.cloud.langfuse.com - EU Cloud:
https://cloud.langfuse.com(default) - Self-hosted: Your custom Langfuse instance URL
Resources
Setup & Development
Prerequisites
- Rust 1.75 or later
- Node.js (for OpenAPI generator)
Initial Setup
- Clone the repository:
- Initialize the development environment:
- Generate the base client from OpenAPI:
- Build the project:
- Run tests:
Development Workflow
Before committing code, ensure it passes all checks:
Or run individual checks:
Git hooks are automatically configured to run these checks before each commit.
Environment Variables
Create a .env file for running examples:
LANGFUSE_PUBLIC_KEY=pk-lf-...
LANGFUSE_SECRET_KEY=sk-lf-...
LANGFUSE_BASE_URL=https://cloud.langfuse.com # Optional
Running Examples
# Basic usage example
# Multi-step LLM chain example
API Overview
Creating Traces
let trace = client.trace
.name
.input
.output
.metadata
.user_id
.session_id
.tags
.send
.await?;
Creating Observations
Spans
client.span
.name
.parent_observation_id // Optional nesting
.input
.output
.level
.send
.await?;
Generations (LLM calls)
client.generation
.name
.model
.model_parameters
.input
.output
.tokens // prompt tokens, completion tokens
.send
.await?;
Events
client.event
.name
.level
.metadata
.send
.await?;
Adding Scores
// Numeric score
client.score
.value
.comment
.send
.await?;
// Binary score
client.binary_score
.send
.await?;
// Rating score (e.g., 4 out of 5 stars)
client.rating_score
.send
.await?;
// Categorical score
client.categorical_score
.send
.await?;
Architecture
OpenAPI Generation
The langfuse-client-base crate is generated from the official Langfuse OpenAPI specification using openapi-generator. This ensures complete API coverage and type safety.
To regenerate the client:
Builder Pattern Enhancement
The langfuse-ergonomic crate wraps the generated client with intuitive builder patterns using the Bon library. This provides:
- Named parameters for better readability
- Optional parameters with sensible defaults
- Compile-time validation
- Method chaining for fluent interfaces
Publishing & Releases
This project uses release-plz to automate releases and publishing to crates.io.
Automated Releases
When changes are merged to main, release-plz will:
- Create a PR with version bumps and changelog updates
- After merging the release PR, automatically:
- Create GitHub releases with changelogs
- Publish crates to crates.io
- Tag the release
Manual Publishing
To publish manually:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Workflow
- Make changes to the ergonomic layer in
langfuse-ergonomic/ - If API changes are needed, update the OpenAPI generation script
- Run tests:
cargo test - Format code:
cargo fmt - Check lints:
cargo clippy
License
This project is licensed under MIT OR Apache-2.0.
Acknowledgments
- Langfuse for the observability platform
- Bon for the excellent builder macro library
- OpenAPI Generator for code generation
- Initial implementation created with Claude Code