Start monitoring your AI applications with the OpenLIT SDK in a few steps
OpenLIT automatically instruments LLMs, VectorDBs, MCP, and frameworks by default.
This guide demonstrates how to implement real-time cost tracking, token usage monitoring, hallucination detection, and latency optimization for your AI applications with OpenTelemetry traces and metrics.
1
Deploy OpenLIT
1
Git clone OpenLIT repository
git clone git@github.com:openlit/openlit.git
2
Start Docker Compose
From the root directory of the OpenLIT Repo, Run the below command:
docker compose up -d
Prefer Kubernetes, or already have ClickHouse/OpenTelemetry Collector running? See Self-Host OpenLIT for Helm charts and other deployment options.
import osfrom anthropic import Anthropicimport openlitopenlit.init(otlp_endpoint="http://127.0.0.1:4318")client = Anthropic( # This is the default and can be omitted api_key=os.environ.get("ANTHROPIC_API_KEY"),)message = client.messages.create( max_tokens=1024, messages=[ { "role": "user", "content": "Hello, What is LLM Observability?", } ], model="claude-3-opus-20240229",)
from litellm import completionimport osimport openlitopenlit.init(otlp_endpoint="http://127.0.0.1:4318")os.environ["HUGGINGFACE_API_KEY"] = "huggingface_api_key"# e.g. Call 'WizardLM/WizardCoder-Python-34B-V1.0' hosted on HF Inference endpointsresponse = completion( model="huggingface/WizardLM/WizardCoder-Python-34B-V1.0", messages=[{ "content": "Hello, how are you?","role": "user"}], api_base="https://my-endpoint.huggingface.cloud")
from langchain_core.messages import HumanMessage, SystemMessageimport openlitopenlit.init(otlp_endpoint="http://127.0.0.1:4318")from langchain_openai import ChatOpenAImodel = ChatOpenAI(model="gpt-4o-mini")messages = [ SystemMessage(content="Translate the following from English into Italian"), HumanMessage(content="hi!"),]model.invoke(messages)
import ollamaimport openlitopenlit.init(otlp_endpoint="http://127.0.0.1:4318")response = ollama.chat(model='llama3.1', messages=[ { 'role': 'user', 'content': 'Why is the sky blue?', },])
Add the following two lines to your application code:
import openlitopenlit.init()
Run the following command to configure the OTEL export endpoint:
import osfrom anthropic import Anthropicimport openlitopenlit.init()client = Anthropic( # This is the default and can be omitted api_key=os.environ.get("ANTHROPIC_API_KEY"),)message = client.messages.create( max_tokens=1024, messages=[ { "role": "user", "content": "Hello, What is LLM Observability?", } ], model="claude-3-opus-20240229",)
from litellm import completionimport osimport openlitopenlit.init()os.environ["HUGGINGFACE_API_KEY"] = "huggingface_api_key"# e.g. Call 'WizardLM/WizardCoder-Python-34B-V1.0' hosted on HF Inference endpointsresponse = completion( model="huggingface/WizardLM/WizardCoder-Python-34B-V1.0", messages=[{ "content": "Hello, how are you?","role": "user"}], api_base="https://my-endpoint.huggingface.cloud")
from langchain_core.messages import HumanMessage, SystemMessageimport openlitopenlit.init()from langchain_openai import ChatOpenAImodel = ChatOpenAI(model="gpt-4o-mini")messages = [ SystemMessage(content="Translate the following from English into Italian"), HumanMessage(content="hi!"),]model.invoke(messages)
import ollamaimport openlitopenlit.init()response = ollama.chat(model='llama3.1', messages=[ { 'role': 'user', 'content': 'Why is the sky blue?', },])
# Configure via environment variablesexport OTEL_SERVICE_NAME=my-ai-appexport OTEL_DEPLOYMENT_ENVIRONMENT=productionexport OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318# Run with zero code changesopenlit-instrument python your_app.py
SDK Integration
Via function parameters
Via environment variables
import Openlit from "openlit"Openlit.init({ otlpEndpoint: "http://127.0.0.1:4318" })
import Openlit from "openlit"Openlit.init({ otlpEndpoint: "http://127.0.0.1:4318" })async function main() { const { ChatOpenAI } = await import("@langchain/openai"); const { HumanMessage, SystemMessage } = await import("@langchain/core/messages"); const model = new ChatOpenAI({ model: "gpt-4o-mini" }); const messages = [ new SystemMessage("Translate the following from English into Italian"), new HumanMessage("hi!"), ]; await model.invoke(messages);}main();
import Openlit from "openlit"Openlit.init({ otlpEndpoint: "http://127.0.0.1:4318" })async function main() { const ollama = await import("ollama").then((e) => e.default); const response = await ollama.chat({ model: "llama3.1", messages: [{ role: "user", content: "Why is the sky blue?" }], }); console.log(response);}main();
Using static top-level imports instead (e.g. import OpenAI from "openai")? Static imports are hoisted and run before Openlit.init(), so the automatic patch can miss the module. Pass it into instrumentations so OpenLIT patches it directly:
import openlit from "openlit"import OpenAI from "openai"openlit.init({ otlpEndpoint: "http://127.0.0.1:4318", instrumentations: { openai: OpenAI, }})async function main() { const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, }); const completion = await openai.chat.completions.create({ model: "gpt-3.5-turbo", messages: [{ role: "user", content: "What is LLM Observability?" }], }); console.log(completion?.choices?.[0]);}main();
The same pattern works for anthropic, cohere, and ollama. LangChain always uses the dynamic-import pattern above, since it patches via a different hook.
Add the following two lines to your application code:
import openlit from "openlit"openlit.init()
Run the following command to configure the OTEL export endpoint:
import openlit from "openlit"openlit.init()async function main() { const { ChatOpenAI } = await import("@langchain/openai"); const { HumanMessage, SystemMessage } = await import("@langchain/core/messages"); const model = new ChatOpenAI({ model: "gpt-4o-mini" }); const messages = [ new SystemMessage("Translate the following from English into Italian"), new HumanMessage("hi!"), ]; await model.invoke(messages);}main();
import openlit from "openlit"openlit.init()async function main() { const ollama = await import("ollama").then((e) => e.default); const response = await ollama.chat({ model: "llama3.1", messages: [{ role: "user", content: "Why is the sky blue?" }], }); console.log(response);}main();
Using static top-level imports instead (e.g. import OpenAI from "openai")? Static imports are hoisted and run before openlit.init(), so the automatic patch can miss the module. Pass it into instrumentations so OpenLIT patches it directly:
import openlit from "openlit"import OpenAI from "openai"openlit.init({ instrumentations: { openai: OpenAI, }})async function main() { const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, }); const completion = await openai.chat.completions.create({ model: "gpt-3.5-turbo", messages: [{ role: "user", content: "What is LLM Observability?" }], }); console.log(completion?.choices?.[0]);}main();
The same pattern works for anthropic, cohere, and ollama. LangChain always uses the dynamic-import pattern above, since it patches via a different hook.
Monitor, debug and test the quality of your AI applications
With real-time LLM observability data now flowing to OpenLIT, visualize comprehensive AI performance metrics including token costs, latency patterns, hallucination rates, and model accuracy to optimize your production AI applications.Just head over to OpenLIT at 127.0.0.1:3000 on your browser to start exploring. You can login using the default credentials
Email: user@openlit.io
Password: openlituser
You’re all set! Your AI applications now have observability with real-time performance monitoring, cost tracking, and AI safety evaluations.Send Observability telemetry to other OpenTelemetry backendsIf you wish to send telemetry directly from the SDK to another backend, you can stop the current Docker services by using the command below. For more details on sending the data to your existing OpenTelemetry backends, checkout our Supported Destinations guide.
docker compose down
If you have any questions or need support, reach out to our community.
Quickstart: LLM Evaluations
Get started with evaluating your LLM responses in 2 simple steps
Integrations
60+ AI integrations with automatic instrumentation and performance tracking
Create a dashboard
Create custom visualizations with flexible widgets, queries, and real-time AI monitoring