# ── AISaver — LLM Token-Efficient Proxy (FastAPI) ─────────────────────────────
FROM python:3.13-slim

# Cache tiktoken BPE files inside the image so there is no runtime download.
ENV TIKTOKEN_CACHE_DIR=/opt/tiktoken_cache \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

WORKDIR /app

# Install dependencies first (better layer caching)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Pre-download the cl100k_base encoding used by token_counter.py
RUN mkdir -p "$TIKTOKEN_CACHE_DIR" \
 && python -c "import tiktoken; tiktoken.get_encoding('cl100k_base')"

# Application code
COPY src ./src

EXPOSE 8000

HEALTHCHECK --interval=15s --timeout=4s --start-period=10s --retries=3 \
    CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://localhost:8000/health', timeout=3).status==200 else 1)"

CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]
