{"record":{"id":"ac7d710b7a01dac0","repo":"BerriAI/litellm","slug":"posthog-api-key-is-not-set-set-posthog-api-key","errorCode":null,"errorMessage":"POSTHOG_API_KEY is not set, set 'POSTHOG_API_KEY=<>'","messagePattern":"POSTHOG_API_KEY is not set, set 'POSTHOG_API_KEY=<>'","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/integrations/posthog.py","lineNumber":55,"sourceCode":"class PostHogLogger(CustomBatchLogger):\n    def __init__(self, **kwargs):\n        \"\"\"\n        Initializes the PostHog logger, checks if the correct env variables are set\n\n        Required environment variables:\n        `POSTHOG_API_KEY` - your PostHog API key\n        `POSTHOG_API_URL` - your PostHog API URL (defaults to https://app.posthog.com)\n        \"\"\"\n        try:\n            verbose_logger.debug(\"PostHog: in init posthog logger\")\n\n            self.is_mock_mode = should_use_posthog_mock()\n            if self.is_mock_mode:\n                create_mock_posthog_client()\n                verbose_logger.debug(\"[POSTHOG MOCK] PostHog logger initialized in mock mode\")\n\n            if os.getenv(\"POSTHOG_API_KEY\", None) is None:\n                raise Exception(\"POSTHOG_API_KEY is not set, set 'POSTHOG_API_KEY=<>'\")\n\n            self.async_client = get_async_httpx_client(llm_provider=httpxSpecialProvider.LoggingCallback)\n            self.sync_client = _get_httpx_client()\n\n            self.POSTHOG_API_KEY = os.getenv(\"POSTHOG_API_KEY\")\n            posthog_api_url: Final = os.getenv(\"POSTHOG_API_URL\", \"https://us.i.posthog.com\")\n            self.posthog_host = posthog_api_url.rstrip(\"/\")\n            self.capture_url = f\"{self.posthog_host}/batch/\"\n\n            self._async_initialized = False\n            self.flush_lock = None\n            self.log_queue = []\n\n            # Register cleanup handler to flush internal queue on exit\n            atexit.register(self._flush_on_exit)\n\n            super().__init__(**kwargs, flush_lock=None, batch_size=POSTHOG_MAX_BATCH_SIZE)\n","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/integrations/posthog.py#L37-L73","documentation":"litellm's PostHog telemetry callback requires an API key at construction time: PostHogLogger.__init__ raises this Exception when the POSTHOG_API_KEY environment variable is absent (mock-mode setup happens earlier but does not bypass the check). The enclosing handler logs it via verbose_logger.exception and re-raises, so callback initialization fails loudly. Note it only checks for None — an empty string passes init but produces auth failures later at send time.","triggerScenarios":"Registering the posthog callback — litellm.callbacks = ['posthog'] in the SDK or success_callback: ['posthog'] under litellm_settings in proxy config — in a process where POSTHOG_API_KEY was never exported, is set in a different shell/container, or exists only in a .env file that this process does not load.","commonSituations":"Works locally (var exported in your shell) but fails in Docker/Kubernetes because the secret was not mounted into that container; CI runners without the env; proxy started with a config that references posthog while the deployment env omitted the key.","solutions":["Export POSTHOG_API_KEY in the exact process that runs litellm and verify: python -c \"import os; print(repr(os.getenv('POSTHOG_API_KEY')))\"","For the proxy, put it in the .env beside the config or inject it as a container secret / environment_variables block","If telemetry is not intended, remove 'posthog' from callbacks entirely","Set POSTHOG_API_URL too if you use EU cloud or self-hosted PostHog (defaults to https://us.i.posthog.com)"],"exampleFix":"# before\nlitellm.callbacks = ['posthog']  # POSTHOG_API_KEY missing -> Exception on first init\n\n# after\nimport os\nif not os.getenv('POSTHOG_API_KEY'):\n    raise RuntimeError('Refusing to enable posthog callback: POSTHOG_API_KEY not set')\nlitellm.callbacks = ['posthog']","handlingStrategy":"validation","validationCode":"import os\n\nif not os.getenv('POSTHOG_API_KEY'):\n    raise RuntimeError('POSTHOG_API_KEY is required when the posthog callback is enabled')\nlitellm.callbacks = ['posthog']","typeGuard":null,"tryCatchPattern":"try:\n    litellm.callbacks = ['posthog']\nexcept Exception as e:\n    logger.warning('posthog callback init failed: %s', e)\n    litellm.callbacks = []","preventionTips":["Declare POSTHOG_API_KEY in every environment that registers the callback (compose/k8s env blocks)","Fail fast at boot with a clear message instead of discovering it on first request","Also pin POSTHOG_API_URL to the correct region to avoid later send failures"],"tags":["posthog","telemetry","missing-env-var","callback","configuration"],"backgroundTag":"missing-env-var","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}