Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(signals): bake tiktoken encoding cache into images (#70952)

posthog[bot]6bea8cae0b44
diff --git a/Dockerfile b/Dockerfile
index d45369d1bbd6..d08b42db1f87 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -174,6 +174,15 @@ RUN --mount=type=cache,id=uv-libxmlsec1.2.37-2,target=/root/.cache/uv \
ENV PATH=/python-runtime/bin:$PATH \
PYTHONPATH=/python-runtime
+# Pre-warm the tiktoken BPE encoding cache into the image so runtime token counting reads the
+# blobs from disk instead of fetching them from OpenAI's blob host on first use, which fails under
+# restricted egress or flaky DNS. Covers the two encodings our callers resolve to: o200k_base
+# (gpt-4o) and cl100k_base (text-embedding-3-small). The cache is keyed by the blob URL, which is
+# stable for the pinned tiktoken version, so the baked files are used verbatim at runtime.
+ENV TIKTOKEN_CACHE_DIR=/code/.tiktoken_cache
+RUN mkdir -p "$TIKTOKEN_CACHE_DIR" && \
+ python -c "import tiktoken; [tiktoken.get_encoding(e) for e in ('o200k_base', 'cl100k_base')]"
+
# Add in Django deps
COPY manage.py manage.py
COPY common/esbuilder common/esbuilder
@@ -377,6 +386,11 @@ COPY --from=frontend-build --chown=posthog:posthog /code/frontend/src/products.j
# Copy the GeoLite2-City database from the fetch-geoip-db stage.
COPY --from=fetch-geoip-db --chown=posthog:posthog /code/share/GeoLite2-City.mmdb /code/share/GeoLite2-City.mmdb
+# Copy the pre-warmed tiktoken encoding cache and point tiktoken at it so token counting never
+# reaches out to OpenAI's blob host at runtime (see the posthog-build stage for details).
+COPY --from=posthog-build --chown=posthog:posthog /code/.tiktoken_cache /code/.tiktoken_cache
+ENV TIKTOKEN_CACHE_DIR=/code/.tiktoken_cache
+
# Copy plugin transpiler (used by Django for site destinations/apps). The transpiler dist is a
# self-contained esbuild bundle (build.mjs uses bundle:true), so only its own dist + node_modules
# are needed at runtime — the full root /code/node_modules COPY was redundant.
diff --git a/Dockerfile.llm-analytics b/Dockerfile.llm-analytics
index 43b4332862c3..9526b3cabc97 100644
--- a/Dockerfile.llm-analytics
+++ b/Dockerfile.llm-analytics
@@ -61,7 +61,15 @@ COPY bin/temporal-django-worker bin/temporal-django-worker
ENV PATH=/python-runtime/bin:$PATH \
PYTHONPATH=/python-runtime \
- POSTHOG_SENTIMENT_MODEL_CACHE=/opt/posthog-sentiment-model
+ POSTHOG_SENTIMENT_MODEL_CACHE=/opt/posthog-sentiment-model \
+ TIKTOKEN_CACHE_DIR=/code/.tiktoken_cache
+
+# Pre-warm the tiktoken BPE encoding cache into the image so runtime token counting reads the
+# blobs from disk instead of fetching them from OpenAI's blob host on first use, which fails under
+# restricted egress or flaky DNS. Covers the two encodings our callers resolve to: o200k_base
+# (gpt-4o) and cl100k_base (text-embedding-3-small).
+RUN mkdir -p "$TIKTOKEN_CACHE_DIR" && \
+ python -c "import tiktoken; [tiktoken.get_encoding(e) for e in ('o200k_base', 'cl100k_base')]"
# Pre-bake the sentiment ONNX model so workers don't download from HuggingFace on startup.
# Downloads from a pinned HuggingFace revision, exports to ONNX, verifies file integrity
diff --git a/posthog/helpers/tiktoken_encoding.py b/posthog/helpers/tiktoken_encoding.py
index 4bb7d7e9b1fd..dd6464633fb9 100644
--- a/posthog/helpers/tiktoken_encoding.py
+++ b/posthog/helpers/tiktoken_encoding.py
@@ -4,6 +4,11 @@ Tiktoken may download and verify encoding blobs on first use. Centralizing
access avoids duplicate lazy-load logic and keeps import-time failures from
transient hash mismatches out of unrelated modules.
+In the production images the blobs are pre-warmed into ``TIKTOKEN_CACHE_DIR``
+at build time (see ``Dockerfile`` / ``Dockerfile.llm-analytics``), so the first
+use reads from disk and never reaches OpenAI's blob host — restricted-egress or
+flaky-DNS environments can't turn token counting into a network failure.
+
Use two proxy model names only:
- LLM-facing token counts → ``gpt-4o`` (``o200k_base`` in tiktoken).
- Embedding-facing token counts → ``text-embedding-3-small`` (``cl100k_base``).