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

# LiteLLM

> Send LiteLLM traces to Orq.ai using OpenTelemetry instrumentation. Monitor LLM calls, costs, and latency across all providers LiteLLM supports.

> [card] [Observability](#observability)
>
> Instrument application code with OpenTelemetry to capture traces, logs, and metrics for every LLM call, agent step, and tool use.

To retire a **LiteLLM** proxy rather than instrument it, see [Migrate to Orq.ai](https://docs.orq.ai/ai-gateway/get-started/migrate.md#migrate-from-litellm).

## Observability

### Getting Started

LiteLLM provides a unified interface for multiple LLM providers, enabling seamless switching between OpenAI, Anthropic, Cohere, and 100+ other providers. Tracing LiteLLM with **Orq.ai** provides comprehensive insights into provider performance, cost optimization, routing decisions, and API reliability across a multi-provider setup.

### Prerequisites

Ensure the following prerequisites are in place:

* An **Orq.ai** account and [API Key](https://docs.orq.ai/ai-studio/organization/api-keys.md)
* LiteLLM installed in the project
* Python 3.8+
* API keys for the LLM providers (OpenAI, Anthropic, Cohere, etc.)

### Install Dependencies

> [tab] Via LiteLLM Proxy Server
>
> ```bash Bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
> pip install 'litellm[proxy]'
> ```

> [tab] Standalone Scripts
>
> ```bash Bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
> pip install litellm opentelemetry-sdk opentelemetry-exporter-otlp openinference-instrumentation-litellm
> ```

### Configure Orq.ai

Set the following environment variables to connect to the **Orq.ai** OpenTelemetry collector:

```bash Unix/Linux/macOS theme={"theme":{"light":"github-light","dark":"github-dark"}}
export ORQ_API_KEY="<YOUR_ORQ_API_KEY>"
export OTEL_EXPORTER_OTLP_ENDPOINT="https://my.orq.ai/v2/otel/v1/traces"
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer $ORQ_API_KEY"
export OTEL_RESOURCE_ATTRIBUTES="service.name=litellm-app,service.version=1.0.0"
export LITELLM_MASTER_KEY="<YOUR_LITELLM_MASTER_KEY>"
# Provider API keys: add only the ones you use
export OPENAI_API_KEY="<YOUR_OPENAI_API_KEY>"
export ANTHROPIC_API_KEY="<YOUR_ANTHROPIC_API_KEY>"
export COHERE_API_KEY="<YOUR_COHERE_API_KEY>"
export GOOGLE_API_KEY="<YOUR_GOOGLE_API_KEY>"
```

```bash Windows (PowerShell) theme={"theme":{"light":"github-light","dark":"github-dark"}}
$env:ORQ_API_KEY = "<YOUR_ORQ_API_KEY>"
$env:OTEL_EXPORTER_OTLP_ENDPOINT = "https://my.orq.ai/v2/otel/v1/traces"
$env:OTEL_EXPORTER_OTLP_HEADERS = "Authorization=Bearer $env:ORQ_API_KEY"
$env:OTEL_RESOURCE_ATTRIBUTES = "service.name=litellm-app,service.version=1.0.0"
$env:LITELLM_MASTER_KEY = "<YOUR_LITELLM_MASTER_KEY>"
# Provider API keys: add only the ones you use
$env:OPENAI_API_KEY = "<YOUR_OPENAI_API_KEY>"
$env:ANTHROPIC_API_KEY = "<YOUR_ANTHROPIC_API_KEY>"
$env:COHERE_API_KEY = "<YOUR_COHERE_API_KEY>"
$env:GOOGLE_API_KEY = "<YOUR_GOOGLE_API_KEY>"
```

```bash .env theme={"theme":{"light":"github-light","dark":"github-dark"}}
ORQ_API_KEY=<YOUR_ORQ_API_KEY>
OTEL_EXPORTER_OTLP_ENDPOINT=https://my.orq.ai/v2/otel/v1/traces
OTEL_EXPORTER_OTLP_HEADERS=Authorization=Bearer <YOUR_ORQ_API_KEY>
OTEL_RESOURCE_ATTRIBUTES=service.name=litellm-app,service.version=1.0.0
LITELLM_MASTER_KEY=<YOUR_LITELLM_MASTER_KEY>
# Provider API keys: add only the ones you use
OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
ANTHROPIC_API_KEY=<YOUR_ANTHROPIC_API_KEY>
COHERE_API_KEY=<YOUR_COHERE_API_KEY>
GOOGLE_API_KEY=<YOUR_GOOGLE_API_KEY>
```

### Integrations

> [!WARNING]
>
> `litellm.callbacks = ["otel"]` only emits spans when running inside **LiteLLM Proxy Server**. In a standalone Python script it logs a warning and skips OTel initialisation. No spans reach **Orq.ai**. Choose the setup that matches the environment below.

> [tab] Via LiteLLM Proxy Server
>
> Run the LiteLLM Proxy Server with the `otel` callback enabled. The proxy handles all OTel export using the environment variables configured above.
>
> **1. Create config.yaml**
>
> ```yaml config.yaml theme={"theme":{"light":"github-light","dark":"github-dark"}}
> model_list:
>   - model_name: gpt-5.6-sol
>     litellm_params:
>       model: openai/gpt-5.6-sol
>       api_key: os.environ/OPENAI_API_KEY
>
> litellm_settings:
>   callbacks: ["otel"]
>
> router_settings:
>   pass_through_all_models: true
>
> general_settings:
>   master_key: os.environ/LITELLM_MASTER_KEY
> ```
>
> **2. Start the proxy**
>
> ```bash Bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
> litellm --config config.yaml
> ```
>
> **3. Call the proxy from application code**
>
> ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
> from openai import OpenAI
> import os
>
> client = OpenAI(
>     base_url="http://localhost:4000",
>     api_key=os.getenv("LITELLM_MASTER_KEY"),
> )
>
> response = client.chat.completions.create(
>     model="gpt-5.6-sol",
>     messages=[{"role": "user", "content": "Hello, how are you?"}],
> )
> print(response.choices[0].message.content)
> ```

> [tab] Standalone Scripts
>
> Use `openinference-instrumentation-litellm` for automatic OpenTelemetry tracing without running a proxy:
>
> ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
> from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
> from opentelemetry.sdk.resources import Resource
> from opentelemetry.sdk.trace import TracerProvider
> from opentelemetry.sdk.trace.export import BatchSpanProcessor
> from openinference.instrumentation.litellm import LiteLLMInstrumentor
> import litellm
> import os
>
> tracer_provider = TracerProvider(
>     resource=Resource({"service.name": "litellm-app"})
> )
>
> otlp_exporter = OTLPSpanExporter(
>     endpoint=os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT"),
>     headers={"Authorization": f"Bearer {os.getenv('ORQ_API_KEY')}"},
> )
>
> tracer_provider.add_span_processor(BatchSpanProcessor(otlp_exporter))
>
> LiteLLMInstrumentor().instrument(tracer_provider=tracer_provider)
>
> response = litellm.completion(
>     model="gpt-5.6-sol",
>     messages=[{"role": "user", "content": "Hello, how are you?"}],
> )
> print(response.choices[0].message.content)
> ```

> [!TIP]
>
> All LiteLLM calls will be automatically instrumented and exported to **Orq.ai** through the OTLP exporter. For more details, see [Traces](https://docs.orq.ai/ai-studio/observability/traces.md).

### Examples

**Basic Multi-Provider Usage**

> [tab] Via LiteLLM Proxy Server
>
> ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
> from openai import OpenAI
> import os
>
> client = OpenAI(
>     base_url="http://localhost:4000",
>     api_key=os.getenv("LITELLM_MASTER_KEY"),
> )
>
> def basic_multi_provider_example():
>     models = [
>         "gpt-5.6-sol",
>         "claude-opus-5",
>         "command-r",
>         "gemini/gemini-3.5-flash",
>         "ollama/llama3.2",
>     ]
>
>     prompt = "Explain the benefits of microservices architecture in 2 sentences."
>
>     results = []
>     for model in models:
>         try:
>             print(f"Testing {model}...")
>             response = client.chat.completions.create(
>                 model=model,
>                 messages=[{"role": "user", "content": prompt}],
>                 max_tokens=150,
>                 temperature=0.7,
>             )
>
>             results.append({
>                 "model": model,
>                 "content": response.choices[0].message.content,
>                 "tokens": response.usage.total_tokens,
>                 "cost": response.usage.total_tokens * 0.002,
>             })
>
>         except Exception as e:
>             print(f"Error with {model}: {e}")
>             results.append({"model": model, "error": str(e)})
>
>     return results
>
> results = basic_multi_provider_example()
> for result in results:
>     if "error" not in result:
>         print(f"{result['model']}: {result['tokens']} tokens, ~${result['cost']:.4f}")
> ```

> [tab] Standalone Scripts
>
> ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
> from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
> from opentelemetry.sdk.resources import Resource
> from opentelemetry.sdk.trace import TracerProvider
> from opentelemetry.sdk.trace.export import BatchSpanProcessor
> from openinference.instrumentation.litellm import LiteLLMInstrumentor
> import litellm
> import os
>
> tracer_provider = TracerProvider(
>     resource=Resource({"service.name": "litellm-app"})
> )
> otlp_exporter = OTLPSpanExporter(
>     endpoint=os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT"),
>     headers={"Authorization": f"Bearer {os.getenv('ORQ_API_KEY')}"},
> )
> tracer_provider.add_span_processor(BatchSpanProcessor(otlp_exporter))
> LiteLLMInstrumentor().instrument(tracer_provider=tracer_provider)
>
> def basic_multi_provider_example():
>     models = [
>         "gpt-5.6-sol",
>         "claude-opus-5",
>         "command-r",
>         "gemini/gemini-3.5-flash",
>         "ollama/llama3.2",
>     ]
>
>     prompt = "Explain the benefits of microservices architecture in 2 sentences."
>
>     results = []
>     for model in models:
>         try:
>             print(f"Testing {model}...")
>             response = litellm.completion(
>                 model=model,
>                 messages=[{"role": "user", "content": prompt}],
>                 max_tokens=150,
>                 temperature=0.7,
>             )
>
>             results.append({
>                 "model": model,
>                 "content": response.choices[0].message.content,
>                 "tokens": response.usage.total_tokens,
>                 "cost": response.usage.total_tokens * 0.002,
>             })
>
>         except Exception as e:
>             print(f"Error with {model}: {e}")
>             results.append({"model": model, "error": str(e)})
>
>     return results
>
> results = basic_multi_provider_example()
> for result in results:
>     if "error" not in result:
>         print(f"{result['model']}: {result['tokens']} tokens, ~${result['cost']:.4f}")
> ```

**Cost Optimization with Provider Fallback**

> [tab] Via LiteLLM Proxy Server
>
> ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
> from openai import OpenAI
> from typing import List, Dict, Any
> import os
>
> client = OpenAI(
>     base_url="http://localhost:4000",
>     api_key=os.getenv("LITELLM_MASTER_KEY"),
> )
>
> def cost_optimized_completion(
>     messages: List[Dict[str, str]],
>     fallback_models: List[str] = None,
>     max_tokens: int = 100,
> ) -> Dict[str, Any]:
>     if fallback_models is None:
>         fallback_models = [
>             "gpt-5.4-mini",
>             "claude-haiku-4-5",
>             "command",
>             "gpt-5.6-sol",
>             "claude-sonnet-5",
>         ]
>
>     for i, model in enumerate(fallback_models):
>         try:
>             print(f"Attempting {model} (priority {i+1})...")
>
>             response = client.chat.completions.create(
>                 model=model,
>                 messages=messages,
>                 max_tokens=max_tokens,
>                 temperature=0.7,
>             )
>
>             # approximate values: check provider pricing pages for current rates
>             cost_per_1k_tokens = {
>                 "gpt-5.4-mini": 0.0015,
>                 "gpt-5.6-sol": 0.005,
>                 "claude-haiku-4-5": 0.00025,
>                 "claude-sonnet-5": 0.003,
>                 "command": 0.015,
>             }
>
>             estimated_cost = (response.usage.total_tokens / 1000) * cost_per_1k_tokens.get(model, 0.002)
>
>             return {
>                 "success": True,
>                 "model_used": model,
>                 "content": response.choices[0].message.content,
>                 "tokens": response.usage.total_tokens,
>                 "estimated_cost": estimated_cost,
>                 "attempt_number": i + 1,
>             }
>
>         except Exception as e:
>             print(f"Failed with {model}: {e}")
>             if i == len(fallback_models) - 1:
>                 return {
>                     "success": False,
>                     "error": f"All models failed. Last error: {e}",
>                     "attempts": len(fallback_models),
>                 }
>             continue
>
>     return {"success": False, "error": "No models available"}
>
> result = cost_optimized_completion([
>     {"role": "user", "content": "Summarize the key benefits of using Docker containers for development"}
> ])
>
> if result["success"]:
>     print(f"Success with {result['model_used']} on attempt {result['attempt_number']}")
>     print(f"Cost: ~${result['estimated_cost']:.4f}, Tokens: {result['tokens']}")
>     print(f"Response: {result['content'][:100]}...")
> ```

> [tab] Standalone Scripts
>
> ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
> from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
> from opentelemetry.sdk.resources import Resource
> from opentelemetry.sdk.trace import TracerProvider
> from opentelemetry.sdk.trace.export import BatchSpanProcessor
> from openinference.instrumentation.litellm import LiteLLMInstrumentor
> import litellm
> from typing import List, Dict, Any
> import os
>
> tracer_provider = TracerProvider(
>     resource=Resource({"service.name": "litellm-app"})
> )
> otlp_exporter = OTLPSpanExporter(
>     endpoint=os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT"),
>     headers={"Authorization": f"Bearer {os.getenv('ORQ_API_KEY')}"},
> )
> tracer_provider.add_span_processor(BatchSpanProcessor(otlp_exporter))
> LiteLLMInstrumentor().instrument(tracer_provider=tracer_provider)
>
> def cost_optimized_completion(
>     messages: List[Dict[str, str]],
>     fallback_models: List[str] = None,
>     max_tokens: int = 100,
> ) -> Dict[str, Any]:
>     if fallback_models is None:
>         fallback_models = [
>             "gpt-5.4-mini",
>             "claude-haiku-4-5",
>             "command",
>             "gpt-5.6-sol",
>             "claude-sonnet-5",
>         ]
>
>     for i, model in enumerate(fallback_models):
>         try:
>             print(f"Attempting {model} (priority {i+1})...")
>
>             response = litellm.completion(
>                 model=model,
>                 messages=messages,
>                 max_tokens=max_tokens,
>                 temperature=0.7,
>             )
>
>             # approximate values: check provider pricing pages for current rates
>             cost_per_1k_tokens = {
>                 "gpt-5.4-mini": 0.0015,
>                 "gpt-5.6-sol": 0.005,
>                 "claude-haiku-4-5": 0.00025,
>                 "claude-sonnet-5": 0.003,
>                 "command": 0.015,
>             }
>
>             estimated_cost = (response.usage.total_tokens / 1000) * cost_per_1k_tokens.get(model, 0.002)
>
>             return {
>                 "success": True,
>                 "model_used": model,
>                 "content": response.choices[0].message.content,
>                 "tokens": response.usage.total_tokens,
>                 "estimated_cost": estimated_cost,
>                 "attempt_number": i + 1,
>             }
>
>         except Exception as e:
>             print(f"Failed with {model}: {e}")
>             if i == len(fallback_models) - 1:
>                 return {
>                     "success": False,
>                     "error": f"All models failed. Last error: {e}",
>                     "attempts": len(fallback_models),
>                 }
>             continue
>
>     return {"success": False, "error": "No models available"}
>
> result = cost_optimized_completion([
>     {"role": "user", "content": "Summarize the key benefits of using Docker containers for development"}
> ])
>
> if result["success"]:
>     print(f"Success with {result['model_used']} on attempt {result['attempt_number']}")
>     print(f"Cost: ~${result['estimated_cost']:.4f}, Tokens: {result['tokens']}")
>     print(f"Response: {result['content'][:100]}...")
> ```

### View Traces

Head to the [Traces](https://docs.orq.ai/ai-studio/observability/traces.md) tab to view LiteLLM traces in the AI Studio.

![View Traces](https://mintcdn.com/orqai/8ublVIDMeb653NWy/images/docs/4f393acb185bb7a48d77798bb71ed3d5e53e2d5a64caa019158ac2a949a0a291-Screenshot_2025-10-06_at_15.56.09.png?fit=max\&auto=format\&n=8ublVIDMeb653NWy\&q=85\&s=e4aff60e61ea52aac10d410b51cad7e2)

## Evaluations & Experiments

Once agents are running, use **Evaluatorq** to score outputs across a dataset and **Experiments** to compare configurations side-by-side.

> [card] [Run Evaluations with Evaluatorq](https://docs.orq.ai/ai-studio/optimize/evaluators.md#evaluatorq)
>
> Run parallel evaluations across deployed agents and compare results.

> [card] [Run Experiments via the API](https://docs.orq.ai/ai-studio/optimize/experiments.md)
>
> Compare agent configurations and view results in the AI Studio.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.