BerriAI/litellm · error · ValueError

LANGTRACE_API_KEY not found in environment variables

Error message

LANGTRACE_API_KEY not found in environment variables

What it means

Raised when the 'langtrace' callback initializes: LiteLLM requires LANGTRACE_API_KEY in os.environ before building the OTel HTTP exporter that ships spans to https://langtrace.ai/api/trace. It is a hard precondition — the key becomes the Authorization credential for the trace endpoint — and fires at logger construction (first logged call or proxy startup).

Solutions

  1. Set LANGTRACE_API_KEY in the LiteLLM process environment and restart it.
  2. For the proxy, inject it via compose/k8s secrets or config.yaml environment_vars.
  3. Verify with printenv LANGTRACE_API_KEY in the same execution context as LiteLLM.

Example fix

# before
import litellm
litellm.success_callback = ['langtrace']  # ValueError: LANGTRACE_API_KEY not found

# after
import os
os.environ['LANGTRACE_API_KEY'] = 'your-langtrace-key'
import litellm
litellm.success_callback = ['langtrace']
Defensive patterns

Strategy: validation

Validate before calling

import os

if not os.environ.get('LANGTRACE_API_KEY'):
    raise RuntimeError('LANGTRACE_API_KEY must be set before enabling the langtrace callback')

Try / catch

try:
    litellm.completion(model='gpt-4o', messages=m)
except ValueError as e:
    if 'LANGTRACE_API_KEY' in str(e):
        raise RuntimeError('Set LANGTRACE_API_KEY in the LiteLLM process environment and restart')
    raise

Prevention

When it happens

Trigger: callbacks/success_callback containing 'langtrace' in a process without LANGTRACE_API_KEY; env var present locally but absent in the deployed proxy container, systemd unit, or CI runner.

Common situations: Copying a config.yaml with langtrace enabled into a new environment; forgetting to add the secret to docker-compose/k8s; typos in the variable name.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/7bd747a222699bf4. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/litellm_logging.py:4169

                _PROXY_DynamicRateLimitHandlerV3,
            )

            for callback in _in_memory_loggers:
                if isinstance(callback, _PROXY_DynamicRateLimitHandlerV3):
                    return callback

            if internal_usage_cache is None:
                raise Exception(f"Internal Error: Cache cannot be empty - internal_usage_cache={internal_usage_cache}")

            dynamic_rate_limiter_obj_v3 = _PROXY_DynamicRateLimitHandlerV3(internal_usage_cache=internal_usage_cache)

            if llm_router is not None and isinstance(llm_router, litellm.Router):
                dynamic_rate_limiter_obj_v3.update_variables(llm_router=llm_router)
            _in_memory_loggers.append(dynamic_rate_limiter_obj_v3)
            return dynamic_rate_limiter_obj_v3
        elif logging_integration == "langtrace":
            if "LANGTRACE_API_KEY" not in os.environ:
                raise ValueError("LANGTRACE_API_KEY not found in environment variables")
            _v2 = _maybe_construct_otel_v2("langtrace", _in_memory_loggers)
            if _v2 is not None:
                return _v2

            from litellm.integrations.opentelemetry import (
                OpenTelemetry,
                OpenTelemetryConfig,
            )

            otel_config = OpenTelemetryConfig(
                exporter="otlp_http",
                endpoint="https://langtrace.ai/api/trace",
            )
            os.environ["OTEL_EXPORTER_OTLP_TRACES_HEADERS"] = f"api_key={os.getenv('LANGTRACE_API_KEY')}"
            for callback in _in_memory_loggers:
                if isinstance(callback, OpenTelemetry) and callback.callback_name == "langtrace":
                    return callback
            _otel_logger = OpenTelemetry(config=otel_config, callback_name="langtrace")

View on GitHub (pinned to 77b7c6c40c)