Skip to content

Commit f48db67

Browse files
Prewarm tiktoken cache to avoid Foundry egress (#18)
* Prewarm tiktoken cache for Foundry runtime Avoid runtime DNS/egress failures by caching cl100k_base during image build and setting TIKTOKEN_CACHE_DIR in the container. * Fix tiktoken prewarm to use venv python Use /app/.venv/bin/python so the build step can import tiktoken after uv sync.
1 parent 757f855 commit f48db67

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Dockerfile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@ RUN uv sync --frozen --no-dev
1616
COPY src ./src
1717
COPY artifacts ./artifacts
1818

19+
# Pre-warm tiktoken encoding cache during build so the runtime does not
20+
# need outbound access to openaipublic.blob.core.windows.net.
21+
RUN mkdir -p /app/.tiktoken_cache \
22+
&& TIKTOKEN_CACHE_DIR=/app/.tiktoken_cache /app/.venv/bin/python -c 'import tiktoken; tiktoken.get_encoding("cl100k_base")'
23+
1924

2025
FROM python:3.13-slim AS runtime
2126

2227
ARG SERVER_OPENAPI="{}"
2328

2429
ENV PYTHONUNBUFFERED=1 \
2530
PATH="/app/.venv/bin:${PATH}" \
26-
PYTHONPATH="/app"
31+
PYTHONPATH="/app" \
32+
TIKTOKEN_CACHE_DIR="/app/.tiktoken_cache"
2733

2834
WORKDIR /app
2935

@@ -33,9 +39,10 @@ RUN groupadd --gid 5000 app \
3339
COPY --from=builder --chown=5000:5000 /app/.venv /app/.venv
3440
COPY --from=builder --chown=5000:5000 /app/src ./src
3541
COPY --from=builder --chown=5000:5000 /app/artifacts ./artifacts
42+
COPY --from=builder --chown=5000:5000 /app/.tiktoken_cache /app/.tiktoken_cache
3643
COPY --chown=5000:5000 pyproject.toml uv.lock README.md ./
3744

38-
RUN mkdir -p /app/data/.dspy_cache && chown -R 5000:5000 /app
45+
RUN mkdir -p /app/data/.dspy_cache /app/.tiktoken_cache && chown -R 5000:5000 /app
3946

4047
LABEL server.openapi="${SERVER_OPENAPI}"
4148

0 commit comments

Comments
 (0)