|
1 | 1 | ---
|
2 |
| -integration: third-party |
| 2 | +integration: logfire |
3 | 3 | ---
|
| 4 | +# LiteLLM |
4 | 5 |
|
5 |
| -LiteLLM allows you to call over 100 Large Language Models (LLMs) using the same input/output format. It also supports Logfire for logging and monitoring. |
| 6 | +**Logfire** supports instrumenting calls to the [LiteLLM](https://docs.litellm.ai/) Python SDK with the [`logfire.instrument_litellm()`][logfire.Logfire.instrument_litellm] method. |
6 | 7 |
|
7 |
| -To integrate Logfire with LiteLLM: |
| 8 | +## Installation |
8 | 9 |
|
9 |
| -1. Set the `LOGFIRE_TOKEN` environment variable. |
10 |
| -2. Add `logfire` to the callbacks of LiteLLM. |
| 10 | +Install `logfire` with the `litellm` extra: |
11 | 11 |
|
12 |
| -For more details, [check the official LiteLLM documentation.](https://docs.litellm.ai/docs/observability/logfire_integration) |
| 12 | +{{ install_logfire(extras=['litellm']) }} |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +```python hl_lines="6" |
| 17 | +import litellm |
| 18 | + |
| 19 | +import logfire |
| 20 | + |
| 21 | +logfire.configure() |
| 22 | +logfire.instrument_litellm() |
| 23 | + |
| 24 | +response = litellm.completion( |
| 25 | + model='gpt-4o-mini', |
| 26 | + messages=[{'role': 'user', 'content': 'Hi'}], |
| 27 | +) |
| 28 | +print(response.choices[0].message.content) |
| 29 | +# > Hello! How can I assist you today? |
| 30 | +``` |
| 31 | + |
| 32 | +!!! warning |
| 33 | + This currently works best if all arguments of instrumented methods are passed as keyword arguments, |
| 34 | + e.g. `litellm.completion(model=model, messages=messages)`. |
| 35 | + |
| 36 | +This creates a span which shows the conversation in the Logfire UI: |
| 37 | + |
| 38 | +<figure markdown="span"> |
| 39 | +{ width="697" } |
| 40 | +</figure> |
| 41 | + |
| 42 | +[`logfire.instrument_litellm()`][logfire.Logfire.instrument_litellm] uses the `LiteLLMInstrumentor().instrument()` method of the [`openinference-instrumentation-litellm`](https://pypi.org/project/openinference-instrumentation-litellm/) package. |
| 43 | + |
| 44 | +!!! note |
| 45 | + [LiteLLM has its own integration with Logfire](https://docs.litellm.ai/docs/observability/logfire_integration), but we recommend using `logfire.instrument_litellm()` instead. |
0 commit comments