Course ES
← back to chapter

Speed vs quality

The same prompt to a small fast model and a larger slower one, with latencies side by side. Most tasks don't need the big model; the skill is telling which ones do.

Showcase — Speed vs quality

The same prompt to a small fast model and a larger slower one, with latencies side by side. Most tasks don't need the big model; the skill is telling which ones do.

Run

bash bootstrap-secrets.sh              # reads ../../../../.env, writes secrets/
docker compose up --build              # default: PROVIDER=openai

Open http://localhost:3000. Gemini: PROVIDER=gemini docker compose up --build.

What's where

  • backend/ai_openai.py / backend/ai_gemini.py — two models, timed, one prompt.
  • frontend/app/page.tsx — prompt box, both answers with latency.

Stop

docker compose down

Run locally

Download the project as a ZIP and run it with Docker. Brings up a FastAPI backend + Next.js frontend on localhost:3000.

Download speed-vs-quality.zip

unzip speed-vs-quality.zip
cd speed-vs-quality
bash bootstrap-secrets.sh   # one-time: pulls API keys into ./secrets
docker compose up --build   # default provider: openai
# or:  PROVIDER=gemini docker compose up --build

Type some input, pick a provider, and run the same code shown in Source against the live API. Sign-in required.


  

The same modules the Run button hits. The whole project (frontend, Dockerfile, compose) is in the ZIP under README.

backend/ai_openai.py

"""Showcase 3 (OpenAI): speed vs quality.

The same prompt to a small fast model and a larger slower one, side by side with
their latencies. Most of the time the fast model is good enough and a fraction of
the cost and wait — the skill is knowing which tasks actually need the big model.
"""
import time

from openai import OpenAI

_client = OpenAI()

_FAST = "gpt-5.4-nano"
_QUALITY = "gpt-5.4"


def _call(model: str, prompt: str) -> tuple[str, float]:
    start = time.time()
    response = _client.responses.create(model=model, input=[{"role": "user", "content": prompt}])
    return response.output_text, time.time() - start


def run(prompt: str) -> str:
    p = prompt.strip()
    if not p:
        return "Enter a prompt to send to both models."
    fast_out, fast_s = _call(_FAST, p)
    qual_out, qual_s = _call(_QUALITY, p)
    return (f"FAST — {_FAST}  ({fast_s:.2f}s)\n{fast_out}\n\n"
            f"{'-' * 40}\n\n"
            f"QUALITY — {_QUALITY}  ({qual_s:.2f}s)\n{qual_out}")

backend/ai_gemini.py

"""Showcase 3 (Gemini): speed vs quality.

A fast Gemini model and a stronger one on the same prompt, with latencies.
"""
import os
import time

from google import genai
from google.genai import types

_client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])

_FAST = "gemini-3.1-flash-lite"
_QUALITY = "gemini-3.1-pro"


def _call(model: str, prompt: str) -> tuple[str, float]:
    start = time.time()
    response = _client.models.generate_content(
        model=model,
        contents=[types.Content(role="user", parts=[types.Part(text=prompt)])],
    )
    return (response.text or ""), time.time() - start


def run(prompt: str) -> str:
    p = prompt.strip()
    if not p:
        return "Enter a prompt to send to both models."
    fast_out, fast_s = _call(_FAST, p)
    qual_out, qual_s = _call(_QUALITY, p)
    return (f"FAST — {_FAST}  ({fast_s:.2f}s)\n{fast_out}\n\n"
            f"{'-' * 40}\n\n"
            f"QUALITY — {_QUALITY}  ({qual_s:.2f}s)\n{qual_out}")

Project files

  • .gitignore
  • README.es.md
  • README.md
  • backend/Dockerfile
  • backend/ai_gemini.py
  • backend/ai_openai.py
  • backend/main.py
  • backend/requirements.txt
  • bootstrap-secrets.sh
  • docker-compose.yml
  • frontend/Dockerfile
  • frontend/app/layout.tsx
  • frontend/app/page.tsx
  • frontend/next.config.ts
  • frontend/package.json
  • frontend/tsconfig.json