{"record":{"id":"e51f42afb674087e","repo":"BerriAI/litellm","slug":"invalid-lago-api-charge-by-set","errorCode":null,"errorMessage":"invalid LAGO_API_CHARGE_BY set","messagePattern":"invalid LAGO_API_CHARGE_BY set","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/integrations/lago.py","lineNumber":97,"sourceCode":"        litellm_params: Final = kwargs.get(\"litellm_params\", {}) or {}\n        proxy_server_request: Final = litellm_params.get(\"proxy_server_request\") or {}\n        end_user_id: Final = proxy_server_request.get(\"body\", {}).get(\"user\", None)\n        user_id: Final = litellm_params[\"metadata\"].get(\"user_api_key_user_id\", None)\n        team_id: Final = litellm_params[\"metadata\"].get(\"user_api_key_team_id\", None)\n        litellm_params[\"metadata\"].get(\"user_api_key_org_id\", None)\n\n        charge_by: Literal[\"end_user_id\", \"team_id\", \"user_id\"] = \"end_user_id\"\n        external_customer_id: str | None = None\n\n        if os.getenv(\"LAGO_API_CHARGE_BY\", None) is not None and isinstance(os.environ[\"LAGO_API_CHARGE_BY\"], str):\n            if os.environ[\"LAGO_API_CHARGE_BY\"] in [\n                \"end_user_id\",\n                \"user_id\",\n                \"team_id\",\n            ]:\n                charge_by = os.environ[\"LAGO_API_CHARGE_BY\"]\n            else:\n                raise Exception(\"invalid LAGO_API_CHARGE_BY set\")\n\n        if charge_by == \"end_user_id\":\n            external_customer_id = end_user_id\n        elif charge_by == \"team_id\":\n            external_customer_id = team_id\n        elif charge_by == \"user_id\":\n            external_customer_id = user_id\n\n        if external_customer_id is None:\n            raise Exception(\n                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}\"\n            )\n\n        returned_val: Final = {\n            \"event\": {\n                \"transaction_id\": str(uuid.uuid4()),\n                \"external_subscription_id\": external_customer_id,\n                \"code\": os.getenv(\"LAGO_API_EVENT_CODE\"),","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/integrations/lago.py#L79-L115","documentation":"Exception raised by the Lago integration when LAGO_API_CHARGE_BY is set but its value is not one of the allowed literals: end_user_id, user_id, team_id. It guards the mapping that decides which request identity becomes the Lago external customer id.","triggerScenarios":"Setting LAGO_API_CHARGE_BY to any other string — e.g. 'end-user-id' (hyphens), 'customer_id', 'organization', or with stray whitespace/quotes — in the environment of a process with the lago callback enabled.","commonSituations":"Config typo like END_USER_ID or hyphenated end-user-id; copying a value from docs of a different Lago SDK; values stored with surrounding quotes in YAML/docker-compose env entries.","solutions":["Set LAGO_API_CHARGE_BY to exactly one of: end_user_id, user_id, team_id","Remove the variable entirely if the default end_user_id is acceptable","Check docker-compose/k8s env entries for injected quotes or trailing whitespace"],"exampleFix":"# before\nLAGO_API_CHARGE_BY=end-user-id  # hyphens invalid\n\n# after\nLAGO_API_CHARGE_BY=end_user_id","handlingStrategy":"validation","validationCode":"import os\n\nVALID = {\"end_user_id\", \"user_id\", \"team_id\"}\nval = os.getenv(\"LAGO_API_CHARGE_BY\")\nif val is not None and val not in VALID:\n    raise RuntimeError(f\"LAGO_API_CHARGE_BY must be one of {sorted(VALID)}, got {val!r}\")","typeGuard":"from typing import Literal\n\ndef is_valid_charge_by(value: str | None) -> bool:\n    return value is None or value in {\"end_user_id\", \"user_id\", \"team_id\"}","tryCatchPattern":null,"preventionTips":["Validate the env var value at startup against the literal set","Use underscores (end_user_id), never hyphens","Lint env config in docker-compose/k8s for quoting artifacts"],"tags":["lago","litellm","configuration","environment","validation"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}