BerriAI/litellm · error · Exception

invalid LAGO_API_CHARGE_BY set

Error message

invalid LAGO_API_CHARGE_BY set

What it means

Lago billing callback guard: the LAGO_API_CHARGE_BY setting did not match any of the allowed literals ('end_user_id', 'team_id', 'user_id'), so the callback cannot decide which identifier to bill against in Lago.

Solutions

  1. Set LAGO_API_CHARGE_BY to a supported value such as 'user_id', 'end_user_id', or 'team_id'.

Example fix

export LAGO_API_CHARGE_BY=user_id
Defensive patterns

Strategy: validation

When it happens

Trigger: LAGO_API_CHARGE_BY is set to a value the integration does not support.

Common situations: Typo in the env var value or using a charge mode this integration version does not implement.


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

Appendix: source

Thrown at litellm/integrations/lago.py:97

        litellm_params: Final = kwargs.get("litellm_params", {}) or {}
        proxy_server_request: Final = litellm_params.get("proxy_server_request") or {}
        end_user_id: Final = proxy_server_request.get("body", {}).get("user", None)
        user_id: Final = litellm_params["metadata"].get("user_api_key_user_id", None)
        team_id: Final = litellm_params["metadata"].get("user_api_key_team_id", None)
        litellm_params["metadata"].get("user_api_key_org_id", None)

        charge_by: Literal["end_user_id", "team_id", "user_id"] = "end_user_id"
        external_customer_id: str | None = None

        if os.getenv("LAGO_API_CHARGE_BY", None) is not None and isinstance(os.environ["LAGO_API_CHARGE_BY"], str):
            if os.environ["LAGO_API_CHARGE_BY"] in [
                "end_user_id",
                "user_id",
                "team_id",
            ]:
                charge_by = os.environ["LAGO_API_CHARGE_BY"]
            else:
                raise Exception("invalid LAGO_API_CHARGE_BY set")

        if charge_by == "end_user_id":
            external_customer_id = end_user_id
        elif charge_by == "team_id":
            external_customer_id = team_id
        elif charge_by == "user_id":
            external_customer_id = user_id

        if external_customer_id is None:
            raise Exception(
                f"External Customer ID is not set. Charge_by={charge_by}. User_id={user_id}. End_user_id={end_user_id}. Team_id={team_id}"
            )

        returned_val: Final = {
            "event": {
                "transaction_id": str(uuid.uuid4()),
                "external_subscription_id": external_customer_id,
                "code": os.getenv("LAGO_API_EVENT_CODE"),

View on GitHub (pinned to 77b7c6c40c)