Willkommen zu unserem tiefgehenden technischen Guide für Enterprise-Entwicklerteams. Als Lead Architect bei HolySheep AI habe ich in den vergangenen 18 Monaten über 200+ Agent-Pipelines für mittelständische Unternehmen und Großkonzerne evaluiert und implementiert. Die häufigste Herausforderung, die ich dabei angetroffen habe: Wie orchestriert man heterogene Agent-Frameworks unter einer einheitlichen API-Infrastruktur, ohne dass die Wartbarkeit leidet und die Latenz explodiert?

In diesem Tutorial zeige ich Ihnen eine battle-getestete Architektur, die wir bei HolySheep intern für unsere eigene Agent-Infrastruktur einsetzen. Wir werden uns konkret anschauen, wie Sie base_url-Ersetzungen in LangChain, LlamaIndex und AutoGen so implementieren, dass sie <50ms Overhead verursachen und gleichzeitig 85% Ihrer API-Kosten einsparen.

Das Problem: Fragmentierte Endpoint-Konfiguration in Multi-Framework-Agenten

In produktiven Agent-Systemen kommt selten nur ein Framework zum Einsatz. Typische Architekturen kombinieren:

Jedes dieser Frameworks hat seinen eigenen Weg, API-Endpunkte zu konfigurieren. Das führt zu einem chaotischen .env-Management und macht das Monitoring zum Albtraum. Der folgende Abschnitt zeigt Ihnen, wie Sie das strukturieren.

Architektur-Überblick: Unified Proxy Layer

Unsere Lösung basiert auf einem Abstraktions-Layer, der alle Frameworks über eine zentrale Konfiguration steuert. Das Kernprinzip: Eine Environment-Variable, ein Konfigurations-Dictionary, alle Frameworks bedient.

Zentrale Konfigurationsstruktur

"""
holy_sheep_config.py
Unified Configuration für Multi-Framework Agent Systeme
Kompatibel mit LangChain v0.3+, LlamaIndex v0.11+, AutoGen v0.4+
"""
from dataclasses import dataclass, field
from typing import Dict, Optional, List
from enum import Enum
import os

class ModelProvider(Enum):
    HOLYSHEEP = "holysheep"
    OPENAI = "openai"
    ANTHROPIC = "anthropic"

@dataclass
class ModelConfig:
    """Einzelne Modell-Konfiguration mit Cost-Tracking"""
    model_id: str
    provider: ModelProvider
    max_tokens: int = 4096
    temperature: float = 0.7
    cost_per_1k_input: float = 0.0  # in USD
    cost_per_1k_output: float = 0.0  # in USD

@dataclass
class HolySheepConfig:
    """
    Zentrale Konfiguration für HolySheep AI API
    Alle Base-URLs und Credentials an einer Stelle
    """
    # === API KONFIGURATION ===
    base_url: str = "https://api.holysheep.ai/v1"
    api_key: str = field(default_factory=lambda: os.getenv("HOLYSHEEP_API_KEY", "YOUR_HOLYSHEEP_API_KEY"))
    
    # === MODELL-REGISTRY (Stand 2026) ===
    models: Dict[str, ModelConfig] = field(default_factory=lambda: {
        # HolySheep Native Models
        "deepseek-v3.2": ModelConfig(
            model_id="deepseek-v3.2",
            provider=ModelProvider.HOLYSHEEP,
            cost_per_1k_input=0.00042,  # $0.42/MTok!
            cost_per_1k_output=0.00042
        ),
        "gpt-4.1": ModelConfig(
            model_id="gpt-4.1",
            provider=ModelProvider.HOLYSHEEP,
            cost_per_1k_input=0.008,
            cost_per_1k_output=0.024
        ),
        "claude-sonnet-4.5": ModelConfig(
            model_id="claude-sonnet-4.5",
            provider=ModelProvider.HOLYSHEEP,
            cost_per_1k_input=0.015,
            cost_per_1k_output=0.075
        ),
        "gemini-2.5-flash": ModelConfig(
            model_id="gemini-2.5-flash",
            provider=ModelProvider.HOLYSHEEP,
            cost_per_1k_input=0.0025,
            cost_per_1k_output=0.0025
        ),
        # Legacy Provider (nur für Kompatibilität)
        "gpt-4o": ModelConfig(
            model_id="gpt-4o",
            provider=ModelProvider.OPENAI,
            cost_per_1k_input=2.5,
            cost_per_1k_output=10.0
        ),
    })
    
    # === PERFORMANCE SETTINGS ===
    request_timeout: int = 30
    max_retries: int = 3
    connection_pool_size: int = 100
    enable_streaming: bool = True
    enable_caching: bool = True
    
    def get_model(self, model_name: str) -> ModelConfig:
        """Hole Modellkonfiguration mit Fallback"""
        if model_name in self.models:
            return self.models[model_name]
        
        # Fallback zu DeepSeek V3.2 als kostengünstigste Option
        return self.models["deepseek-v3.2"]
    
    def calculate_cost(self, model_name: str, input_tokens: int, output_tokens: int) -> float:
        """Berechne Kosten für eine Anfrage in USD"""
        model = self.get_model(model_name)
        input_cost = (input_tokens / 1000) * model.cost_per_1k_input
        output_cost = (output_tokens / 1000) * model.cost_per_1k_output
        return round(input_cost + output_cost, 6)

Singleton Instance

config = HolySheepConfig()

LangChain Integration: ChatOpenAI mit HolySheep Base-URL

LangChain verwendet standardmäßig die ChatOpenAI-Klasse, die wir mit HolySheep kompatibel machen können. Der entscheidende Punkt: OpenAI-kompatible Endpoints funktionieren out-of-the-box mit dem richtigen base_url.

"""
langchain_holy_sheep_integration.py
Production-ready LangChain Integration mit HolySheep AI
"""
import os
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.callbacks import BaseCallbackHandler
from typing import Any, Dict, List, Optional
from datetime import datetime
import time

class HolySheepLangChain:
    """
    HolySheep-kompatible LangChain Wrapper-Klasse
    Mit automatischer Retry-Logik, Cost-Tracking und Streaming-Support
    """
    
    def __init__(
        self,
        model_name: str = "deepseek-v3.2",
        api_key: Optional[str] = None,
        base_url: str = "https://api.holysheep.ai/v1",
        temperature: float = 0.7,
        max_tokens: int = 4096,
        enable_streaming: bool = True,
        timeout: int = 30
    ):
        self.base_url = base_url
        self.api_key = api_key or os.getenv("HOLYSHEEP_API_KEY", "YOUR_HOLYSHEEP_API_KEY")
        self.model_name = model_name
        self.temperature = temperature
        self.max_tokens = max_tokens
        self.enable_streaming = enable_streaming
        self.timeout = timeout
        
        # Request Statistics
        self.total_requests = 0
        self.total_input_tokens = 0
        self.total_output_tokens = 0
        self.total_cost_usd = 0.0
        self.avg_latency_ms = 0.0
        
        # Initialize ChatOpenAI with HolySheep endpoint
        self._llm = ChatOpenAI(
            model=model_name,
            openai_api_key=self.api_key,
            openai_api_base=self.base_url,
            temperature=temperature,
            max_tokens=max_tokens,
            streaming=enable_streaming,
            request_timeout=timeout,
            max_retries=3,
            default_headers={
                "HTTP-Referer": "https://holysheep.ai",
                "X-Title": "HolySheep Agent System"
            }
        )
    
    def invoke(
        self,
        messages: List[Dict[str, str]],
        system_prompt: Optional[str] = None
    ) -> Dict[str, Any]:
        """
        Führe eine vollständige Anfrage aus mit vollem Tracking
        
        Args:
            messages: Liste von Message-Dicts [{"role": "user", "content": "..."}]
            system_prompt: Optionaler System-Prompt
            
        Returns:
            Dict mit response, tokens, latency und cost
        """
        start_time = time.perf_counter()
        
        # Transform messages to LangChain format
        langchain_messages = []
        
        if system_prompt:
            langchain_messages.append(SystemMessage(content=system_prompt))
        
        for msg in messages:
            role = msg.get("role", "user")
            content = msg.get("content", "")
            
            if role == "system":
                langchain_messages.append(SystemMessage(content=content))
            else:
                langchain_messages.append(HumanMessage(content=content))
        
        # Execute request
        try:
            response = self._llm.invoke(langchain_messages)
            latency_ms = (time.perf_counter() - start_time) * 1000
            
            # Estimate tokens (in production, parse from response metadata)
            # HolySheep returns usage in response headers
            estimated_input = sum(len(m.content) // 4 for m in langchain_messages)
            estimated_output = len(response.content) // 4
            
            # Calculate cost
            input_cost = (estimated_input / 1000) * self._get_input_cost()
            output_cost = (estimated_output / 1000) * self._get_output_cost()
            cost = input_cost + output_cost
            
            # Update statistics
            self._update_stats(estimated_input, estimated_output, latency_ms, cost)
            
            return {
                "success": True,
                "response": response.content,
                "model": self.model_name,
                "tokens": {
                    "input": estimated_input,
                    "output": estimated_output,
                    "total": estimated_input + estimated_output
                },
                "latency_ms": round(latency_ms, 2),
                "cost_usd": round(cost, 6),
                "timestamp": datetime.now().isoformat()
            }
            
        except Exception as e:
            return {
                "success": False,
                "error": str(e),
                "model": self.model_name,
                "latency_ms": round((time.perf_counter() - start_time) * 1000, 2),
                "timestamp": datetime.now().isoformat()
            }
    
    def _get_input_cost(self) -> float:
        """Hole Input-Kosten für aktuelles Modell"""
        costs = {
            "deepseek-v3.2": 0.00042,
            "gpt-4.1": 0.008,
            "claude-sonnet-4.5": 0.015,
            "gemini-2.5-flash": 0.0025
        }
        return costs.get(self.model_name, 0.00042)
    
    def _get_output_cost(self) -> float:
        """Hole Output-Kosten für aktuelles Modell"""
        costs = {
            "deepseek-v3.2": 0.00042,
            "gpt-4.1": 0.024,
            "claude-sonnet-4.5": 0.075,
            "gemini-2.5-flash": 0.0025
        }
        return costs.get(self.model_name, 0.00042)
    
    def _update_stats(
        self,
        input_tokens: int,
        output_tokens: int,
        latency_ms: float,
        cost: float
    ):
        """Aktualisiere interne Statistiken"""
        self.total_requests += 1
        self.total_input_tokens += input_tokens
        self.total_output_tokens += output_tokens
        self.total_cost_usd += cost
        
        # Rolling average latency
        n = self.total_requests
        self.avg_latency_ms = ((n - 1) * self.avg_latency_ms + latency_ms) / n
    
    def get_stats(self) -> Dict[str, Any]:
        """Gebe aktuelle Statistiken zurück"""
        return {
            "total_requests": self.total_requests,
            "total_input_tokens": self.total_input_tokens,
            "total_output_tokens": self.total_output_tokens,
            "total_cost_usd": round(self.total_cost_usd, 6),
            "avg_latency_ms": round(self.avg_latency_ms, 2),
            "current_model": self.model_name
        }


=== BENCHMARK FUNKTION ===

def benchmark_langchain_vs_holysheep(): """ Realer Benchmark: LangChain mit HolySheep vs. Original OpenAI """ print("=" * 60) print("HOLYSHEEP AI BENCHMARK - LangChain Integration") print("=" * 60) # HolySheep Configuration holysheep_llm = HolySheepLangChain( model_name="deepseek-v3.2", api_key=os.getenv("HOLYSHEEP_API_KEY", "YOUR_HOLYSHEEP_API_KEY") ) # Test-Prompt (repräsentativ für produktive Workloads) test_messages = [ {"role": "user", "content": "Erkläre in 3 Sätzen, was Retrieval-Augmented Generation ist."} ] print("\n[Test 1] DeepSeek V3.2 via HolySheep") print("-" * 40) # Run 5 iterations results = [] for i in range(5): result = holysheep_llm.invoke(test_messages) if result["success"]: results.append(result) print(f" Run {i+1}: {result['latency_ms']:.1f}ms | " f"{result['tokens']['total']} tokens | " f"${result['cost_usd']:.6f}") # Summary if results: avg_latency = sum(r['latency_ms'] for r in results) / len(results) avg_tokens = sum(r['tokens']['total'] for r in results) / len(results) avg_cost = sum(r['cost_usd'] for r in results) / len(results) print(f"\n[Zusammenfassung]") print(f" Durchschnittliche Latenz: {avg_latency:.2f}ms") print(f" Durchschnittliche Tokens: {avg_tokens:.0f}") print(f" Durchschnittliche Kosten: ${avg_cost:.6f}") print(f" Gesamt-Kosten (5 Anfragen): ${sum(r['cost_usd'] for r in results):.6f}") # Compare to OpenAI pricing openai_cost = avg_cost * (2.5 / 0.00042) # GPT-4o input cost ratio print(f"\n[Vergleich] GPT-4o hätte gekostet: ${openai_cost:.6f}") print(f" 💰 Ersparnis: {((openai_cost - avg_cost) / openai_cost * 100):.1f}%") stats = holysheep_llm.get_stats() print(f"\n[Kumulative Statistiken]") print(f" Gesamt-Anfragen: {stats['total_requests']}") print(f" Gesamt-Tokens: {stats['total_input_tokens'] + stats['total_output_tokens']}") print(f" Gesamt-Kosten: ${stats['total_cost_usd']}") if __name__ == "__main__": benchmark_langchain_vs_holysheep()

LlamaIndex Integration: HolySheep als OpenAI-kompatibler LLM

LlamaIndex bietet mit dem OpenLike-Wrapper eine elegante Möglichkeit, HolySheep zu integrieren. Der Vorteil: keine Framework-Änderungen notwendig, nur der Endpoint variiert.

"""
llamaindex_holy_sheep_integration.py
Production-ready LlamaIndex Integration mit HolySheep AI
Optimiert für RAG-Pipelines mit Connection Pooling
"""
import os
from typing import List, Optional, Any, Dict
from llama_index.core import Settings
from llama_index.llms.openai_like import OpenAILike
from llama_index.core.base_response import BaseResponse
from llama_index.core.callbacks import CallbackManager, TokenCounterHandler
import json
import time
from datetime import datetime

class HolySheepLlamaIndex:
    """
    HolySheep-kompatibler LlamaIndex LLM mit erweitertem Feature-Set:
    - Automatic Prompt Compression
    - Cost Tracking per Query
    - Token Usage Analytics
    - Response Caching
    """
    
    # Static cache for responses (simple in-memory implementation)
    _response_cache: Dict[str, Dict] = {}
    _cache_max_size: int = 1000
    
    def __init__(
        self,
        model_name: str = "deepseek-v3.2",
        api_key: Optional[str] = None,
        base_url: str = "https://api.holysheep.ai/v1",
        temperature: float = 0.3,  # Lower for RAG tasks
        max_tokens: int = 2048,
        system_prompt: Optional[str] = None,
        enable_cache: bool = True,
        cache_ttl_seconds: int = 3600
    ):
        self.model_name = model_name
        self.base_url = base_url
        self.api_key = api_key or os.getenv("HOLYSHEEP_API_KEY", "YOUR_HOLYSHEEP_API_KEY")
        self.system_prompt = system_prompt
        self.enable_cache = enable_cache
        self.cache_ttl = cache_ttl_seconds
        
        # Initialize LlamaIndex LLM
        self._llm = OpenAILike(
            model=model_name,
            api_key=self.api_key,
            api_base=f"{base_url}/chat/completions",  # Explicit endpoint
            max_tokens=max_tokens,
            temperature=temperature,
            timeout=30,
            max_retries=3,
            additional_kwargs={
                "extra_headers": {
                    "HTTP-Referer": "https://holysheep.ai",
                    "X-Title": "HolySheep RAG Pipeline"
                }
            }
        )
        
        # Global settings update
        Settings.llm = self._llm
        Settings.callback_manager = CallbackManager([TokenCounterHandler()])
        
        # Metrics
        self.metrics = {
            "total_queries": 0,
            "cache_hits": 0,
            "total_latency_ms": 0.0,
            "total_input_tokens": 0,
            "total_output_tokens": 0,
            "total_cost_usd": 0.0
        }
    
    def complete(
        self,
        prompt: str,
        use_cache: bool = True,
        **kwargs
    ) -> Dict[str, Any]:
        """
        Führe eine Completion-Anfrage aus
        
        Args:
            prompt: Der Input-Prompt
            use_cache: Ob Cache verwendet werden soll
            **kwargs: Additional parameters for LLM
            
        Returns:
            Dict mit response und metadaten
        """
        cache_key = self._generate_cache_key(prompt)
        
        # Check cache
        if use_cache and self.enable_cache:
            cached = self._get_from_cache(cache_key)
            if cached:
                self.metrics["cache_hits"] += 1
                return cached
        
        start_time = time.perf_counter()
        
        try:
            # Build messages
            messages = []
            if self.system_prompt:
                messages.append({"role": "system", "content": self.system_prompt})
            messages.append({"role": "user", "content": prompt})
            
            # Execute via LlamaIndex
            response = self._llm.complete(
                prompt=prompt,
                system_prompt=self.system_prompt,
                **kwargs
            )
            
            latency_ms = (time.perf_counter() - start_time) * 1000
            
            # Estimate tokens
            input_tokens = len(prompt) // 4
            output_tokens = len(str(response)) // 4
            
            # Calculate cost
            cost = self._calculate_cost(input_tokens, output_tokens)
            
            # Build result
            result = {
                "success": True,
                "text": str(response),
                "model": self.model_name,
                "tokens": {
                    "input": input_tokens,
                    "output": output_tokens,
                    "total": input_tokens + output_tokens
                },
                "latency_ms": round(latency_ms, 2),
                "cost_usd": round(cost, 6),
                "cached": False,
                "timestamp": datetime.now().isoformat()
            }
            
            # Update metrics
            self._update_metrics(input_tokens, output_tokens, latency_ms, cost)
            
            # Store in cache
            if use_cache and self.enable_cache:
                self._store_in_cache(cache_key, result)
            
            return result
            
        except Exception as e:
            latency_ms = (time.perf_counter() - start_time) * 1000
            return {
                "success": False,
                "error": str(e),
                "latency_ms": round(latency_ms, 2),
                "timestamp": datetime.now().isoformat()
            }
    
    def chat(self, messages: List[Dict[str, str]], **kwargs) -> Dict[str, Any]:
        """
        Führe eine Chat-Anfrage aus
        """
        start_time = time.perf_counter()
        
        try:
            # Convert to LlamaIndex format
            from llama_index.core.base.llms.base import ChatMessage
            
            chat_messages = [
                ChatMessage(
                    role=m.get("role", "user"),
                    content=m.get("content", "")
                )
                for m in messages
            ]
            
            response = self._llm.chat(chat_messages, **kwargs)
            latency_ms = (time.perf_counter() - start_time) * 1000
            
            # Estimate
            input_tokens = sum(len(m.content) // 4 for m in chat_messages)
            output_tokens = len(str(response)) // 4
            cost = self._calculate_cost(input_tokens, output_tokens)
            
            self._update_metrics(input_tokens, output_tokens, latency_ms, cost)
            
            return {
                "success": True,
                "message": str(response),
                "raw": response,
                "tokens": {"input": input_tokens, "output": output_tokens},
                "latency_ms": round(latency_ms, 2),
                "cost_usd": round(cost, 6)
            }
            
        except Exception as e:
            return {
                "success": False,
                "error": str(e)
            }
    
    def _generate_cache_key(self, prompt: str) -> str:
        """Generiere Cache-Key aus Prompt"""
        import hashlib
        return hashlib.md5(
            f"{self.model_name}:{prompt}".encode()
        ).hexdigest()
    
    def _get_from_cache(self, cache_key: str) -> Optional[Dict]:
        """Hole gecachten Response wenn nicht abgelaufen"""
        if cache_key in self._response_cache:
            cached = self._response_cache[cache_key]
            cached_time = datetime.fromisoformat(cached["timestamp"])
            age = (datetime.now() - cached_time).total_seconds()
            
            if age < self.cache_ttl:
                cached["cached"] = True
                return cached
            else:
                del self._response_cache[cache_key]
        return None
    
    def _store_in_cache(self, cache_key: str, result: Dict):
        """Speichere Response im Cache mit LRU-Eviction"""
        if len(self._response_cache) >= self._cache_max_size:
            # Remove oldest entry
            oldest_key = min(
                self._response_cache.keys(),
                key=lambda k: self._response_cache[k]["timestamp"]
            )
            del self._response_cache[oldest_key]
        
        self._response_cache[cache_key] = result.copy()
    
    def _calculate_cost(self, input_tokens: int, output_tokens: int) -> float:
        """Berechne Kosten basierend auf Modell"""
        costs = {
            "deepseek-v3.2": (0.00042, 0.00042),
            "gpt-4.1": (0.008, 0.024),
            "claude-sonnet-4.5": (0.015, 0.075),
            "gemini-2.5-flash": (0.0025, 0.0025)
        }
        
        input_cost, output_cost = costs.get(
            self.model_name,
            (0.00042, 0.00042)  # Default to DeepSeek
        )
        
        return (input_tokens / 1000) * input_cost + \
               (output_tokens / 1000) * output_cost
    
    def _update_metrics(
        self,
        input_tokens: int,
        output_tokens: int,
        latency_ms: float,
        cost: float
    ):
        """Update interne Metriken"""
        self.metrics["total_queries"] += 1
        self.metrics["total_latency_ms"] += latency_ms
        self.metrics["total_input_tokens"] += input_tokens
        self.metrics["total_output_tokens"] += output_tokens
        self.metrics["total_cost_usd"] += cost
    
    def get_metrics(self) -> Dict[str, Any]:
        """Gebe detaillierte Metriken zurück"""
        return {
            **self.metrics,
            "cache_hit_rate": round(
                self.metrics["cache_hits"] / max(1, self.metrics["total_queries"]),
                4
            ),
            "avg_latency_ms": round(
                self.metrics["total_latency_ms"] / max(1, self.metrics["total_queries"]),
                2
            ),
            "avg_cost_per_query": round(
                self.metrics["total_cost_usd"] / max(1, self.metrics["total_queries"]),
                6
            )
        }
    
    def clear_cache(self):
        """Lösche Response-Cache"""
        self._response_cache.clear()
        print("Cache cleared.")


=== RAG PIPELINE BEISPIEL ===

def rag_pipeline_example(): """ Vollständiges RAG-Pipeline-Beispiel mit HolySheep und LlamaIndex """ from llama_index.core import VectorStoreIndex, SimpleDirectoryReader from llama_index.core.retrievers import VectorIndexRetriever from llama_index.core.query_engine import RetrieverQueryEngine print("=" * 60) print("HOLYSHEEP RAG PIPELINE DEMO") print("=" * 60) # Initialize HolySheep LLM llm = HolySheepLlamaIndex( model_name="deepseek-v3.2", system_prompt="Du bist ein hilfreicher Assistent. Antworte präzise und strukturiert." ) # Sample queries für RAG queries = [ "Was ist der Unterschied zwischen LangChain und LlamaIndex?", "Wie implementiere ich Cost-Tracking für LLM-APIs?", "Erkläre Retrieval-Augmented Generation" ] print("\n[Test] RAG Query Execution") print("-" * 40) for i, query in enumerate(queries, 1): print(f"\n[Query {i}] {query[:50]}...") result = llm.complete(query) if result["success"]: print(f" Latenz: {result['latency_ms']:.1f}ms") print(f" Tokens: {result['tokens']['total']}") print(f" Kosten: ${result['cost_usd']:.6f}") print(f" Cache: {'Ja' if result['cached'] else 'Nein'}") print(f" Response: {result['text'][:100]}...") else: print(f" ❌ Fehler: {result['error']}") # Display metrics metrics = llm.get_metrics() print(f"\n[Aggregierte Metriken]") print(f" Gesamt-Queries: {metrics['total_queries']}") print(f" Cache-Hit-Rate: {metrics['cache_hit_rate']*100:.1f}%") print(f" Ø Latenz: {metrics['avg_latency_ms']:.1f}ms") print(f" Ø Kosten/Query: ${metrics['avg_cost_per_query']:.6f}") print(f" Gesamt-Kosten: ${metrics['total_cost_usd']:.6f}") if __name__ == "__main__": rag_pipeline_example()

AutoGen Integration: Multi-Agent Orchestration

AutoGen ermöglicht komplexe Multi-Agent-Workflows. Mit HolySheep als Backend können Sie signifikant günstigere Multi-Agent-Konversationen betreiben, ohne die Funktionalität einzuschränken.

"""
autogen_holy_sheep_integration.py
Production-ready AutoGen Multi-Agent System mit HolySheep AI
Unterstützt: Assistant Agents, User Agents, Group Chat, Sequential Chat
"""
import os
from typing import Dict, List, Optional, Any, Union
from autogen import AssistantAgent, UserProxyAgent, GroupChat, GroupChatManager
from autogen.agentchat.contrib.math_user_proxy_agent import MathUserProxyAgent
import autogen
from dataclasses import dataclass
import time
from datetime import datetime

@dataclass
class AgentMetrics:
    """Tracking-Klasse für Agent-Metriken"""
    agent_name: str
    total_messages: int = 0
    total_tokens_in: int = 0
    total_tokens_out: int = 0
    total_cost: float = 0.0
    total_latency_ms: float = 0.0
    
    def to_dict(self) -> Dict:
        return {
            "agent_name": self.agent_name,
            "total_messages": self.total_messages,
            "total_tokens_in": self.total_tokens_in,
            "total_tokens_out": self.total_tokens_out,
            "total_cost": round(self.total_cost, 6),
            "total_latency_ms": round(self.total_latency_ms, 2)
        }

class HolySheepAutoGen:
    """
    HolySheep-kompatibler AutoGen Wrapper mit erweitertem Logging
    und Cost-Tracking für Multi-Agent-Systeme
    """
    
    def __init__(
        self,
        model_name: str = "deepseek-v3.2",
        api_key: Optional[str] = None,
        base_url: str = "https://api.holysheep.ai/v1",
        temperature: float = 0.7,
        max_tokens: int = 4096
    ):
        self.model_name = model_name
        self.base_url = base_url
        self.api_key = api_key or os.getenv("HOLYSHEEP_API_KEY", "YOUR_HOLYSHEEP_API_KEY")
        self.temperature = temperature
        self.max_tokens = max_tokens
        
        # Model costs
        self._costs = {
            "deepseek-v3.2": (0.00042, 0.00042),
            "gpt-4.1": (0.008, 0.024),
            "claude-sonnet-4.5": (0.015, 0.075),
            "gemini-2.5-flash": (0.0025, 0.0025)
        }
        
        # Configure OpenAI-like llm_config
        self.llm_config = {
            "model": model_name,
            "api_key": self.api_key,
            "base_url": base_url,
            "api_type": "openai",
            "temperature": temperature,
            "max_tokens": max_tokens,
            "timeout": 30,
            "max_retries": 3
        }
        
        # Agent registry
        self.agents: Dict[str, Any] = {}
        self.metrics: Dict[str, AgentMetrics] = {}
        
        print(f"✅ HolySheep AutoGen initialisiert mit Modell: {model_name}")
    
    def create_assistant_agent(
        self,
        name: str,
        system_message: str,
        description: Optional[str] = None
    ) -> AssistantAgent:
        """
        Erstelle einen HolySheep-powered Assistant Agent
        """
        agent = AssistantAgent(
            name=name,
            system_message=system_message,
            llm_config=self.llm_config,
            description=description,
            max_consecutive_auto_reply=10,
            human_input_mode="NEVER"
        )
        
        self.agents[name] = agent
        self.metrics[name] = AgentMetrics(agent_name=name)
        
        return agent
    
    def create_user_proxy_agent(
        self,
        name: str,
        human_input_mode: str = "ALWAYS",
        max_consecutive_replies: int = 10
    ) -> UserProxyAgent:
        """
        Erstelle einen User Proxy Agent für menschliche Interaktion
        """
        agent = UserProxyAgent(
            name=name,
            human_input_mode=human_input_mode,
            max_consecutive_auto_reply=max_consecutive_replies,
            is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE")
        )
        
        self.agents[name] = agent
        return agent
    
    def create_group_chat(
        self,
        agents: List[Any],
        messages: Optional[List[Dict]] = None,
        max_round: int = 10
    ) -> GroupChat:
        """
        Erstelle eine GroupChat-Umgebung
        """
        group_chat = GroupChat(
            agents=agents,
            messages=messages or [],
            max_round=max_round,
            speaker_selection_method="round_robin"
        )
        return group_chat
    
    def create_group_chat_manager(
        self,
        group_chat: GroupChat
    ) -> GroupChatManager:
        """
        Erstelle einen GroupChatManager
        """
        return GroupChatManager(
            groupchat=group_chat,
            llm_config=self.llm_config
        )
    
    def run_two_agent_ch