Skip to course content
Free FastAPI backend course

FastAPI for AI Backend Development

Unit 13.04: Refusing before you spend anything

The cheapest request is the one you refuse before spending anything.

Four pre-checks, none of which cost a token

Empty input, over-length input, a card number, and an exhausted budget.

The code runs four inputs through the pre-check.

import re

CARD = re.compile(r"\b\d{4}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}\b")
MAX_CHARS, DAILY_BUDGET_USD = 4000, 2.0
spent = 1.995


def precheck(text: str) -> tuple[bool, str]:
    if not text.strip():
        return False, "empty_input"
    if len(text) > MAX_CHARS:
        return False, "input_too_long"
    if CARD.search(text):
        return False, "input_contains_card_number"
    if spent + 0.01 > DAILY_BUDGET_USD:
        return False, "daily_budget_exhausted"
    return True, ""


for text in ["charged twice", "", "x" * 5000, "card 4111 1111 1111 1111"]:
    ok, reason = precheck(text)
    print(f"{text[:26]!r:30} -> {'call the model' if ok else 'REFUSE: ' + reason}")

print("\nEvery refusal here costs nothing -- no tokens, no latency, and the")
print("card number never reaches the provider or your logs.")

Every refusal here costs a string operation. No tokens, no latency, and - for the card number - the value never reaches the provider, your logs or your trace store.

The budget check is the one that saves money at scale. A runaway caller hits the ceiling and is refused, rather than being served until someone notices the invoice.

The mistake this prevents

The mistake is checking the budget after the call, when you know the actual cost. By then you have spent it - the check has to use an estimate and run first.

Takeaway

Pre-check input and budget before calling the model. Refusals then cost nothing, and sensitive input never leaves your process.