Langfuse JS/TS SDKs
    Preparing search index...

    Function startObservation

    Creates and starts a new Langfuse observation with automatic TypeScript type inference.

    This is the primary method for creating observations in Langfuse. It supports multiple observation types with full TypeScript type safety - the return type is automatically inferred based on the asType parameter.

    • span (default): General-purpose operations, functions, or workflows
    • generation: LLM calls, text generation, or AI model interactions
    • embedding: Text embedding generation or vector operations
    • agent: AI agent workflows with tool usage and decision making
    • tool: Individual tool calls, API requests, or function invocations
    • chain: Multi-step processes like RAG pipelines or sequential operations
    • retriever: Document retrieval, vector search, or knowledge base queries
    • evaluator: Quality assessment, scoring, or evaluation operations
    • guardrail: Safety checks, content filtering, or validation operations
    • event: Point-in-time occurrences or log entries (automatically ended)

    Descriptive name for the observation (e.g., 'openai-gpt-4', 'vector-search')

    Type-specific attributes (input, output, metadata, etc.)

    Configuration options including observation type and timing

    Strongly-typed observation object based on asType parameter

    import { startObservation } from '@langfuse/tracing';

    // Span for general operations (default)
    const span = startObservation('user-workflow', {
    input: { userId: '123', action: 'checkout' },
    metadata: { version: '2.1.0', feature: 'new-checkout' }
    });
    span.update({ output: { success: true, orderId: '456' } });
    span.end();

    // Generation for LLM interactions
    const generation = startObservation('openai-gpt-4', {
    input: [{ role: 'user', content: 'Explain quantum computing' }],
    model: 'gpt-4-turbo',
    modelParameters: { temperature: 0.7, maxTokens: 500 }
    }, { asType: 'generation' });

    generation.update({
    output: { role: 'assistant', content: 'Quantum computing...' },
    usageDetails: { promptTokens: 12, completionTokens: 150, totalTokens: 162 },
    costDetails: { totalCost: 0.002, currency: 'USD' }
    });
    generation.end();

    // Agent for AI workflows with tools
    const agent = startObservation('research-agent', {
    input: { query: 'Latest developments in renewable energy' },
    metadata: { tools: ['web-search', 'pdf-reader'], model: 'gpt-4' }
    }, { asType: 'agent' });

    // Tool for individual API calls
    const weatherTool = startObservation('weather-api', {
    input: { location: 'San Francisco', units: 'metric' },
    metadata: { provider: 'openweather', timeout: 5000 }
    }, { asType: 'tool' });

    // Chain for multi-step RAG pipeline
    const ragChain = startObservation('rag-qa-pipeline', {
    input: { question: 'How does photosynthesis work?' },
    metadata: { steps: ['retrieve', 'rerank', 'generate'], vectorDb: 'pinecone' }
    }, { asType: 'chain' });

    // Retriever for vector search
    const retriever = startObservation('vector-search', {
    input: { query: 'machine learning algorithms', topK: 5 },
    metadata: { vectorStore: 'chroma', similarity: 'cosine' }
    }, { asType: 'retriever' });

    // Evaluator for quality assessment
    const evaluator = startObservation('quality-check', {
    input: {
    response: 'Paris is the capital of France',
    reference: 'The capital city of France is Paris'
    },
    metadata: { metric: 'semantic-similarity', threshold: 0.8 }
    }, { asType: 'evaluator' });

    // Guardrail for content filtering
    const guardrail = startObservation('content-filter', {
    input: { text: 'User message content', policies: ['no-profanity', 'no-pii'] },
    metadata: { strictMode: true, version: 'v2' }
    }, { asType: 'guardrail' });

    // Embedding for text vectorization
    const embedding = startObservation('text-embedder', {
    input: { texts: ['Hello world', 'Machine learning'] },
    model: 'text-embedding-ada-002',
    metadata: { dimensions: 1536 }
    }, { asType: 'embedding' });

    // Event for point-in-time occurrences (auto-ended)
    const event = startObservation('user-login', {
    input: { userId: '123', method: 'oauth' },
    level: 'DEFAULT',
    metadata: { ip: '192.168.1.1', userAgent: 'Chrome/120.0' }
    }, { asType: 'event' });

    // Nested observations with parent context
    const parentSpan = startObservation('ai-pipeline');
    const childRetriever = startObservation('doc-search', {
    input: { query: 'AI safety' }
    }, {
    asType: 'retriever',
    parentSpanContext: parentSpan.otelSpan.spanContext()
    });