跳到正文
DEV Community· Span Observer·· 29 分鐘前AI 評分59

OpenLIT OpenTelemetry 如何為 LLM 應用程式加入追蹤

OpenLIT OpenTelemetry: instrument an LLM application with little effort

AI 導讀

OpenLIT 以 OpenTelemetry 為 LLM 應用程式加入追蹤,讓每次模型呼叫的 span 記錄模型、token 數量及成本。SDK 提供 Python、TypeScript 和 Go 版本,列出超過 70 項整合,並介紹 openlit.init()、openlit-instrument 和 TypeScript 靜態匯入的設定方式。

正文

Your application calls an LLM, the bill keeps growing, and nobody knows which request costs the most. With OpenLIT OpenTelemetry, the answer fits in a trace: each model call becomes a span carrying its model, its tokens and its cost. The openlit/openlit repository is licensed under Apache 2.0 and showed about 3,000 stars on 11 November 2026.

This article starts from an operations decision: should you instrument by hand, or let an SDK do it? And above all, what are you willing to store in your traces? The facts come from the repository README and the docs.openlit.io documentation, read on 11 November 2026.

A word of honesty: span.observer did not run these commands. The snippets follow the documentation, and the trace screenshot comes from the project’s repository (Apache 2.0). To compare with other approaches, see Langfuse and OpenLLMetry and OpenInference to a Collector.

OpenLIT OpenTelemetry: what the project actually does

OpenLIT describes itself as an open-source platform to trace, evaluate and improve AI applications with OpenTelemetry. The README lists more than 70 integrations: model providers, agent frameworks, vector databases and GPUs.

  • SDKs: Python (pip install openlit), TypeScript (npm install openlit) and Go.
  • Conventions: traces and metrics follow the OpenTelemetry GenAI semantic conventions, in the gen_ai.* namespace.
  • Export: plain OTLP, so standard traces, metrics and events that any compatible backend can read.
  • Interface: a self-hosted dashboard, started with docker compose up -d, which receives OTLP on ports 4317 (gRPC) and 4318 (HTTP).

The dashboard is therefore optional. The SDK matters more: it produces spans you can send elsewhere.

Three ways to instrument, one deciding question

The documentation offers three entry points. The deciding question is simple: who is allowed to change the application code?

You own the code: openlit.init()

One call at start-up is enough. The SDK detects the installed libraries and instruments them. The snippet below combines the OTLP address with two privacy options covered further down.

Python file app.py: openlit.init call with otlp\_endpoint, capture\_message\_content set to False and max\_content\_length set to 2000

You cannot touch the code: openlit-instrument

The CLI launches your program for you. Service, environment and endpoint travel through environment variables. It suits a third-party application or a container you do not rebuild.

Terminal: three variables OTEL\_SERVICE\_NAME, OTEL\_DEPLOYMENT\_ENVIRONMENT and OTEL\_EXPORTER\_OTLP\_ENDPOINT, then the command openlit-instrument python your\_app.py

You use TypeScript with static imports

A top-level import such as import OpenAI from "openai" escapes automatic instrumentation. The documentation then asks you to pass the module to the instrumentations parameter. LangChain must use dynamic imports.

Watch out for a trap on the quickstart page: the line export OTEL_EXPORTER_OTLP_ENDPOINT = "..." is written there with spaces around the equals sign, which the shell rejects. Write it without spaces.

What a span contains: reading a trace

Open a trace and look at the span attributes. In the repository screenshot, a chat call shows the model, the duration, the token count and the cost. The attributes are named gen_ai.operation.name, gen_ai.provider.name and gen_ai.request.model.

OpenLIT interface: trace of a LangGraph agent, with the detail of the chat gpt-4o-mini span and its gen\_ai.\* attributes in the Span Attributes tab

Source : github.com

Image source: openlit/openlit repository, Apache 2.0 licence. This breakdown makes the trace usable: one span per call, one parent per conversation. You see where the time goes and where the tokens go.

Deciding what to capture: prompts, length, PII

By default, the Python SDK captures message content. That helps debugging, but it is risky: prompts often hold customer data. Three settings frame the decision.

  • capture_message_content=False (variable OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT) keeps prompts and answers out of traces. The default is True.
  • max_content_length truncates the stored content.
  • guards=[openlit.PII(action="redact")] masks sensitive values before the prompt is sent. The documentation states that this rewrite only applies to single-message prompts.

Our recommendation, untested: in production, switch content off first, then turn it back on only in a pre-production environment.

Steps: from zero to a first trace

  1. Install the SDK with pip install openlit.
  2. Pick the entry point: openlit.init() in the code, or openlit-instrument in front of the command.
  3. Set OTEL_SERVICE_NAME and OTEL_DEPLOYMENT_ENVIRONMENT. Without them, the documentation gives both the value default.
  4. Point OTEL_EXPORTER_OTLP_ENDPOINT at the OpenLIT dashboard or at your Collector.
  5. Call the model once, then look for the service in the interface.

To check that it works, the trace must contain a chat span with gen_ai.request.model and the token count. If it stays empty, first check port 4318 and make sure init() runs before the LLM client is imported.

One precedence rule to remember, from the configuration page: environment variables win over CLI arguments, which win over SDK parameters. When in doubt, inspect the process variables.

Which backend should receive the data?

The documentation says you can skip the bundled stack and send OTLP to another OpenTelemetry backend. The rest is classic engineering: a Collector in front, clean resource attributes, suitable retention. Token-side cost tracking is covered in our article on the cost of an AI agent.

Dynatrace is not mentioned in the project pages we read, and we did not verify that integration. For Dynatrace, rely only on docs.dynatrace.com, keeping SaaS and Managed apart.

Best practices

  • Set service_name and the environment: without them, all your services end up under « default ».
  • Change the dashboard default credentials (the documentation example uses a public account) before any network exposure.
  • Turn off message capture as soon as prompts contain personal data.
  • Keep the SDK up to date: the GenAI conventions evolve and attribute names can change.
  • Measure the instrumentation overhead on your own load: we did not.

Frequently asked questions

Does OpenLIT replace OpenTelemetry?

No. OpenLIT is an SDK and a platform built on OpenTelemetry. It exports standard OTLP and follows the gen_ai.* conventions.

Do I need the OpenLIT dashboard?

No. It helps you start quickly and can be replaced by another OTLP backend. The SDK stays the same.

Are prompts stored in the traces?

By default, yes, for the Python SDK. Use capture_message_content=False to avoid it.

Conclusion

OpenLIT is worth a trial if you want standard GenAI traces without writing spans by hand. Pick the entry point according to who controls the code, then decide what traces may contain. Repository: openlit/openlit; documentation: docs.openlit.io.

span.observer is an independent community blog, not affiliated with Dynatrace LLC or OpenLIT. Trademarks belong to their owners. Tools change quickly: check commands and options in the official documentation.


Originally published on span.observer. French version: https://span.observer/fr/openlit-opentelemetry-application-llm/.

來源:DEV Community · dev.to