Skip to content
C.W.K.
Lesson 02 of 05 · published

The Test That Prices the Worst Case

~15 min · testing, token-budget, vision-tokens, worst-case, invariants

Level 0Closed Lens
0 XP0/45 lessons0/13 achievements
0/100 XP to next level100 XP to go0% complete

A Budget Nobody Checks Is a Wish

Writing "look costs at most 1,600 tokens" in a document is easy. Keeping it true while forty people (or forty sessions of one Pippa) add fields, raise text limits and change picture sizes is the hard part. Oriel's answer is invariant 10: every look has a stated cost and a failing test. A new look action lands with its budget row and its worst case in backend/tests/test_look_budgets.py, and the test fails before any brain ever pays for an answer that grew.

Price the Picture for the Dearest Reader

Oriel does not know which brain will receive a look. It might be Claude, GPT or Gemini, and they count image tokens by different formulas. So the code implements three formulas and charges the maximum:

  • Claude: scale the long edge down to 1,568 px if needed, then width × height ÷ 750.
  • OpenAI (its tile formula, used by the GPT-4o family): scale into 2,048 px, then the short side to 768 px, then 85 plus 170 for each 512 px tile.
  • Gemini: 258 for a small image, else 258 for each 768 px tile.

Run the sizes Oriel actually returns through them and you can see why the picture sizes are what they are:

SizeClaudeOpenAIGeminiCharged
1280×720 (one input)1,2291,1055161,229
1536×648 (both decks)1,3281,1055161,328
1536×864 (replay sheet)1,7701,1051,0321,770
1024×1024 (largest crop)1,3997651,0321,399
1920×1080 (not used)1,8441,1051,5481,844

The ceiling is honest only for models that count by one of these formulas. OpenAI's smaller models count image patches instead and can charge more (by OpenAI's published pricing, a 1024×1024 picture costs a GPT-4.1-mini about 1,659 tokens, above the 1,399 charged here), so a brain that counts some other way is outside this guarantee until its formula is added.

The last row is the one that decided the viewing tier, as the next lesson shows: a 1080p frame alone costs more than the whole 1,600-token look budget, before any text.

Price the Text at Its Caps

Text is priced conservatively, three characters to a token, and it is priced at its worst, not its typical size. The test builds, for every action, the longest note the code can write: every text field at its cap (the read line, the asker's reason echoed back, the fallback note, the stale note), every fixed field every answer carries (the spoiler ladder, the cost block, a large look id), fifteen sessions in a session list, nine frame labels on a replay. Where two fields can never appear together (a stale note replaces the echo of the reason), it prices both versions and keeps the larger.

That last detail is a design choice in the product, not just in the test. When a "now" frame turns out to be old, its stale line takes the place of the echoed reason, which keeps the look inside its budget. The test is what forced that choice: adding the stale note on top of everything else would have broken the ceiling, so the answer was shaped to fit it.

Three Small Tests Do a Lot

The file is short and each test closes a different hole:

  • test_every_action_has_a_budget: the set of budgeted actions equals the set of priced worst cases. A new action with no worst case fails.
  • test_worst_case_fits_every_budget: for each action, worst image plus worst text is within its budget.
  • test_vendor_formulas_take_the_dearest: spot checks the Claude and OpenAI formulas and that the charge is at least OpenAI's at one size. It is thinner than its name: a refactor that dropped the Gemini formula, or Claude's, from the maximum would still pass it, which is a gap worth closing with one assertion per vendor.

And one test guards the boundary between apps: the row for a play look with Dad's words must sit at or under 2,100, the ceiling cwkPippa's door uses for a look today, and must not be an action a brain can request. The 2,100 in the test is a copy, not a read of cwkPippa's value, so it catches Oriel growing past the line but would not notice cwkPippa moving the line.

Code

Three vendors, charged at the dearest (trimmed excerpt, backend/looks.py)·python
def claude_tokens(width: int, height: int) -> int:
    scale = min(1.0, 1568 / max(width, height))
    return math.ceil((width * scale) * (height * scale) / 750)


def openai_tokens(width: int, height: int) -> int:
    scale = min(1.0, 2048 / max(width, height))
    w, h = width * scale, height * scale
    if min(w, h) > 768:
        shrink = 768 / min(w, h)
        w, h = w * shrink, h * shrink
    return 85 + 170 * math.ceil(w / 512) * math.ceil(h / 512)


def gemini_tokens(width: int, height: int) -> int:
    if width <= 384 and height <= 384:
        return 258
    return 258 * math.ceil(width / 768) * math.ceil(height / 768)


def image_tokens(width: int, height: int) -> int:
    """The image's cost to the most expensive vessel that could receive it."""
    return max(claude_tokens(width, height), openai_tokens(width, height), gemini_tokens(width, height))


def text_tokens(text: str) -> int:
    """A conservative count: three characters a token."""
    return math.ceil(len(text) / 3)
The tests that hold every budget (trimmed excerpt, backend/tests/test_look_budgets.py)·python
def test_every_action_has_a_budget():
    assert set(looks.BUDGETS) == set(WORST_IMAGE)


def test_worst_case_fits_every_budget():
    for action, size in WORST_IMAGE.items():
        image = looks.image_tokens(*size) if size else 0
        total = image + looks.text_tokens(_worst_text(action))
        assert total <= looks.BUDGETS[action], f"{action}: {total} tokens over {looks.BUDGETS[action]}"


def test_dads_words_never_outgrow_cwkpippas_door():
    assert looks.BUDGETS["play_look"] <= 2100
    assert "play_look" not in looks.ACTIONS and set(looks.ACTIONS) | {"play_look"} == set(looks.BUDGETS)

External links

Exercise

Copy the three formulas into a file and reproduce the table in this lesson. Then design a new action, diff, that shows a moment before and after an event, each frame at 1280×360. Decide how it returns them under Oriel's one-picture rule, price it, decide whether it fits a 1,600 budget, and write the worst-case test for it.
Hint
Oriel's contract says at most one JPEG per answer, so the honest version of diff composes the two halves into one 1280×720 image (1,229 tokens charged) and leaves about 370 tokens of text. If your design needs two separate images, it needs a new contract, not a bigger budget.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign in — Please sign in to comment.

No comments yet — be the first.