langfuse-sdk-patterns
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".
What this skill does
# 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: Langfuse | null = null;
export function getLangfuse(): Langfuse {
if (!instance) {
instance = new Langfuse({
flushAt: 15,
flushInterval: 10000,
});
process.on("beforeExit", () => instance?.shutdownAsync());
}
return instance;
}
```
### Pattern 2: `observe` Wrapper for Existing Functions
The `observe` wrapper is the most ergonomic way to add tracing. It wraps any function and auto-creates a span.
```typescript
import { observe, updateActiveObservation } from "@langfuse/tracing";
// Wrap existing functions -- no internal changes needed
const fetchUserProfile = observe(async (userId: string) => {
updateActiveObservation({ input: { userId } });
const profile = await db.users.findById(userId);
updateActiveObservation({ output: { found: !!profile } });
return profile;
});
// Mark LLM calls as generations
const summarize = observe(
{ name: "summarize-text", asType: "generation" },
async (text: string) => {
updateActiveObservation({ model: "gpt-4o-mini", input: text });
const result = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: `Summarize: ${text}` }],
});
const output = result.choices[0].message.content;
updateActiveObservation({
output,
usage: {
promptTokens: result.usage?.prompt_tokens,
completionTokens: result.usage?.completion_tokens,
},
});
return output;
}
);
// When called inside another observed function, spans auto-nest
const pipeline = observe(async (userId: string) => {
const profile = await fetchUserProfile(userId);
const summary = await summarize(profile.bio);
return { profile, summary };
});
```
### Pattern 3: `startActiveObservation` for Inline Control
Use when you need fine-grained control over observation lifecycle within a function:
```typescript
import { startActiveObservation, updateActiveObservation } from "@langfuse/tracing";
async function processOrder(orderId: string) {
return await startActiveObservation("process-order", async () => {
updateActiveObservation({ input: { orderId } });
// Nested spans are automatic
const validated = await startActiveObservation("validate", async () => {
const result = await validateOrder(orderId);
updateActiveObservation({ output: { valid: result.valid } });
return result;
});
if (!validated.valid) {
updateActiveObservation({ output: { error: "validation failed" } });
return { success: false };
}
// Generation span for LLM call
const description = await startActiveObservation(
{ name: "generate-confirmation", asType: "generation" },
async () => {
updateActiveObservation({ model: "gpt-4o-mini" });
const result = await generateConfirmation(orderId);
updateActiveObservation({ output: result });
return result;
}
);
updateActiveObservation({ output: { success: true } });
return { success: true, description };
});
}
```
### Pattern 4: Session and User Tracking
Link traces across conversation turns for user-level analytics:
```typescript
// v4+: Set session/user via observation metadata
await startActiveObservation("chat-turn", async () => {
updateActiveObservation({
metadata: {
sessionId: "session-abc-123",
userId: "user-456",
},
});
// All nested observations inherit this context
await handleUserMessage(message);
});
// v3: Set directly on trace
const trace = langfuse.trace({
name: "chat-turn",
sessionId: "session-abc-123", // Groups traces into a session
userId: "user-456", // Links to user analytics
input: { message },
});
```
### Pattern 5: Error-Safe Tracing
Never let tracing failures break your application:
```typescript
import { observe, updateActiveObservation } from "@langfuse/tracing";
const safeObserve = <T extends (...args: any[]) => Promise<any>>(
name: string,
fn: T
): T => {
return (async (...args: Parameters<T>) => {
try {
return await observe({ name }, async () => {
updateActiveObservation({ input: args });
const result = await fn(...args);
updateActiveObservation({ output: result });
return result;
})();
} catch (tracingError) {
// If tracing fails, still run the function
console.warn(`Tracing error in ${name}:`, tracingError);
return fn(...args);
}
}) as T;
};
// Usage -- function works even if Langfuse is down
const processRequest = safeObserve("process-request", async (input: string) => {
return await callLLM(input);
});
```
### Pattern 6: Legacy v3 -- Always End Spans
```typescript
// Always use try/finally to ensure .end() is called
const span = trace.span({ name: "risky-operation", input: data });
try {
const result = await riskyOperation(data);
span.end({ output: result });
return result;
} catch (error) {
span.end({ level: "ERROR", statusMessage: String(error) });
throw error;
}
```
## Anti-Patterns to Avoid
| Anti-Pattern | Problem | Correct Pattern |
|-------------|---------|-----------------|
| `new Langfuse()` per request | Memory leaks, duplicate traces | Singleton client |
| Awaiting flush in hot path | Adds latency to every request | Background flush, shutdown handler |
| Logging full request bodies | Trace payloads too large | Truncate/summarize inputs |
| Missing `.end()` on spans (v3) | Spans show "in progress" forever | Use `try/finally` or `observe` wrapper |
| Hardcoding API keys | Security risk | Environment variables only |
## Resources
- [TypeScript SDK Instrumentation](https://langfuse.com/docs/observability/sdk/typescript/instrumentation)
- [Advanced SDK Configuration](https://langfuse.com/docs/observability/sdk/typescript/advanced-usage)
- [Python Decorators](https://langfuse.com/docs/sdk/python/decorators)
- [Event Queuing/Batching](https://langfuse.com/docs/observability/features/queuing-batching)
## Next Steps
For OpenAI/LangChain tracing examples, see `langfuse-core-workflow-a`.
Related in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.