C.W.K.
Stream
Lesson 08 of 10 · published

Cost Engineering — Where the Money Goes

~14 min · production, cost

Level 0Apprentice
0 XP0/100 lessons0/14 achievements
0/120 XP to next level120 XP to go0% complete

The bill has predictable shape

For most prompt-using systems in 2026, the cost is dominated by input tokens (because context is large), then reasoning tokens (when extended thinking is on), then output tokens, then everything else. Caching, model tier choice, and reasoning routing are the three biggest levers.

Levers in order of impact

  1. Prompt caching — 5–10× reduction on stable prefixes.
  2. Model-tier routing — send routine requests to mid-tier; reserve top-tier for hard ones.
  3. Reasoning budget routing — only enable reasoning when needed.
  4. Context trimming — RAG top-K tighter, document compression, dropping turns from history.
  5. Output discipline — schema-bound outputs are smaller than free-form ones.
  6. Provider competition — periodic comparison; switch where appropriate.

What not to optimize for

  • Don't trade quality for cost without measuring the quality drop.
  • Don't optimize average cost when tail cost is the issue (one slow long-context call drowns 1,000 cheap ones).
  • Don't optimize cost without optimizing operational complexity.

Code

Cost attribution per request·python
@dataclass
class CostBreakdown:
    input_tokens: int
    cache_read_tokens: int
    cache_write_tokens: int
    output_tokens: int
    thinking_tokens: int

    def total_usd(self, rates) -> float:
        return (
            self.input_tokens     * rates.input_per_tok
          + self.cache_read_tokens * rates.cache_read_per_tok
          + self.cache_write_tokens * rates.cache_write_per_tok
          + self.output_tokens    * rates.output_per_tok
          + self.thinking_tokens  * rates.thinking_per_tok
        )

External links

Exercise

For one production endpoint, attribute last week's cost across input / cached / output / thinking. Identify the largest bucket. Apply the matching lever (caching, trimming, routing) and measure the change.

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 inPlease sign in to comment.

No comments yet — be the first.