Objective
Learning objectives
- Explain why a curving graph means the rate of change itself is changing
- Distinguish a linear scenario from a nonlinear one before modeling it
- Plot a linear and a nonlinear function on the same axes for direct visual contrast
- Choose between a linear and nonlinear model for a real scenario and justify the choice
Hook
Every lesson in this phase so far has used a flat per-token rate: the same $0.002 per 1,000 tokens, whether it's your first request or your millionth. But what if a provider offered bulk discounts — cost per token gets cheaper the more you send? That single change breaks the defining property from Lesson 03: equal steps right no longer produce equal steps up. The graph won't be a straight line anymore. What is it instead?
See it
Picture two pricing plans plotted together. Plan A (flat rate) is the straight line you already know. Plan B (bulk-discounted) starts at the same point but bends — early on it climbs about like Plan A does, but the more tokens you add, the less each additional batch adds to the total. The line curves, visibly flattening as it goes right. That bend is the rate of change shrinking, drawn directly.
The curve's steepness is a different number at every point
On a line, "how steep is it here?" has exactly one answer, anywhere you ask. On a curve, "how steep is it here?" depends entirely on where — near the start, Plan B climbs almost like Plan A; far out, it's nearly flat. This idea — a rate of change that's itself a function of position, not a constant — is real and precise, not hand-wavy, and it gets its full formal treatment in Phase 04 of this course and later in Math for AI & ML's calculus material. For now, the job is just to see it.
Name it
A nonlinear function is any function whose graph isn't a straight line — equivalently, one whose rate of change isn't constant. That's a wide category (curves that flatten, curves that steepen, curves that wiggle) unified by one shared trait: rise-over-run gives a different answer depending on which two points you pick, unlike Lesson 03's linear functions where it never changes.
Code it
import math
import matplotlib.pyplot as plt
def cost_flat(tokens):
return (tokens / 1000) * 0.002
def cost_bulk(tokens):
return 0.002 * math.sqrt(tokens / 1000)
xs = [0, 1000, 2000, 4000, 8000, 16000]
flat = [cost_flat(x) for x in xs]
bulk = [cost_bulk(x) for x in xs]
for x, f, b in zip(xs, flat, bulk):
print(x, round(f, 5), round(b, 5))
plt.plot(xs, flat, marker="o", label="flat rate")
plt.plot(xs, bulk, marker="o", label="bulk discount")
plt.xlabel("tokens")
plt.ylabel("cost ($)")
plt.legend()
plt.savefig("pricing_comparison.png", dpi=150, bbox_inches="tight")
0 0.0 0.0
1000 0.002 0.002
2000 0.004 0.00283
4000 0.008 0.004
8000 0.016 0.00566
16000 0.032 0.008
Both plans start identically at (1000, 0.002) — same rate at the very first step. But look at how the gap opens: by 16,000 tokens, the flat plan costs 4 times what the bulk plan does (0.032 vs. 0.008), even though both started in exact agreement. That growing gap, not just the final numbers, is what "nonlinear" looks like in a table — and it's exactly what the two plotted lines show visually, one straight, one bending away from it.
def rate_between(x1, y1, x2, y2):
return (y2 - y1) / (x2 - x1)
segments = [(1000, 2000), (2000, 4000), (4000, 8000), (8000, 16000)]
for a, b in segments:
r = rate_between(a, cost_bulk(a), b, cost_bulk(b))
print(a, "to", b, ":", round(r, 9))
Four different segments of the exact same cost_bulk curve, and four different rates — each one smaller than the last. That's the numeric proof behind the visual bend: this function's rate of change isn't one number, it's shrinking as tokens increase. Compare this to Lesson 03, where rate_between would have printed the same value for every segment of a linear function, no matter which two points you picked.