Skip to content

ecologits.tracers.litellm_tracer

ChatCompletion

Bases: ModelResponse

Wrapper of litellm.types.utils.ModelResponse with ImpactsOutput

ChatCompletionChunk

Bases: ModelResponse

Wrapper of litellm.types.utils.ModelResponse with ImpactsOutput

LiteLLMInstrumentor()

Instrumentor initialized by EcoLogits to automatically wrap all LiteLLM calls

Source code in ecologits/tracers/litellm_tracer.py
def __init__(self) -> None:
    self.wrapped_methods = [
        {
            "module": litellm,
            "name": "completion",
            "wrapper": litellm_chat_wrapper,
        },
        {
            "module": litellm,
            "name": "acompletion",
            "wrapper": litellm_async_chat_wrapper,
        },
    ]

litellm_match_model(model_name)

Match according provider and model from a litellm model_name.

Looks up the exact model name (aliases included), then the name without host details (Bedrock region and vendor prefixes, version suffixes, Vertex @ versions), then <name>-latest. As a last step it uses fuzzy matching, but only when the name has numbers and they are the same in both names. Other names return None.

Parameters:

Name Type Description Default
model_name str

Name of the model as used in litellm.

required

Returns:

Type Description
Optional[tuple[str, str]]

A tuple (provider, model_name) matching a record of the ModelRepository.

Source code in ecologits/tracers/litellm_tracer.py
def litellm_match_model(model_name: str) -> Optional[tuple[str, str]]:
    """
    Match according provider and model from a litellm model_name.

    Looks up the exact model name (aliases included), then the name without host details
    (Bedrock region and vendor prefixes, version suffixes, Vertex `@` versions), then
    `<name>-latest`. As a last step it uses fuzzy matching, but only when the name has
    numbers and they are the same in both names. Other names return None.

    Args:
        model_name: Name of the model as used in litellm.

    Returns:
        A tuple (provider, model_name) matching a record of the ModelRepository.
    """
    match = _match(model_name)
    return None if match is None else (match[0], match[1])

litellm_chat_wrapper(wrapped, instance, args, kwargs)

Function that wraps a LiteLLM answer with computed impacts

Parameters:

Name Type Description Default
wrapped Callable

Callable that returns the LLM response

required
instance Completions

Never used - for compatibility with wrapt

required
args Any

Arguments of the callable

required
kwargs Any

Keyword arguments of the callable

required

Returns:

Type Description
Union[ChatCompletion, CustomStreamWrapper]

A wrapped ChatCompletion or CustomStreamWrapper with impacts

Source code in ecologits/tracers/litellm_tracer.py
def litellm_chat_wrapper(
    wrapped: Callable,
    instance: Completions,
    args: Any,
    kwargs: Any
) -> Union[ChatCompletion, CustomStreamWrapper]:
    """
    Function that wraps a LiteLLM answer with computed impacts

    Args:
        wrapped: Callable that returns the LLM response
        instance: Never used - for compatibility with `wrapt`
        args: Arguments of the callable
        kwargs: Keyword arguments of the callable

    Returns:
        A wrapped `ChatCompletion` or `CustomStreamWrapper` with impacts
    """
    if not is_tracking_enabled(PROVIDER):
        return wrapped(*args, **kwargs)
    trackers = get_active_trackers(PROVIDER)
    if kwargs.get("stream", False):
        timer_start = time.perf_counter()
        stream = wrapped(*args, **kwargs)
        return ImpactsStream(stream, litellm_chat_wrapper_stream(stream, timer_start, trackers))
    else:
        return litellm_chat_wrapper_non_stream(wrapped, instance, args, kwargs, trackers)

litellm_async_chat_wrapper(wrapped, instance, args, kwargs) async

Function that wraps a LiteLLM answer with computed impacts in async mode

Parameters:

Name Type Description Default
wrapped Callable

Async callable that returns the LLM response

required
instance AsyncCompletions

Never used - for compatibility with wrapt

required
args Any

Arguments of the callable

required
kwargs Any

Keyword arguments of the callable

required

Returns:

Type Description
Union[ChatCompletion, CustomStreamWrapper]

A wrapped ChatCompletion or CustomStreamWrapper with impacts

Source code in ecologits/tracers/litellm_tracer.py
async def litellm_async_chat_wrapper(
        wrapped: Callable,
        instance: AsyncCompletions,
        args: Any,
        kwargs: Any
) -> Union[ChatCompletion, CustomStreamWrapper]:
    """
    Function that wraps a LiteLLM answer with computed impacts in async mode

    Args:
        wrapped: Async callable that returns the LLM response
        instance: Never used - for compatibility with `wrapt`
        args: Arguments of the callable
        kwargs: Keyword arguments of the callable

    Returns:
        A wrapped `ChatCompletion` or `CustomStreamWrapper` with impacts
    """
    if not is_tracking_enabled(PROVIDER):
        return await wrapped(*args, **kwargs)
    trackers = get_active_trackers(PROVIDER)
    if kwargs.get("stream", False):
        timer_start = time.perf_counter()
        stream = await wrapped(*args, **kwargs)
        return AsyncImpactsStream(stream, litellm_async_chat_wrapper_stream(stream, timer_start, trackers))
    else:
        return await litellm_async_chat_wrapper_base(wrapped, instance, args, kwargs, trackers)