> ## Documentation Index
> Fetch the complete documentation index at: https://docs.zespan.com/llms.txt
> Use this file to discover all available pages before exploring further.

# LiteLLM

> Trace LiteLLM calls across any provider in TypeScript and Python with wrapLiteLLM() or patch_litellm().

<Note>
  Available for: **Python** and **TypeScript**.
</Note>

LiteLLM provides a unified interface for 100+ LLM providers. In Python, patch the `litellm` module directly with `patch_litellm()`. In TypeScript, `wrapLiteLLM()` points a wrapped OpenAI-compatible client at your LiteLLM proxy server — the Node ecosystem doesn't have a `litellm` client package, so TypeScript apps talk to LiteLLM through its OpenAI-compatible proxy endpoint instead of an in-process SDK.

## Installation

<CodeGroup>
  ```bash TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  npm install @zespan/sdk openai
  ```

  ```bash Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  pip install zespan litellm
  ```
</CodeGroup>

<Note>
  TypeScript only needs the `openai` package — `wrapLiteLLM()` builds an OpenAI client internally and points it at your running [LiteLLM proxy server](https://docs.litellm.ai/docs/proxy/quick_start). Python calls the `litellm` package's module-level `completion()` / `acompletion()` functions directly, no proxy required.
</Note>

## Setup

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  import { zespan } from "@zespan/sdk";

  zespan.init({ apiKey: process.env.ZESPAN_API_KEY! });

  const litellm = zespan.wrapLiteLLM({
    baseURL: "http://localhost:4000", // your LiteLLM proxy server
    apiKey: process.env.LITELLM_API_KEY,
  });
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  import os
  import litellm
  import zespan

  zespan.init(api_key=os.environ["ZESPAN_API_KEY"])
  zespan.patch_litellm()
  ```
</CodeGroup>

`wrapLiteLLM()` takes a `{ baseURL, apiKey }` options object (not a client instance) and returns an OpenAI-compatible wrapped client pointed at that proxy. `patch_litellm()` monkey-patches `litellm.completion` and `litellm.acompletion` at the module level — no client object to construct at all.

## Example

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  const response = await litellm.chat.completions.create({
    model: "gpt-4o",
    messages: [{ role: "user", content: "What is observability?" }],
  });
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  response = litellm.completion(
      model="gpt-4o",
      messages=[{"role": "user", "content": "What is observability?"}],
  )

  # Async
  response = await litellm.acompletion(
      model="gpt-4o",
      messages=[{"role": "user", "content": "What is observability?"}],
  )
  ```
</CodeGroup>

Both `litellm.completion(..., stream=True)` (Python, sync and async) and streaming calls through the wrapped TypeScript client are traced end-to-end, including time-to-first-token.

## What gets captured

| Field         | Details                                                                                            |
| ------------- | -------------------------------------------------------------------------------------------------- |
| Model         | The model string passed to LiteLLM (e.g. `gpt-4o`, `claude-sonnet-4-6`, `gemini/gemini-2.5-flash`) |
| Input tokens  | From LiteLLM's normalized `usage` field                                                            |
| Output tokens | From LiteLLM's normalized `usage` field                                                            |
| Cost          | Calculated from token counts and Zespan's model pricing registry                                   |
| Latency       | Total call duration                                                                                |
| Finish reason | LiteLLM's normalized `finish_reason` (e.g. `stop`, `length`, `tool_calls`)                         |
| Tool calls    | Tool name and parsed arguments, both languages                                                     |
