Skip to content

๐Ÿ” LangFuse Integration Guide

Learn how to integrate SuperOptiX with LangFuse for comprehensive LLM observability, tracing, and performance monitoring. This guide covers the complete setup process from installation to production deployment.

๐ŸŽฏ Overview

LangFuse is a modern observability platform specifically designed for LLM applications. This integration provides:

  • Real-time LLM tracing with detailed token usage
  • Performance monitoring and cost tracking
  • User feedback collection and evaluation
  • A/B testing for different agent configurations
  • Production debugging with full trace visibility

๐Ÿš€ Installation & Setup

Step 1: Install LangFuse Python SDK

Bash
# Install LangFuse Python SDK
pip install langfuse

# Verify installation
python -c "import langfuse; print('LangFuse SDK installed successfully')"

Step 2: Set Up LangFuse Locally

Create a docker-compose.yml file for local LangFuse deployment:

YAML
# docker-compose.yml
version: '3.8'
services:
  langfuse:
    image: langfuse/langfuse:latest
    container_name: langfuse
    ports:
      - "3000:3000"
    environment:
      - LANGFUSE_SECRET_KEY=your-secret-key
      - LANGFUSE_PUBLIC_KEY=your-public-key
      - LANGFUSE_HOST=http://localhost:3000
    volumes:
      - langfuse_data:/app/data
    restart: unless-stopped

volumes:
  langfuse_data:

Start LangFuse locally:

Bash
# Start LangFuse with Docker Compose
docker compose up -d

# Verify LangFuse is running
curl http://localhost:3000/api/public/health

Expected output:

JSON
{"status":"OK","version":"3.81.0"}

Step 3: Initialize SuperOptiX Project

Bash
# Initialize new SuperOptiX project
super init langfuse_demo
cd langfuse_demo

# Pull developer agent with Genies tier
super agent pull developer --tier genies

Expected output:

Text Only
๐Ÿš€ Enhancing agent 'developer' for Genies tier...
  โœ… Model configured for Genies tier: llama3.1:8b
  โœ… ReAct configuration added
  โœ… Default toolset added (calculator, text_analyzer, file_reader)
  โœ… Memory system configured
  โœ… Preserving optimization and testing sections
================================================================================

๐Ÿค– Adding agent 'developer'...
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ ๐ŸŽ‰ AGENT ADDED SUCCESSFULLY! Pre-built Agent Ready                                                       โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Step 4: Configure LangFuse Observability

Update the agent playbook (langfuse_demo/agents/developer/playbook/developer_playbook.yaml) to include LangFuse configuration:

YAML
# Add this section to your agent playbook
observability:
  enabled: true
  backends:
    - langfuse
  langfuse:
    public_key: "pk-lf-5218b891-1fd7-4b59-9ce6-4dcfb66c2414"  # Your public key
    secret_key: "sk-lf-xxxxxx"  # Your secret key
    host: "http://localhost:3000"
    project: "superoptix-agents"
    tags:
      agent_type: "developer"
      tier: "genies"
      environment: "development"

Step 5: Compile Agent with LangFuse Support

Bash
# Compile the agent with LangFuse observability
super agent compile developer

Expected output:

Text Only
๐Ÿ”จ Compiling agent 'developer'...
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โšก Compilation Details โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚                                                                                                          โ”‚
โ”‚  ๐Ÿค– COMPILATION IN PROGRESS                                                                              โ”‚
โ”‚                                                                                                          โ”‚
โ”‚  ๐ŸŽฏ Agent: Developer Assistant                                                                           โ”‚
โ”‚  ๐Ÿ—๏ธ Framework: DSPy (default) Junior Pipeline โ€” other frameworks coming soon
 โ”‚
โ”‚  ๐Ÿ”ง Process: YAML playbook โ†’ Executable Python pipeline                                                  โ”‚
โ”‚  ๐Ÿ“ Output: langfuse_demo/agents/developer/pipelines/developer_pipeline.py                               โ”‚
โ”‚                                                                                                          โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
โœ… Successfully generated Genies-tier pipeline (mixin) at: 
/Users/shashi/superagentic/SuperOptiX/langfuse_demo/langfuse_demo/agents/developer/pipelines/developer_pipeline.py

๐Ÿงช Testing the Integration

Step 1: Create LangFuse Integration Test

Create test_langfuse_integration.py:

Python
#!/usr/bin/env python3
"""
Test LangFuse Integration with SuperOptiX
This script demonstrates how to use LangFuse with the local instance.
"""

from langfuse import Langfuse
import json
from datetime import datetime

def test_langfuse_integration():
    """Test LangFuse integration with local instance."""

    # Initialize LangFuse client for local instance
    langfuse = Langfuse(
        public_key="pk-lf-xxxxx",
        secret_key="sk-lf-c172306b-0e77-47a3-b365-07314c6c40e0",
        host="http://localhost:3000"
    )

    print("๐Ÿงช Testing LangFuse integration with local instance")

    try:
        # Create a trace using context manager
        with langfuse.start_as_current_span(
            name="superoptix_agent_execution",
            metadata={
                "agent_type": "developer",
                "tier": "genies",
                "model": "llama3.1:8b"
            }
        ) as trace:

            print(f"โœ… Created trace: {langfuse.get_current_trace_id()}")

            # Create a generation span for model call
            with langfuse.start_as_current_generation(
                name="llm_call",
                model="llama3.1:8b",
                input={
                    "prompt": "Write a Python function to calculate factorial",
                    "temperature": 0.1
                },
                model_parameters={
                    "temperature": 0.1,
                    "max_tokens": 500
                }
            ) as generation:

                print(f"โœ… Created generation span: {langfuse.get_current_observation_id()}")

                # Simulate model response
                response = "def factorial(n):\n    if n <= 1:\n        return 1\n    return n * factorial(n-1)"

                # Update generation with output
                generation.update(
                    output=response,
                    usage_details={
                        "prompt_tokens": 10,
                        "completion_tokens": 45
                    }
                )

                # Score the generation
                langfuse.score_current_span(
                    name="code_quality",
                    value=0.85,
                    comment="Good code quality with proper recursion"
                )

                langfuse.score_current_span(
                    name="execution_time",
                    value=2.5,
                    comment="Fast execution time"
                )

                print("โœ… Logged scores")

            # Score the overall trace
            langfuse.score_current_trace(
                name="overall_quality",
                value=0.9,
                comment="High quality agent execution"
            )

            print("โœ… Completed trace successfully")

        # Force flush to ensure data is sent
        langfuse.flush()

        print("๐Ÿ“Š Trace data sent to LangFuse successfully")

        return True

    except Exception as e:
        print(f"โŒ Error testing LangFuse: {e}")
        return False

def check_langfuse_connection():
    """Check if LangFuse server is accessible."""
    try:
        import requests
        response = requests.get("http://localhost:3000/api/public/health")
        if response.status_code == 200:
            print("โœ… LangFuse server is running and accessible")
            return True
        else:
            print(f"โŒ LangFuse server returned status code: {response.status_code}")
            return False
    except Exception as e:
        print(f"โŒ Cannot connect to LangFuse server: {e}")
        return False

if __name__ == "__main__":
    print("๐Ÿงช SuperOptiX LangFuse Integration Test")
    print("=" * 50)

    # Check LangFuse connection
    if check_langfuse_connection():
        # Run the test
        success = test_langfuse_integration()
        if success:
            print(f"\n๐ŸŽ‰ Test completed successfully!")
            print(f"๐ŸŒ View results at: http://localhost:3000")
        else:
            print(f"\nโŒ Test failed")
    else:
        print("\nโŒ LangFuse server is not accessible.")
        print("   Make sure Docker is running and LangFuse is started:")
        print("   docker compose up -d")

Step 2: Run the Integration Test

Bash
# Run the LangFuse integration test
python test_langfuse_integration.py

Expected output:

Text Only
๐Ÿงช SuperOptiX LangFuse Integration Test
==================================================
โœ… LangFuse server is running and accessible
๐Ÿงช Testing LangFuse integration with local instance
โœ… Created trace: 5ec5a318e2d5fe2069d826866ce8624e
โœ… Created generation span: fcb95e33f3176269
โœ… Logged scores
โœ… Completed trace successfully
๐Ÿ“Š Trace data sent to LangFuse successfully

๐ŸŽ‰ Test completed successfully!
๐ŸŒ View results at: http://localhost:3000

Step 3: Test SuperOptiX Agent with LangFuse

Bash
# Run agent with LangFuse tracing enabled
super agent run developer --goal "Write a Python function to calculate the Fibonacci sequence"

Expected output:

Text Only
๐Ÿš€ Running agent 'developer'...
๐Ÿ” Tracing enabled for agent developer_20250714_212620
๐Ÿ“ Traces will be stored in: /Users/shashi/superagentic/SuperOptiX/langfuse_demo/.superoptix/traces
๐Ÿš€ Configuring llama3.1:8b with ollama for genies-tier capabilities
โœ… Model connection successful: ollama/llama3.1:8b
โœ… 3 tools configured successfully
โœ… ReAct agent configured with 3 tools
๐Ÿ“‹ Loaded 5 BDD specifications for execution
โœ… DeveloperPipeline (Genie tier) initialized with ReAct and 5 BDD scenarios

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Agent Execution โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ ๐Ÿค– Running Developer Pipeline                                                                            โ”‚
โ”‚                                                                                                          โ”‚
โ”‚ Executing Task: Write a Python function to calculate the Fibonacci sequence                              โ”‚
โ”‚                                                                                                          โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Aspect         โ”ƒ Value                                                                                   โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ Implementation โ”‚ Python function implementation with proper logic                                        โ”‚
โ”‚ Reasoning      โ”‚ Step-by-step reasoning process with tool usage                                         โ”‚
โ”‚ Success        โ”‚ True                                                                                    โ”‚
โ”‚ Execution_Time โ”‚ 18.71 seconds                                                                           โ”‚
โ”‚ Agent_Id       โ”‚ developer_20250714_212620                                                               โ”‚
โ”‚ Tier           โ”‚ genies                                                                                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŽ‰ Agent execution completed successfully!

Step 4: Check Observability Data

Bash
# List agents with traces
super observe list

Expected output:

Text Only
๐Ÿ“‹ Available Agents with Traces

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Agent ID                  โ”ƒ Trace Count โ”ƒ Last Activity       โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ developer                 โ”‚ 2           โ”‚ 2025-07-14 21:26:39 โ”‚
โ”‚ developer_20250714_212620 โ”‚ 23          โ”‚ 2025-07-14 21:26:39 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Bash
# Check trace configuration
super observe check

Expected output:

Text Only
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Trace Check Configuration โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ ๐Ÿ” Pipeline Trace Analysis                                                                               โ”‚
โ”‚                                                                                                          โ”‚
โ”‚ Agent ID: All agents                                                                                     โ”‚
โ”‚ Run Test: No                                                                                             โ”‚
โ”‚ Check DSPy: No                                                                                           โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
๐Ÿ“ Checking for trace files...
โœ… Found 6 potential trace files:
   ๐Ÿ“„ .superoptix/traces (224 bytes)
   ๐Ÿ“„ .superoptix/traces/developer_20250714_212748.jsonl (8684 bytes)
   ๐Ÿ“„ .superoptix/traces/developer_20250714_212620.jsonl (11356 bytes)
   ๐Ÿ“„ .superoptix/traces/developer_20250714_212830.jsonl (7719 bytes)
   ๐Ÿ“„ .superoptix/traces/developer.jsonl (9340 bytes)
   ๐Ÿ“„ .superoptix/traces/developer_20250714_212809.jsonl (8297 bytes)
โœ… SuperOptiX trace directory found: .superoptix/traces
   ๐Ÿ“Š Found 5 trace files

๐Ÿ“Š Demo Results

Comprehensive Demo Script

Create demo_langfuse_superoptix.py for a complete demonstration:

Python
#!/usr/bin/env python3
"""
Comprehensive LangFuse Integration Demo with SuperOptiX
This script demonstrates the complete LangFuse integration workflow.
"""

import subprocess
import time
import json
import os
from datetime import datetime

def run_command(command, description):
    """Run a command and return the result."""
    print(f"\n๐Ÿ”ง {description}")
    print(f"Command: {command}")
    print("-" * 50)

    try:
        result = subprocess.run(command, shell=True, capture_output=True, text=True)
        print(f"Exit Code: {result.returncode}")
        if result.stdout:
            print("Output:")
            print(result.stdout)
        if result.stderr:
            print("Errors:")
            print(result.stderr)
        return result.returncode == 0
    except Exception as e:
        print(f"Error running command: {e}")
        return False

def check_langfuse_status():
    """Check if LangFuse is running."""
    print("๐Ÿ” Checking LangFuse Status")
    print("=" * 50)

    # Check if Docker containers are running
    result = subprocess.run("docker ps --filter 'name=langfuse' --format 'table {{.Names}}\t{{.Status}}'", 
                           shell=True, capture_output=True, text=True)

    if "langfuse" in result.stdout:
        print("โœ… LangFuse containers are running:")
        print(result.stdout)
        return True
    else:
        print("โŒ LangFuse containers are not running")
        print("Starting LangFuse...")
        run_command("docker compose up -d", "Starting LangFuse with Docker Compose")
        time.sleep(10)  # Wait for services to start
        return True

def demo_langfuse_integration():
    """Demonstrate LangFuse integration with SuperOptiX."""
    print("\n๐Ÿš€ LangFuse Integration Demo")
    print("=" * 50)

    # Step 1: Check LangFuse status
    if not check_langfuse_status():
        print("โŒ Failed to start LangFuse")
        return False

    # Step 2: Run the LangFuse test script
    print("\n๐Ÿ“ Step 1: Testing LangFuse API Integration")
    success = run_command("python test_langfuse_integration.py", 
                         "Running LangFuse API integration test")

    if not success:
        print("โŒ LangFuse API test failed")
        return False

    # Step 3: Run SuperOptiX agent with LangFuse observability
    print("\n๐Ÿค– Step 2: Running SuperOptiX Agent with LangFuse")
    goals = [
        "Write a Python function to calculate the sum of all even numbers in a list",
        "Create a simple web scraper function in Python",
        "Write a function to validate email addresses using regex"
    ]

    for i, goal in enumerate(goals, 1):
        print(f"\n๐ŸŽฏ Running agent with goal {i}: {goal}")
        success = run_command(f'super agent run developer --goal "{goal}"', 
                             f"Running agent with goal {i}")

        if not success:
            print(f"โŒ Agent execution {i} failed")
            continue

        # Wait a bit between runs
        time.sleep(2)

    # Step 4: Check observability data
    print("\n๐Ÿ“Š Step 3: Checking Observability Data")

    # List agents with traces
    run_command("super observe list", "Listing agents with traces")

    # Check trace configuration
    run_command("super observe check", "Checking trace configuration")

    # Analyze performance
    run_command("super observe analyze", "Analyzing agent performance")

    # Step 5: Show LangFuse UI information
    print("\n๐ŸŒ Step 4: LangFuse UI Access")
    print("=" * 50)
    print("โœ… LangFuse is running locally!")
    print("๐ŸŒ Access the LangFuse UI at: http://localhost:3000")
    print("๐Ÿ“Š View traces, spans, and generations in real-time")
    print("๐Ÿ“ˆ Monitor agent performance and quality metrics")
    print("๐Ÿ” Debug and analyze agent behavior")

    return True

def create_demo_summary():
    """Create a summary of the demo."""
    print("\n๐Ÿ“‹ Demo Summary")
    print("=" * 50)

    summary = {
        "demo_name": "LangFuse Integration with SuperOptiX",
        "date": datetime.now().isoformat(),
        "components_tested": [
            "LangFuse local instance setup",
            "LangFuse Python SDK integration",
            "SuperOptiX agent execution with tracing",
            "Observability data collection",
            "Performance analysis"
        ],
        "key_features": [
            "Real-time trace collection",
            "Span and generation tracking",
            "Performance metrics",
            "Quality scoring",
            "Debugging capabilities"
        ],
        "next_steps": [
            "Explore traces in LangFuse UI",
            "Set up custom scoring metrics",
            "Configure alerts and notifications",
            "Integrate with production workflows",
            "Set up team collaboration features"
        ]
    }

    print(json.dumps(summary, indent=2))

    # Save summary to file
    with open("langfuse_demo_summary.json", "w") as f:
        json.dump(summary, f, indent=2)

    print(f"\n๐Ÿ’พ Demo summary saved to: langfuse_demo_summary.json")

def main():
    """Main demo function."""
    print("๐ŸŽ‰ LangFuse + SuperOptiX Integration Demo")
    print("=" * 60)
    print("This demo showcases the complete integration between")
    print("LangFuse observability platform and SuperOptiX agents.")
    print("=" * 60)

    # Run the demo
    success = demo_langfuse_integration()

    if success:
        print("\nโœ… Demo completed successfully!")
        create_demo_summary()

        print("\n๐ŸŽฏ What's Next?")
        print("-" * 30)
        print("1. ๐ŸŒ Open http://localhost:3000 to explore traces")
        print("2. ๐Ÿ“Š Analyze agent performance in LangFuse UI")
        print("3. ๐Ÿ”ง Configure custom metrics and alerts")
        print("4. ๐Ÿš€ Deploy to production with real API keys")
        print("5. ๐Ÿ‘ฅ Set up team collaboration features")

    else:
        print("\nโŒ Demo encountered issues")
        print("Check the output above for error details")

    print("\n" + "=" * 60)
    print("๐ŸŽ‰ Thank you for trying LangFuse + SuperOptiX integration!")

if __name__ == "__main__":
    main()

Run the Comprehensive Demo

Bash
# Run the complete LangFuse integration demo
python demo_langfuse_superoptix.py

๐Ÿ“Š Results Achieved

Trace Collection Statistics

Text Only
๐Ÿ“‹ Available Agents with Traces
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Agent ID                  โ”ƒ Trace Count โ”ƒ Last Activity       โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ developer                 โ”‚ 8           โ”‚ 2025-07-14 21:28:52 โ”‚
โ”‚ developer_20250714_212620 โ”‚ 23          โ”‚ 2025-07-14 21:26:39 โ”‚
โ”‚ developer_20250714_212748 โ”‚ 23          โ”‚ 2025-07-14 21:28:03 โ”‚
โ”‚ developer_20250714_212809 โ”‚ 23          โ”‚ 2025-07-14 21:28:24 โ”‚
โ”‚ developer_20250714_212830 โ”‚ 20          โ”‚ 2025-07-14 21:28:52 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Agent Performance Metrics

Text Only
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Aspect         โ”ƒ Value                                                                                   โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ Implementation โ”‚ Python function implementation with proper logic                                        โ”‚
โ”‚ Reasoning      โ”‚ Step-by-step reasoning process with tool usage                                         โ”‚
โ”‚ Success        โ”‚ True                                                                                    โ”‚
โ”‚ Execution_Time โ”‚ 15.53 seconds                                                                           โ”‚
โ”‚ Agent_Id       โ”‚ developer_20250714_212748                                                               โ”‚
โ”‚ Tier           โ”‚ genies                                                                                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

API Integration Test Results

Text Only
๐Ÿงช SuperOptiX LangFuse Integration Test
==================================================
โœ… LangFuse server is running and accessible
๐Ÿงช Testing LangFuse integration with local instance
โœ… Created trace: 5ec5a318e2d5fe2069d826866ce8624e
โœ… Created generation span: fcb95e33f3176269
โœ… Logged scores
โœ… Completed trace successfully
๐Ÿ“Š Trace data sent to LangFuse successfully

๐ŸŽ‰ Test completed successfully!
๐ŸŒ View results at: http://localhost:3000

๐ŸŒ LangFuse UI Access

Access the LangFuse Dashboard

Open your browser and navigate to: http://localhost:3000

Available Features in LangFuse UI

  • ๐Ÿ“Š Real-time Traces: View all agent execution traces
  • ๐Ÿ” Span Analysis: Detailed span and generation information
  • ๐Ÿ“ˆ Performance Metrics: Execution time and quality scores
  • ๐ŸŽฏ Quality Scoring: Custom metrics and evaluations
  • ๐Ÿ”ง Debugging Tools: Trace comparison and analysis
  • ๐Ÿ‘ฅ Team Collaboration: Share traces and insights

๐Ÿ”ง Advanced Configuration

Custom Trace Attributes

Add custom attributes to traces in your agent playbook:

YAML
observability:
  langfuse:
    custom_attributes:
      - name: "code_complexity"
        type: "number"
        description: "Complexity score of generated code"
      - name: "user_expertise"
        type: "string"
        description: "User expertise level"
      - name: "task_difficulty"
        type: "number"
        description: "Task difficulty rating"

User Feedback Integration

Collect and track user feedback:

YAML
observability:
  langfuse:
    feedback:
      enabled: true
      score_range: [1, 5]
      comment_enabled: true
      categories:
        - "accuracy"
        - "helpfulness"
        - "code_quality"

A/B Testing Support

Configure A/B testing for different agent configurations:

YAML
observability:
  langfuse:
    ab_testing:
      enabled: true
      variants:
        - name: "baseline"
          config:
            temperature: 0.7
            model: "llama3.1:8b"
        - name: "optimized"
          config:
            temperature: 0.5
            model: "llama3.1:70b"

๐Ÿš€ Production Deployment

Environment Configuration

Set up production environment variables:

Bash
# Production environment
export LANGFUSE_PUBLIC_KEY="your-production-public-key"
export LANGFUSE_SECRET_KEY="your-production-secret-key"
export LANGFUSE_HOST="https://cloud.langfuse.com"
export LANGFUSE_PROJECT="production-agents"

LangFuse Cloud Setup

  1. Sign up at cloud.langfuse.com
  2. Create a new project for your SuperOptiX agents
  3. Get your API keys from the project settings
  4. Update your agent playbook with production keys
  5. Deploy your agents with LangFuse integration

Kubernetes Integration

For production deployments, use Kubernetes secrets:

YAML
# langfuse-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: langfuse-credentials
type: Opaque
data:
  public-key: <base64-encoded-public-key>
  secret-key: <base64-encoded-secret-key>

๐Ÿ”ง Troubleshooting

Common Issues

1. LangFuse Server Not Accessible

Bash
# Check if Docker containers are running
docker ps --filter 'name=langfuse'

# Restart LangFuse if needed
docker compose down
docker compose up -d

2. Authentication Errors

Bash
# Verify API keys
curl -H "Authorization: Bearer your-secret-key" \
     http://localhost:3000/api/public/traces

3. Missing Traces

YAML
# Enable debug logging in your agent playbook
observability:
  langfuse:
    debug: true
    log_level: "DEBUG"
    flush_interval: 5  # seconds

Debug Mode

Enable comprehensive debugging:

YAML
observability:
  langfuse:
    debug: true
    log_level: "DEBUG"
    verbose: true
    test_mode: true  # For development

๐Ÿ“Š Cost Tracking

Token Usage Monitoring

Track and optimize token usage:

Python
# Cost analysis script
def analyze_costs():
    traces = langfuse.get_traces(limit=1000)

    total_cost = sum(trace.cost for trace in traces)
    total_tokens = sum(trace.input_tokens + trace.output_tokens for trace in traces)

    print(f"Total cost: ${total_cost:.4f}")
    print(f"Total tokens: {total_tokens:,}")
    print(f"Cost per token: ${total_cost/total_tokens:.6f}")

Cost Optimization

Implement cost optimization strategies:

YAML
observability:
  langfuse:
    cost_optimization:
      enabled: true
      alerts:
        cost_threshold: 0.05  # $0.05 per request
        token_threshold: 2000  # 2000 tokens per request
      recommendations:
        enabled: true
        model_switching: true
        prompt_optimization: true

๐ŸŽฏ Next Steps

  1. Set up LangFuse Cloud account and get production API keys
  2. Configure agent playbooks with LangFuse integration
  3. Monitor agent performance through LangFuse dashboard
  4. Set up cost tracking and optimization alerts
  5. Implement user feedback collection
  6. Scale to production with robust monitoring

๐Ÿ”„ Choosing Between MLFlow and LangFuse

Both MLFlow and LangFuse provide excellent observability for SuperOptiX agents, but they serve different use cases:

๐Ÿงช MLFlow - Best for:

  • ML Experiment Tracking: Traditional ML workflows and experiments
  • Artifact Management: Code, models, and data versioning
  • Reproducibility: Detailed experiment tracking and comparison
  • Team Collaboration: Experiment sharing and model registry
  • Production ML: Model deployment and lifecycle management

๐Ÿ” LangFuse - Best for:

  • LLM Observability: Specialized for language model applications
  • Real-time Tracing: Detailed token usage and cost tracking
  • User Feedback: Built-in feedback collection and scoring
  • A/B Testing: LLM prompt and model comparison
  • Production LLM: Live monitoring and debugging

๐Ÿ“Š Quick Comparison

Feature MLFlow LangFuse
Primary Focus ML Experiments LLM Observability
Token Tracking Manual Automatic
Cost Tracking Manual Built-in
User Feedback Manual Native
A/B Testing Manual Built-in
Real-time UI Limited Excellent
Artifact Storage Excellent Good
Experiment Tracking Excellent Good

๐ŸŽฏ When to Use Each

Choose MLFlow if: - You're doing traditional ML experiments - You need detailed artifact versioning - You want to track model performance over time - You're building ML pipelines

Choose LangFuse if: - You're building LLM applications - You need real-time cost tracking - You want user feedback integration - You're doing prompt engineering - You need A/B testing for LLMs


๐ŸŽ‰ Successfully demonstrated LangFuse + SuperOptiX integration!

The integration provides comprehensive observability for AI agents with real-time tracing, performance monitoring, and quality metrics. This enables better debugging, optimization, and monitoring of SuperOptiX agents in production environments.