Implementation:BerriAI Litellm LLM Request Utils
| Attribute | Value |
|---|---|
| Sources | litellm/litellm_core_utils/llm_request_utils.py |
| Domains | Request Processing, Model Selection, Safety |
| Last Updated | 2026-02-15 16:00 GMT |
Overview
Provides utilities for sanitizing LLM request parameters, selecting cheapest models by provider, and extracting proxy request headers.
Description
This module contains three utility functions for LLM request handling:
_ensure_extra_body_is_safe-- Ensures that theextra_bodydictionary attached to a request is JSON-serializable. Specifically handles LangfuseTextPromptClientobjects inextra_body["metadata"]["prompt"]by converting them to plain dictionaries via__dict__.pick_cheapest_chat_models_from_llm_provider-- Queries the global model cost map to find thencheapest chat-mode models for a given provider. Models are sorted by combined input + output cost per token. Returns an empty list if the provider is not found.get_proxy_server_request_headers-- Extracts the original HTTP request headers fromlitellm_params["proxy_server_request"]["headers"], useful for custom logging or header forwarding in proxy deployments.
Usage
Import _ensure_extra_body_is_safe when processing user-supplied extra_body parameters before sending to provider APIs. Import pick_cheapest_chat_models_from_llm_provider for model selection in health checks or fallback logic. Import get_proxy_server_request_headers in custom callbacks to access the original proxy request headers.
Code Reference
Source Location
litellm/litellm_core_utils/llm_request_utils.py (83 lines)
Signature
def _ensure_extra_body_is_safe(extra_body: Optional[Dict]) -> Optional[Dict]
def pick_cheapest_chat_models_from_llm_provider(
custom_llm_provider: str, n: int = 1
) -> list[str]
def get_proxy_server_request_headers(litellm_params: Optional[dict]) -> dict
Import
from litellm.litellm_core_utils.llm_request_utils import (
_ensure_extra_body_is_safe,
pick_cheapest_chat_models_from_llm_provider,
get_proxy_server_request_headers,
)
I/O Contract
_ensure_extra_body_is_safe
| Direction | Name | Type | Description |
|---|---|---|---|
| Input | extra_body | Optional[Dict] |
The extra_body dictionary from a request |
| Output | return | Optional[Dict] |
Sanitized extra_body with non-serializable objects converted |
pick_cheapest_chat_models_from_llm_provider
| Direction | Name | Type | Description |
|---|---|---|---|
| Input | custom_llm_provider | str |
Provider name (e.g., "openai", "anthropic")
|
| Input | n | int |
Number of cheapest models to return (default: 1) |
| Output | return | list[str] |
List of model name strings sorted by ascending cost |
get_proxy_server_request_headers
| Direction | Name | Type | Description |
|---|---|---|---|
| Input | litellm_params | Optional[dict] |
The litellm_params dictionary from a call |
| Output | return | dict |
Headers dictionary from the proxy server request, or empty dict |
Usage Examples
from litellm.litellm_core_utils.llm_request_utils import (
_ensure_extra_body_is_safe,
pick_cheapest_chat_models_from_llm_provider,
get_proxy_server_request_headers,
)
# Sanitize extra_body before sending to provider
extra_body = {"metadata": {"prompt": some_langfuse_prompt_client}}
safe_body = _ensure_extra_body_is_safe(extra_body)
# Pick the 3 cheapest OpenAI chat models
cheapest = pick_cheapest_chat_models_from_llm_provider("openai", n=3)
# e.g., ["gpt-4o-mini", "gpt-3.5-turbo", "gpt-4o"]
# Extract proxy request headers in a custom callback
def my_callback(kwargs, response, start_time, end_time):
headers = get_proxy_server_request_headers(kwargs.get("litellm_params"))
user_agent = headers.get("user-agent", "unknown")
Related Pages
- Implementation:BerriAI_Litellm_Health_Check_Helpers -- uses
pick_cheapest_chat_models_from_llm_providerfor wildcard health checks - Implementation:BerriAI_Litellm_Get_Model_Cost_Map -- provides the model cost data used for cheapest-model selection