---
name: langfuse-sdk-patterns-46f92807bc67
description: |
  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".
allowed-tools: Read, Write, Edit
version: 1.0.0
license: MIT
author: Jeremy Longshore <jeremy@intentsolutions.io>
---

# Langfuse SDK Patterns

## Overview
Best practices and idiomatic patterns for using the Langfuse SDK effectively.

## Prerequisites
- Completed `langfuse-install-auth` setup
- Understanding of async/await patterns
- Familiarity with LLM application structure

## Core Concepts

### Trace Hierarchy
```
Trace (top-level, represents complete operation)
├── Span (child operation, any processing step)
│   ├── Span (nested spans allowed)
│   └── Generation (LLM call)
├── Generation (LLM call)
└── Event (point-in-time occurrence)
```

## Instructions

### Pattern 1: Singleton Client Instance

```typescript
// lib/langfuse.ts - Single instance, reused everywhere
import { Langfuse } from "langfuse";

let langfuseInstance: Langfuse | null = null;

export function getLangfuse(): Langfuse {
  if (!langfuseInstance) {
    langfuseInstance = new Langfuse({
      publicKey: process.env.LANGFUSE_PUBLIC_KEY!,
      secretKey: process.env.LANGFUSE_SECRET_KEY!,
      baseUrl: process.env.LANGFUSE_HOST,
    });
  }
  return langfuseInstance;
}

// Clean shutdown
export async function shutdownLangfuse(): Promise<void> {
  if (langfuseInstance) {
    await langfuseInstance.shutdownAsync();
    langfuseInstance = null;
  }
}
```

### Pattern 2: Proper Trace Lifecycle

```typescript
import { getLangfuse } from "./lib/langfuse";

async function handleRequest(request: Request) {
  const langfuse = getLangfuse();

  // 1. Create trace at operation start
  const trace = langfuse.trace({
    name: "api/chat",
    userId: request.userId,
    sessionId: request.sessionId,
    input: request.body,
    metadata: {
      endpoint: "/api/chat",
      method: "POST",
    },
  });

  try {
    // 2. Do work with spans/generations
    const result = await processRequest(trace, request);

    // 3. Update trace with success output
    trace.update({
      output: result,
      metadata: { status: "success" },
    });

    return result;
  } catch (error) {
    // 4. Update trace with error
    trace.update({
      output: { error: String(error) },
      level: "ERROR",
      statusMessage: String(error),
    });
    throw error;
  }
  // Note: Don't await flush here in request handlers
  // Langfuse batches and flushes automatically
}
```

### Pattern 3: Nested Spans for Complex Operations

```typescript
async function processRequest(
  trace: ReturnType<typeof langfuse.trace>,
  request: Request
) {
  // Parent span for the entire process
  const processSpan = trace.span({
    name: "process-request",
    input: request.body,
  });

  // Child span for validation
  const validateSpan = processSpan.span({
    name: "validate-input",
    input: request.body,
  });

  const validatedInput = await validateInput(request.body);
  validateSpan.end({ output: validatedInput });

  // Child span for retrieval (e.g., RAG)
  const retrieveSpan = processSpan.span({
    name: "retrieve-context",
    input: { query: validatedInput.query },
  });

  const context = await retrieveContext(validatedInput.query);
  retrieveSpan.end({
    output: { documentCount: context.length },
    metadata: { source: "vector-db" },
  });

  // Generation for LLM call
  const generation = processSpan.generation({
    name: "generate-response",
    model: "gpt-4",
    input: {
      messages: buildMessages(validatedInput, context),
    },
  });

  const response = await callLLM(validatedInput, context);

  generation.end({
    output: response.content,
    usage: {
      promptTokens: response.usage.prompt_tokens,
      completionTokens: response.usage.completion_tokens,
    },
  });

  processSpan.end({ output: response });
  return response;
}
```

### Pattern 4: Decorators for Clean Code (Python)

```python
from langfuse.decorators import observe, langfuse_context

@observe()
def process_request(user_input: str) -> str:
    """Automatically creates a trace with function name."""
    # Add metadata to current observation
    langfuse_context.update_current_observation(
        metadata={"input_length": len(user_input)}
    )

    validated = validate_input(user_input)
    context = retrieve_context(validated)
    response = generate_response(validated, context)

    return response

@observe()
def validate_input(user_input: str) -> dict:
    """Creates a child span automatically."""
    return {"query": user_input.strip(), "valid": True}

@observe()
def retrieve_context(query: dict) -> list:
    """Another child span."""
    langfuse_context.update_current_observation(
        metadata={"query": query}
    )
    return ["context1", "context2"]

@observe(as_type="generation")
def generate_response(query: dict, context: list) -> str:
    """Creates a generation observation."""
    langfuse_context.update_current_observation(
        model="gpt-4",
        model_parameters={"temperature": 0.7},
        usage={"prompt_tokens": 100, "completion_tokens": 50},
    )
    return "Generated response"
```

### Pattern 5: Session and User Tracking

```typescript
// Track conversations across multiple requests
function createConversationTrace(
  userId: string,
  sessionId: string,
  turn: number
) {
  return langfuse.trace({
    name: "conversation-turn",
    userId,
    sessionId, // Links traces into a session view
    metadata: {
      turn,
      timestamp: new Date().toISOString(),
    },
    tags: ["conversation"],
  });
}

// Usage
const trace = createConversationTrace(
  "user-123",
  "session-abc", // Same session across turns
  3 // Turn number
);
```

### Pattern 6: Scores and Evaluation

```typescript
// Add scores to traces for evaluation
const trace = langfuse.trace({ name: "scored-operation" });

// After operation completes, add scores
langfuse.score({
  traceId: trace.id,
  name: "accuracy",
  value: 0.95, // Numeric score
  comment: "High accuracy response",
});

langfuse.score({
  traceId: trace.id,
  name: "user-feedback",
  value: 1, // Boolean as 0/1
  comment: "User thumbs up",
});

langfuse.score({
  traceId: trace.id,
  observationId: generation.id, // Score specific generation
  name: "relevance",
  value: 0.8,
});
```

## Output
- Singleton client pattern for consistent tracing
- Proper trace lifecycle management
- Nested spans for complex operations
- Clean decorator-based tracing (Python)
- Session and user tracking
- Evaluation and scoring integration

## Error Handling
| Pattern | Issue | Best Practice |
|---------|-------|---------------|
| Client creation | Multiple instances | Use singleton pattern |
| Trace updates | Missing outputs | Always update on success/error |
| Span nesting | Orphaned spans | Always call `.end()` |
| Flush timing | Lost data | Use `shutdownAsync()` on exit |
| Scoring | Invalid values | Use 0-1 range for consistency |

## Examples

### Complete TypeScript Pattern
```typescript
import { Langfuse } from "langfuse";

const langfuse = new Langfuse();

interface ChatRequest {
  userId: string;
  sessionId: string;
  message: string;
}

async function chat(request: ChatRequest) {
  const trace = langfuse.trace({
    name: "chat",
    userId: request.userId,
    sessionId: request.sessionId,
    input: { message: request.message },
  });

  const span = trace.span({ name: "process" });

  try {
    const generation = span.generation({
      name: "llm-call",
      model: "gpt-4",
      input: [{ role: "user", content: request.message }],
    });

    const response = await callOpenAI(request.message);

    generation.end({
      output: response.content,
      usage: response.usage,
    });

    span.end({ output: { response: response.content } });
    trace.update({ output: { response: response.content } });

    return response.content;
  } catch (error) {
    span.end({ level: "ERROR", statusMessage: String(error) });
    trace.update({ level: "ERROR", output: { error: String(error) } });
    throw error;
  }
}
```

## Resources
- [Langfuse Tracing Guide](https://langfuse.com/docs/tracing)
- [Langfuse Python Decorators](https://langfuse.com/docs/sdk/python/decorators)
- [Langfuse Scores](https://langfuse.com/docs/scores)

## Next Steps
For core tracing workflows, see `langfuse-core-workflow-a`.


---
Source attribution: Dicklesworthstone
Original project: https://github.com/Dicklesworthstone/pi_agent_rust/blob/f28fc46ee0db8e710d95b8b55592cd38d60f2b26/tests/ext_conformance/artifacts/plugins-community/plugins/saas-packs/langfuse-pack/skills/langfuse-sdk-patterns/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License (with OpenAI/Anthropic Rider)

Copyright (c) 2026 Jeffrey Emanuel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

ADDITIONAL RIDER / RESTRICTION (OpenAI / Anthropic):

This rider is part of the "conditions" of this License. In the event of any
conflict between this rider and any other portion of this License, this rider
controls.

"Restricted Parties" means OpenAI, L.L.C.; Anthropic, PBC; any of their
respective Affiliates; and any person or entity acting directly or indirectly
on behalf of, for the benefit of, or under the direction of any of the
foregoing (including any officer, director, employee, contractor, agent,
consultant, service provider, or representative).

Notwithstanding any other provision of this License, no rights are granted to
any Restricted Party. Any purported license, sublicense, assignment, transfer,
or other permission to any Restricted Party is null and void absent the
express prior written permission of Jeffrey Emanuel.

You may not provide, disclose, distribute, sublicense, sell, lease, lend,
host, make available, or otherwise permit access to the Software or any
derivative work of the Software (as defined in applicable copyright law)
(collectively, "Derivative Works") to or for any Restricted Party.

For purposes of this rider, "use" includes, without limitation: copying,
modifying, merging, publishing, distributing, sublicensing, selling,
transferring, making available, hosting, deploying, executing, benchmarking,
testing, analyzing, indexing, or incorporating the Software or any Derivative
Works into any dataset, training corpus, evaluation harness, or pipeline for
machine learning or other automated systems.

This rider applies to the Software and all Derivative Works. As a condition of
use, you agree that this rider is a precondition to exercising any rights
under this License, and you agree that any distribution of the Software or any
Derivative Works must include this rider provision unmodified.

Any breach of this rider automatically and immediately terminates the
permissions granted by this License. Upon termination, you must immediately
cease all use and distribution of the Software and any Derivative Works and
destroy all copies under your control.

You agree that a breach of this rider would cause irreparable harm and that
Jeffrey Emanuel may seek injunctive or other equitable relief to enforce this
rider, in addition to any other remedies available at law. To the maximum
extent permitted by applicable law, the prevailing party in any action to
enforce this rider shall be entitled to recover reasonable attorneys' fees and
costs.

For purposes of this rider, "Affiliate" means any entity that directly or
indirectly controls, is controlled by, or is under common control with the
specified party. "Control" means ownership of more than 50% of the voting
securities or other ownership interest, or the power to direct management or
policies by contract or otherwise.

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: Dicklesworthstone
Original project: https://github.com/Dicklesworthstone/pi_agent_rust/blob/f28fc46ee0db8e710d95b8b55592cd38d60f2b26/tests/ext_conformance/artifacts/plugins-community/plugins/saas-packs/langfuse-pack/skills/langfuse-sdk-patterns/SKILL.md
Attribution basis: Source repository record

Fields of work: Data & Analytics, Development & Engineering
Roles: Backend Developer, Data Scientist, ML / AI Engineer
Specialties: Backend
Tools: Langfuse
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: a59e4614b3fa0bca817da58d4059415c931b4e73bf4a4daf17c1621877767386
