Course ES
← back to chapter

Cost meter

Run a prompt and see input/output tokens, dollars (illustrative pricing), and latency alongside the answer. Reading `response.usage` is the habit that keeps the bill from surprising you.

Showcase — Cost meter

Run a prompt and see input/output tokens, dollars (illustrative pricing), and latency alongside the answer. Reading response.usage is the habit that keeps the bill from surprising you.

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 — a call plus a token/cost/latency report.
  • frontend/app/page.tsx — prompt box, answer + metrics.

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 cost-meter.zip

unzip cost-meter.zip
cd cost-meter
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 1 (OpenAI): meter every call.

Run a prompt and see what it actually cost — input tokens, output tokens, dollars
(illustrative pricing), and wall-clock latency, right next to the answer. The
habit this builds is the point: read response.usage and the bill stops surprising
you.
"""
import time

from openai import OpenAI

_client = OpenAI()

_MODEL = "gpt-5.4-nano"
_PRICES = {"gpt-5.4-nano": (0.05, 0.40)}  # USD per 1M (in, out) — illustrative


def run(prompt: str) -> str:
    p = prompt.strip()
    if not p:
        return "Enter a prompt to meter."
    start = time.time()
    response = _client.responses.create(model=_MODEL, input=[{"role": "user", "content": p}])
    seconds = time.time() - start
    u = response.usage
    p_in, p_out = _PRICES[_MODEL]
    cost = (u.input_tokens * p_in + u.output_tokens * p_out) / 1_000_000
    return (f"{response.output_text}\n\n"
            f"--- metrics ---\n"
            f"input tokens:  {u.input_tokens}\n"
            f"output tokens: {u.output_tokens}\n"
            f"cost:          ${cost:.6f}  (illustrative pricing)\n"
            f"latency:       {seconds:.2f}s")

backend/ai_gemini.py

"""Showcase 1 (Gemini): meter every call.

Same metrics, read from response.usage_metadata.
"""
import os
import time

from google import genai
from google.genai import types

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

_MODEL = "gemini-3.1-flash-lite"
_PRICES = {"gemini-3.1-flash-lite": (0.05, 0.30)}  # USD per 1M (in, out) — illustrative


def run(prompt: str) -> str:
    p = prompt.strip()
    if not p:
        return "Enter a prompt to meter."
    start = time.time()
    response = _client.models.generate_content(
        model=_MODEL,
        contents=[types.Content(role="user", parts=[types.Part(text=p)])],
    )
    seconds = time.time() - start
    u = response.usage_metadata
    out = u.candidates_token_count or 0
    p_in, p_out = _PRICES[_MODEL]
    cost = (u.prompt_token_count * p_in + out * p_out) / 1_000_000
    return (f"{response.text or ''}\n\n"
            f"--- metrics ---\n"
            f"input tokens:  {u.prompt_token_count}\n"
            f"output tokens: {out}\n"
            f"cost:          ${cost:.6f}  (illustrative pricing)\n"
            f"latency:       {seconds:.2f}s")

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