AI API 호출이 급증하는 환경에서 로그 분석은 시스템 안정성과 비용 최적화의 핵심입니다. 이 튜토리얼에서는 HolySheep AI를 통해 통합된 다중 모델 API 로그를 ELK Stack으로 수집·분석하는 실전 방법을 다룹니다.

실전 사용 사례: 이커머스 AI 고객 서비스 트래픽 급증

저는 국내 중견 이커머스 기업의 백엔드 엔지니어로서, 블랙프라이드 시즌 AI 고객 서비스 봇의 API 로그 분석 프로젝트를 진행했습니다. 하루 50만 건 이상의 AI API 호출이 있었고, 각 모델(GPT-4.1, Claude Sonnet, Gemini)의 응답 시간과 비용을 실시간으로监控해야 했습니다.

HolySheep의 단일 API 키로 여러 모델을 통합 관리할 수 있었고, ELK Stack을 연동하여 모델별 응답 시간 분포, 토큰 사용량 추세, 에러율 알림까지 구축했습니다. 이번 가이드에서 그 전체 프로세스를 공유합니다.

ELK Stack 아키텍처 개요

┌─────────────────────────────────────────────────────────────────┐
│                    HolySheep API Gateway                        │
│              (GPT-4.1 / Claude / Gemini / DeepSeek)             │
└─────────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────────┐
│                    Log Sources                                   │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐  ┌──────────┐       │
│  │Apache/Nginx│  │Application│  │API Client│  │ System   │       │
│  │  Logs    │  │  Logs    │  │  Logs    │  │  Logs    │       │
│  └────┬─────┘  └────┬─────┘  └────┬─────┘  └────┬─────┘       │
└───────┼─────────────┼─────────────┼─────────────┼───────────────┘
        │             │             │             │
        └─────────────┴─────────────┼─────────────┘
                                    ▼
┌─────────────────────────────────────────────────────────────────┐
│                    Filebeat (Log Shipper)                        │
│              Lightweight Log Collector & Forwarder               │
└─────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────┐
│                    Logstash (Processing)                         │
│        ┌──────────────────────────────────────┐                 │
│        │ • JSON Parse & Filter                │                 │
│        │ • Grok Pattern Matching              │                 │
│        │ • HolySheep API Log Enrichment       │                 │
│        │ • Cost Calculation per Request       │                 │
│        └──────────────────────────────────────┘                 │
└─────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────┐
│                    Elasticsearch (Storage)                       │
│              Index: holy-sheep-api-logs-YYYY.MM.DD              │
└─────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────┐
│                    Kibana (Visualization)                        │
│        • API Response Time Dashboard                            │
│        • Token Usage & Cost Analysis                            │
        • Error Rate Monitoring                                    │
│        • Model Comparison Analytics                             │
└─────────────────────────────────────────────────────────────────┘

사전 준비: HolySheep API 키 및 환경 설정

먼저 HolySheep AI 가입하여 API 키를 발급받습니다. HolySheep는 해외 신용카드 없이 로컬 결제을 지원하여 개발자가 빠르게 시작할 수 있습니다.

# HolySheep API 키 환경 변수 설정
export HOLYSHEEP_API_KEY="YOUR_HOLYSHEEP_API_KEY"
export HOLYSHEEP_BASE_URL="https://api.holysheep.ai/v1"

ELK Stack Docker Compose 파일 생성

cat > docker-compose.elk.yml << 'EOF' version: '3.8' services: elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch:8.11.0 container_name: elasticsearch environment: - discovery.type=single-node - xpack.security.enabled=false - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ports: - "9200:9200" volumes: - elasticsearch-data:/usr/share/elasticsearch/data networks: - elk-network logstash: image: docker.elastic.co/logstash/logstash:8.11.0 container_name: logstash volumes: - ./logstash/pipeline:/usr/share/logstash/pipeline - ./logs:/var/log/holy-sheep ports: - "5044:5044" - "9600:9600" environment: - "LS_JAVA_OPTS=-Xms256m -Xmx256m" depends_on: - elasticsearch networks: - elk-network kibana: image: docker.elastic.co/kibana/kibana:8.11.0 container_name: kibana ports: - "5601:5601" environment: - ELASTICSEARCH_HOSTS=http://elasticsearch:9200 depends_on: - elasticsearch networks: - elk-network filebeat: image: docker.elastic.co/beats/filebeat:8.11.0 container_name: filebeat user: root volumes: - ./filebeat/filebeat.yml:/usr/share/filebeat/filebeat.yml:ro - ./logs:/var/log/holy-sheep:ro - /var/lib/docker/containers:/var/lib/docker/containers:ro - /var/run/docker.sock:/var/run/docker.sock:ro depends_on: - elasticsearch - logstash networks: - elk-network volumes: elasticsearch-data: networks: elk-network: driver: bridge EOF

Docker Compose 실행

docker-compose -f docker-compose.elk.yml up -d

HolySheep API 로깅 클라이언트 구현

HolySheep API를 호출할 때 각 요청의 상세 정보를 JSON 로그로 기록하는 Python 클라이언트를 구현합니다. 이 로그는 ELK Stack으로 전송되어 분석됩니다.

# holy_sheep_logging_client.py
import json
import time
import uuid
from datetime import datetime
from typing import Optional, Dict, Any
from dataclasses import dataclass, asdict
import logging
import os

logging 모듈 설정

logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger("HolySheepLogger") @dataclass class HolySheepAPILog: """HolySheep API 호출 로그 구조체""" log_id: str timestamp: str model: str endpoint: str request_tokens: int response_tokens: int total_tokens: int response_time_ms: float status_code: int error_message: Optional[str] cost_usd: float session_id: str user_id: Optional[str] metadata: Dict[str, Any] class HolySheepAPIClient: """HolySheep API 로깅 클라이언트""" # HolySheep 공식 pricing (2024년 기준) PRICING = { "gpt-4.1": {"input": 8.0, "output": 8.0}, # $8/MTok "claude-sonnet-4-5": {"input": 15.0, "output": 15.0}, # $15/MTok "gemini-2.5-flash": {"input": 2.5, "output": 2.5}, # $2.50/MTok "deepseek-v3.2": {"input": 0.42, "output": 0.42}, # $0.42/MTok } def __init__(self, api_key: str, base_url: str = "https://api.holysheep.ai/v1"): self.api_key = api_key self.base_url = base_url self.session_id = str(uuid.uuid4()) self.log_file_path = os.environ.get("LOG_FILE_PATH", "/var/log/holy-sheep/api_requests.log") # 로그 디렉토리 생성 os.makedirs(os.path.dirname(self.log_file_path), exist_ok=True) def _calculate_cost(self, model: str, input_tokens: int, output_tokens: int) -> float: """토큰 사용량 기반 비용 계산""" if model not in self.PRICING: logger.warning(f"Unknown model: {model}, using default pricing") return 0.0 pricing = self.PRICING[model] input_cost = (input_tokens / 1_000_000) * pricing["input"] output_cost = (output_tokens / 1_000_000) * pricing["output"] return round(input_cost + output_cost, 6) def _write_log(self, log_entry: HolySheepAPILog): """로그 파일에 JSON 라인 기록""" try: with open(self.log_file_path, 'a', encoding='utf-8') as f: f.write(json.dumps(asdict(log_entry), ensure_ascii=False) + '\n') except Exception as e: logger.error(f"Failed to write log: {e}") def call_model(self, model: str, messages: list, temperature: float = 0.7, max_tokens: int = 1000, user_id: Optional[str] = None, metadata: Optional[Dict] = None) -> Dict[str, Any]: """HolySheep API 호출 및 로깅""" import requests log_id = str(uuid.uuid4()) start_time = time.time() # 기본 헤더 headers = { "Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json" } # 요청 페이로드 payload = { "model": model, "messages": messages, "temperature": temperature, "max_tokens": max_tokens } # API 호출 endpoint = f"{self.base_url}/chat/completions" try: response = requests.post(endpoint, headers=headers, json=payload, timeout=30) response_time = (time.time() - start_time) * 1000 if response.status_code == 200: result = response.json() # 토큰 사용량 추출 usage = result.get("usage", {}) prompt_tokens = usage.get("prompt_tokens", 0) completion_tokens = usage.get("completion_tokens", 0) total_tokens = usage.get("total_tokens", 0) # 비용 계산 cost = self._calculate_cost(model, prompt_tokens, completion_tokens) # 로그 기록 log_entry = HolySheepAPILog( log_id=log_id, timestamp=datetime.utcnow().isoformat() + "Z", model=model, endpoint=endpoint, request_tokens=prompt_tokens, response_tokens=completion_tokens, total_tokens=total_tokens, response_time_ms=round(response_time, 2), status_code=response.status_code, error_message=None, cost_usd=cost, session_id=self.session_id, user_id=user_id, metadata=metadata or {} ) logger.info(f"[HolySheep] {model} | {response_time:.0f}ms | {total_tokens} tokens | ${cost:.6f}") else: # 에러 응답 로깅 result = {"error": response.text} log_entry = HolySheepAPILog( log_id=log_id, timestamp=datetime.utcnow().isoformat() + "Z", model=model, endpoint=endpoint, request_tokens=0, response_tokens=0, total_tokens=0, response_time_ms=round((time.time() - start_time) * 1000, 2), status_code=response.status_code, error_message=response.text[:500], cost_usd=0.0, session_id=self.session_id, user_id=user_id, metadata=metadata or {} ) logger.error(f"[HolySheep] ERROR {response.status_code}: {response.text[:200]}") self._write_log(log_entry) return result except requests.exceptions.Timeout: logger.error(f"[HolySheep] Request timeout for {model}") raise except requests.exceptions.RequestException as e: logger.error(f"[HolySheep] Request failed: {e}") raise

사용 예시

if __name__ == "__main__": # HolySheep API 키 설정 api_key = os.environ.get("HOLYSHEEP_API_KEY", "YOUR_HOLYSHEEP_API_KEY") client = HolySheepAPIClient(api_key) # 다양한 모델 테스트 messages = [{"role": "user", "content": "한국의 AI 기술 발전에 대해简要 설명해주세요."}] models = [ ("gpt-4.1", "GPT-4.1 테스트"), ("claude-sonnet-4-5", "Claude Sonnet 테스트"), ("gemini-2.5-flash", "Gemini Flash 테스트"), ("deepseek-v3.2", "DeepSeek 테스트"), ] for model, description in models: try: print(f"\n>>> {description}") result = client.call_model( model=model, messages=messages, user_id="test-user-001", metadata={"source": "elk-integration-test"} ) print(f"Success: {result.get('choices', [{}])[0].get('message', {}).get('content', '')[:100]}...") except Exception as e: print(f"Failed: {e}")

Logstash 파이프라인 설정

Filebeat로 수집된 로그를 Logstash에서 처리하여 Elasticsearch에 저장하는 파이프라인을 설정합니다. HolySheep API 특화 필드 extraction과 비용 계산이 포함됩니다.

# logstash/pipeline/holy-sheep.conf
input {
  beats {
    port => 5044
    host => "0.0.0.0"
  }
  
  # 파일 직접 입력 (Filebeat 미사용 시)
  file {
    path => "/var/log/holy-sheep/api_requests.log"
    start_position => "beginning"
    sincedb_path => "/dev/null"
    codec => json
  }
}

filter {
  # 타임스탬프 파싱
  date {
    match => ["timestamp", "ISO8601"]
    target => "@timestamp"
  }
  
  # HolySheep API 로그 필드 extraction
  if [endpoint] =~ /api\.holysheep\.ai/ {
    
    # 모델 카테고리 분류
    if [model] =~ /^gpt-4/ {
      mutate {
        add_field => { "model_family" => "OpenAI" }
      }
    } else if [model] =~ /^claude/ {
      mutate {
        add_field => { "model_family" => "Anthropic" }
      }
    } else if [model] =~ /^gemini/ {
      mutate {
        add_field => { "model_family" => "Google" }
      }
    } else if [model] =~ /^deepseek/ {
      mutate {
        add_field => { "model_family" => "DeepSeek" }
      }
    } else {
      mutate {
        add_field => { "model_family" => "Other" }
      }
    }
    
    # 응답 시간 범주화
    if [response_time_ms] < 500 {
      mutate {
        add_field => { "response_category" => "fast" }
      }
    } else if [response_time_ms] < 2000 {
      mutate {
        add_field => { "response_category" => "normal" }
      }
    } else if [response_time_ms] < 5000 {
      mutate {
        add_field => { "response_category" => "slow" }
      }
    } else {
      mutate {
        add_field => { "response_category" => "timeout_risk" }
      }
    }
    
    # 성공/실패 분류
    if [status_code] >= 200 and [status_code] < 300 {
      mutate {
        add_field => { "request_status" => "success" }
      }
    } else {
      mutate {
        add_field => { "request_status" => "error" }
      }
    }
    
    # 분 단위 시간 버킷 (집계용)
    ruby {
      code => '
        require "time"
        timestamp = Time.parse(event.get("timestamp"))
        bucket = timestamp.strftime("%Y-%m-%dT%H:%M:00.000Z")
        event.set("time_bucket", bucket)
      '
    }
    
    # 토큰당 비용 극단치 detection
    if [total_tokens] > 0 and [cost_usd] > 0 {
      ruby {
        code => '
          cost_per_token = event.get("cost_usd").to_f / event.get("total_tokens").to_f
          event.set("cost_per_token_usd", cost_per_token)
        '
      }
    }
  }
  
  # 에러 로그 enriched
  if [request_status] == "error" {
    mutate {
      add_tag => ["error", "needs_attention"]
    }
    
    # 에러 타입 분류
    if [status_code] == 401 {
      mutate {
        add_field => { "error_type" => "authentication_failure" }
      }
    } else if [status_code] == 429 {
      mutate {
        add_field => { "error_type" => "rate_limit_exceeded" }
      }
    } else if [status_code] == 500 {
      mutate {
        add_field => { "error_type" => "provider_server_error" }
      }
    } else if [status_code] >= 400 and [status_code] < 500 {
      mutate {
        add_field => { "error_type" => "client_error" }
      }
    } else {
      mutate {
        add_field => { "error_type" => "unknown_error" }
      }
    }
  }
  
  # IP 주소 extraction (metadata에서)
  if [metadata] and [metadata][client_ip] {
    mutate {
      add_field => { "client_ip" => "%{[metadata][client_ip]}" }
    }
  }
}

output {
  # 콘솔 출력 (디버깅용)
  if "debug" in [tags] {
    stdout {
      codec => rubydebug
    }
  }
  
  # Elasticsearch 출력
  elasticsearch {
    hosts => ["elasticsearch:9200"]
    index => "holy-sheep-api-logs-%{+YYYY.MM.dd}"
    
    # 인덱스 템플릿 설정
    template_name => "holy-sheep-api"
    template_overwrite => true
    template => "/usr/share/logstash/templates/holy-sheep-template.json"
  }
  
  # 에러 로그 별도 인덱스
  if [request_status] == "error" {
    elasticsearch {
      hosts => ["elasticsearch:9200"]
      index => "holy-sheep-api-errors-%{+YYYY.MM.dd}"
    }
  }
}

Filebeat 설정

# filebeat/filebeat.yml
filebeat.inputs:
  # HolySheep API 로그 파일 모니터링
  - type: log
    enabled: true
    paths:
      - /var/log/holy-sheep/api_requests.log
    json.keys_under_root: true
    json.add_error_key: true
    json.message_key: log
    fields:
      log_type: holy_sheep_api
      environment: production
    fields_under_root: true
    
  # Nginx/Apache 웹 서버 로그 (HolySheep API 프록시 access log)
  - type: log
    enabled: true
    paths:
      - /var/log/nginx/access.log
    fields:
      log_type: web_access
      service: holy_sheep_proxy
    fields_under_root: true
    
  # 시스템 로그
  - type: log
    enabled: true
    paths:
      - /var/log/syslog
    fields:
      log_type: system
    fields_under_root: true

컨테이너 로그 모니터링

filebeat.autodiscover: providers: - type: docker hints.enabled: true templates: - condition: contains: docker.container.name: "holy-sheep" config: - type: container paths: - /var/lib/docker/containers/${data.docker.container.id}/*.log fields: application: holy_sheep_api

Logstash 출력 설정

output.logstash: hosts: ["logstash:5044"]

로깅 설정

logging.level: info logging.to_files: true logging.files: path: /var/log/filebeat name: filebeat keepfiles: 7 permissions: 0644

프로세스 설정

queue.mem.events: 256 output.elasticsearch: bulk_max_size: 2048 filebeat.shutdown_timeout: 5s

Kibana 대시보드 구성

ELK Stack 통합 후 Kibana에서 HolySheep API 모니터링 대시보드를 구성합니다. 모델별 성능 비교, 비용 추세, 에러율 알림을 시각화합니다.

{
  "title": "HolySheep API Monitoring Dashboard",
  "description": "Multi-model AI API performance and cost monitoring",
  "visState": {
    "type": "lens",
    "title": "API Performance Overview",
    "state": {
      "datasourceStates": {
        "indexpattern": {
          "title": "holy-sheep-api-logs-*",
          "layers": [
            {
              "columns": [
                {"name": "timestamp", "type": "date"},
                {"name": "model", "type": "string"},
                {"name": "model_family", "type": "string"},
                {"name": "response_time_ms", "type": "number"},
                {"name": "total_tokens", "type": "number"},
                {"name": "cost_usd", "type": "number"},
                {"name": "status_code", "type": "number"},
                {"name": "request_status", "type": "string"},
                {"name": "error_type", "type": "string"}
              ]
            }
          ]
        }
      }
    }
  },
  "kibanaSavedObjectMeta": {
    "searchSourceJSON": {
      "query": {
        "query": "",
        "language": "kuery"
      },
      "filter": []
    }
  },
  "panelsJSON": [
    {
      "version": "8.11.0",
      "type": "lens",
      "gridData": {"x": 0, "y": 0, "w": 12, "h": 8},
      "panelIndex": "1",
      "title": "Response Time by Model (ms)",
      "embeddableConfig": {
        "visualization": {
          "layerId": "main",
          "xAxisColumn": "timestamp",
          "yAxisColumns": ["response_time_ms"],
          "breakdownColumns": ["model"],
          "chartType": "lnsXY"
        }
      }
    },
    {
      "version": "8.11.0",
      "type": "lens",
      "gridData": {"x": 12, "y": 0, "w": 12, "h": 8},
      "panelIndex": "2",
      "title": "Token Usage by Model",
      "embeddableConfig": {
        "visualization": {
          "layerId": "main",
          "xAxisColumn": "timestamp",
          "yAxisColumns": ["total_tokens"],
          "breakdownColumns": ["model"],
          "chartType": "lnsXY"
        }
      }
    },
    {
      "version": "8.11.0",
      "type": "metric",
      "gridData": {"x": 24, "y": 0, "w": 6, "h": 4},
      "panelIndex": "3",
      "title": "Total API Cost (USD)",
      "embeddableConfig": {
        "aggs": [
          {"type": "sum", "field": "cost_usd"}
        ]
      }
    },
    {
      "version": "8.11.0",
      "type": "metric",
      "gridData": {"x": 30, "y": 0, "w": 6, "h": 4},
      "panelIndex": "4",
      "title": "Error Rate (%)",
      "embeddableConfig": {
        "aggs": [
          {"type": "avg", "field": "status_code"}
        ]
      }
    },
    {
      "version": "8.11.0",
      "type": "lens",
      "gridData": {"x": 0, "y": 8, "w": 24, "h": 8},
      "panelIndex": "5",
      "title": "Cost Trend by Model Family",
      "embeddableConfig": {
        "visualization": {
          "layerId": "main",
          "xAxisColumn": "time_bucket",
          "yAxisColumns": ["cost_usd"],
          "breakdownColumns": ["model_family"],
          "chartType": "lnsArea"
        }
      }
    },
    {
      "version": "8.11.0",
      "type": "table",
      "gridData": {"x": 24, "y": 4, "w": 12, "h": 12},
      "panelIndex": "6",
      "title": "Top Error Messages",
      "embeddableConfig": {
        "aggs": [
          {"type": "terms", "field": "error_type", "size": 10},
          {"type": "count"}
        ]
      }
    }
  ],
  "timeRestore": true,
  "timeTo": "now",
  "timeFrom": "now-24h",
  "refreshInterval": {
    "pause": false,
    "value": 30000
  },
  "kibanaConfig": {
    "darkMode": false
  }
}

실시간 알림 설정: ElastAlert 연동

HolySheep API 에러율 급증 또는 응답 시간 임계값 초과 시 Slack으로 실시간 알림을 받는 설정을 추가합니다.

# elastalert/holy_sheep_alerts.yaml

ElastAlert 규칙: API 에러율 임계값 초과

name: HolySheep API Error Rate Alert type: change index: holy-sheep-api-logs-*

5분 윈도우 내 에러율 계산

change_fields: - "request_status" query_key: model ignore_null: true

에러율이 5%를 초과할 때

conditions: - compare_key: request_status from_value: success to_value: error comparison: count_increase threshold: 10 alert: - slack: slack_webhook_url: "https://hooks.slack.com/services/YOUR/WEBHOOK/URL" slack_channel_override: "#holy-sheep-alerts" slack_msg_color: "danger" slack_emoji_replace: warning: ":warning:" error: ":rotating_light:" critical: ":fire:" slack_title_link: "https://kibana.yourcompany.com/app/discover" slack_footer: "HolySheep AI API Monitor | ELK Stack" slack_timeout: 30

추가 알림: 이메일

- email: smtp_host: smtp.gmail.com smtp_port: 587 smtp_auth_file: /etc/elastalert/smtp_auth.yaml from_addr: [email protected] to_addr: - [email protected] - [email protected] subject: "[ALERT] HolySheep API Error Rate Spike" name: HolySheep API Slow Response Alert type: spike index: holy-sheep-api-logs-*

모델별 평균 응답 시간의 3배 이상일 때

spike_height: 3 spike_type: up timeframe: minutes: 5 direction: up filter: - range: response_time_ms: gte: 5000 alert: - slack: slack_webhook_url: "https://hooks.slack.com/services/YOUR/WEBHOOK/URL" slack_channel_override: "#holy-sheep-alerts" slack_msg_color: "warning" message: | :hourglass: *HolySheep API Slow Response Detected* Model: {{ terms.model.0 }} Current Avg Response: {{ avg_response_time_ms }}ms Threshold: 5000ms Last 5 requests: {% for hit in hits %} - {{ hit.timestamp }} | {{ hit.response_time_ms }}ms | {{ hit.status_code }} {% endfor %}

비용 임계값 알림 (일일 $100 초과)

name: HolySheep Daily Cost Alert type: flatline index: holy-sheep-api-logs-* threshold: 100 threshold_ref: 24h filter: - term: model_family: "*" alert: - slack: slack_webhook_url: "https://hooks.slack.com/services/YOUR/WEBHOOK/URL" slack_channel_override: "#holy-sheep-cost" slack_msg_color: "warning" message: | :moneybag: *Daily Cost Threshold Warning* Total API Cost (24h): ${{ current_cost }} Threshold: $100 Top Spenders: {% for item in top_spenders %} - {{ item.model }}: ${{ item.cost_usd }} {% endfor %}

비용 최적화 분석 쿼리

Elasticsearch에서 HolySheep API 비용 최적화 기회를 분석하는 쿼리 예시입니다.

// HolySheep AI 비용 최적화 분석 쿼리

// 1. 모델별 일일 비용 및 사용량 요약
GET holy-sheep-api-logs-*/_search
{
  "size": 0,
  "query": {
    "range": {
      "@timestamp": {
        "gte": "now-7d/d",
        "lte": "now/d"
      }
    }
  },
  "aggs": {
    "daily_cost": {
      "date_histogram": {
        "field": "@timestamp",
        "calendar_interval": "day"
      },
      "aggs": {
        "by_model": {
          "terms": {
            "field": "model",
            "size": 10
          },
          "aggs": {
            "total_cost": {
              "sum": { "field": "cost_usd" }
            },
            "total_tokens": {
              "sum": { "field": "total_tokens" }
            },
            "avg_latency": {
              "avg": { "field": "response_time_ms" }
            },
            "request_count": {
              "value_count": { "field": "log_id" }
            }
          }
        }
      }
    }
  }
}

// 2. 비용 효율성 분석: 토큰당 비용 vs 응답 시간 트레이드오프
GET holy-sheep-api-logs-*/_search
{
  "size": 0,
  "aggs": {
    "model_comparison": {
      "terms": {
        "field": "model",
        "size": 10
      },
      "aggs": {
        "avg_cost_per_1k_tokens": {
          "scripted_metric": {
            "init_script": "params._agg.total_cost = 0; params._agg.total_tokens = 0;",
            "map_script": """
              params._agg.total_cost += doc['cost_usd'].value;
              params._agg.total_tokens += doc['total_tokens'].value;
            """,
            "combine_script": "return [cost: params._agg.total_cost, tokens: params._agg.total_tokens]",
            "reduce_script": """
              double total_cost = 0;
              double total_tokens = 0;
              for (s in states) {
                total_cost += s.cost;
                total_tokens += s.tokens;
              }
              return total_tokens > 0 ? (total_cost / total_tokens) * 1000 : 0;
            """
          }
        },
        "avg_response_time": {
          "avg": { "field": "response_time_ms" }
        },
        "p95_response_time": {
          "percentiles": {
            "field": "response_time_ms",
            "percents": [50, 90, 95, 99]
          }
        },
        "error_rate": {
          "filter": { "term": { "request_status": "error" } },
          "aggs": {
            "count": { "value_count": { "field": "log_id" } }
          }
        }
      }
    }
  }
}

// 3. 비효율적 요청 식별: 토큰 대 비율이 불균형한 요청
GET holy-sheep-api-logs-*/_search
{
  "size": 100,
  "query": {
    "bool": {
      "must": [
        { "range": { "@timestamp": { "gte": "now-24h" } } }
      ],
      "should": [
        {
          "script": {
            "script": "doc['response_tokens'].value > 0 && (doc['request_tokens'].value / doc['response_tokens'].value) > 10"
          }
        }
      ]
    }
  },
  "sort": [
    { "cost_usd": "desc" }
  ],
  "_source": ["timestamp", "model", "request_tokens", "response_tokens", "cost_usd", "session_id"]
}

// 4. 모델 전환 권장: 비용 절감 시뮬레이션
POST _sql
{
  "query": """
    SELECT 
      model,
      COUNT(*) as total_requests,
      SUM(total_tokens) as total_tokens,
      SUM(cost_usd) as total_cost,
      AVG(response_time_ms) as avg_latency,
      PERCENTILE(response_time_ms, 95) as p95_latency
    FROM holy-sheep-api-logs-*
    WHERE @timestamp >= NOW() - INTERVAL 7 DAY
    GROUP BY model
    ORDER BY total_cost DESC
  """
}

// DeepSeek 전환 시 비용 절감 분석
//