MCP 비용 추적

MCP 비용 추적

LiteLLM은 MCP 도구 호출의 비용을 추적하는 두 가지 방법을 제공해요:

방법 사용 시점 동작
Config 기반 비용 추적 (Config-based Cost Tracking) 도구/서버당 고정 비용이 있는 단순한 비용 추적 구성에 따라 비용을 자동 추적
사용자 지정 Post-MCP 훅 (Custom Post-MCP Hook) 사용자 지정 로직이 있는 동적 비용 추적 사용자 지정 비용 계산과 응답 수정 허용

출처: 문서

본문

Config 기반 비용 추적 (Config-based Cost Tracking)

config.yaml에서 MCP 서버의 고정 비용을 직접 구성해 주세요:

config.yaml:

model_list:
  - model_name: gpt-5.6-terra
    litellm_params:
      model: openai/gpt-5.6-terra
      api_key: sk-xxxxxxx

mcp_servers:
  zapier_server:
    url: "https://actions.zapier.com/mcp/sk-xxxxx/sse"
    mcp_info:
      mcp_server_cost_info:
        # Default cost for all tools in this server
        default_cost_per_query: 0.01
        # Custom cost for specific tools
        tool_name_to_cost_per_query:
          send_email: 0.05
          create_document: 0.03
          
  expensive_api_server:
    url: "https://api.expensive-service.com/mcp"
    mcp_info:
      mcp_server_cost_info:
        default_cost_per_query: 1.50

사용자 지정 Post-MCP 훅 (Custom Post-MCP Hook)

동적 비용 계산이 필요하거나 MCP 응답을 사용자에게 반환하기 전에 수정하고 싶을 때 사용해 주세요.

1. 사용자 지정 MCP 훅 파일 생성 (Create a custom MCP hook file)

custom_mcp_hook.py:

from typing import Optional
from litellm.integrations.custom_logger import CustomLogger
from litellm.types.mcp import MCPPostCallResponseObject


class CustomMCPCostTracker(CustomLogger):
    """
    Custom handler for MCP cost tracking and response modification
    """
    
    async def async_post_mcp_tool_call_hook(
        self, 
        kwargs, 
        response_obj: MCPPostCallResponseObject, 
        start_time, 
        end_time
    ) -> Optional[MCPPostCallResponseObject]:
        """
        Called after each MCP tool call. 
        Modify costs and response before returning to user.
        """
        
        # Extract tool information from kwargs
        tool_name = kwargs.get("name", "")
        server_name = kwargs.get("server_name", "")
        
        # Calculate custom cost based on your logic
        custom_cost = 42.00
        
        # Set the response cost
        response_obj.hidden_params.response_cost = custom_cost
        
  
      
        return response_obj
    

# Create instance for LiteLLM to use
custom_mcp_cost_tracker = CustomMCPCostTracker()

2. config.yaml에서 구성 (Configure in config.yaml)

config.yaml:

model_list:
  - model_name: gpt-5.6-terra
    litellm_params:
      model: openai/gpt-5.6-terra
      api_key: «redacted:sk-…»

# Add your custom MCP hook
callbacks:
  - custom_mcp_hook.custom_mcp_cost_tracker

mcp_servers:
  zapier_server:
    url: "https://actions.zapier.com/mcp/sk-xxxxx/sse"

3. 프록시 시작 (Start the proxy)

$ litellm --config /path/to/config.yaml 

MCP 도구가 호출되면 사용자 지정 훅이:

  1. 사용자 지정 로직에 따라 비용 계산
  2. 필요 시 응답 수정
  3. LiteLLM 로깅 시스템에서 비용 추적

더 알아보기 (Learn more)