langfuse-sdk-patterns

Featured

Langfuse SDK best practices, patterns, and idiomatic usage. Use when learning Langfuse SDK patterns, implementing proper tracing, or following best practices for LLM observability. Trigger with phrases like "langfuse patterns", "langfuse best practices", "langfuse SDK guide", "how to use langfuse", "langfuse idioms".

AI & Automation 2,926 stars 433 forks Updated yesterday MIT

Install

View on GitHub

Quality Score: 99/100

Stars 20%
100
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# Langfuse SDK Patterns ## Overview Production-quality patterns for the Langfuse SDK: singleton clients, the `observe` wrapper, `startActiveObservation` for nested traces, session tracking, graceful shutdown, and error-safe tracing. ## Prerequisites - Completed `langfuse-install-auth` setup - Understanding of async/await patterns - For v4+: `@langfuse/tracing`, `@langfuse/otel`, `@opentelemetry/sdk-node` ## Instructions ### Pattern 1: Singleton Client with Graceful Shutdown ```typescript // src/lib/langfuse.ts -- single file, import everywhere import { LangfuseClient } from "@langfuse/client"; import { LangfuseSpanProcessor } from "@langfuse/otel"; import { NodeSDK } from "@opentelemetry/sdk-node"; // Singleton client for prompts, datasets, scores let client: LangfuseClient | null = null; export function getLangfuseClient(): LangfuseClient { if (!client) { client = new LangfuseClient(); } return client; } // One-time OTel setup (call at app entry point) let sdk: NodeSDK | null = null; export function initTracing(): NodeSDK { if (!sdk) { sdk = new NodeSDK({ spanProcessors: [new LangfuseSpanProcessor()], }); sdk.start(); // Graceful shutdown on process exit const shutdown = async () => { await sdk?.shutdown(); process.exit(0); }; process.on("SIGTERM", shutdown); process.on("SIGINT", shutdown); } return sdk; } ``` **Legacy v3 singleton:** ```typescript import { Langfuse } from "langfuse"; let instance: L...

Details

Author
jeremylongshore
Repository
jeremylongshore/claude-code-plugins-plus-skills
Created
1 years ago
Last Updated
yesterday
Language
Python
License
MIT

Integrates with

Bundled in these plugins

Similar Skills

Semantically similar based on skill content — not just same category