Prompt caching correctly — the variable input goes LAST — step 8 of 9
Write estimate_cost(stable_tokens, variable_tokens, calls, cached, input_rate) that:
- Returns the total cost in dollars (a float, rounded to 2 places).
- When
cached=False:input_rate * (stable_tokens + variable_tokens) * calls / 1_000_000. - When
cached=True:- Cache write (one-time):
input_rate * 1.25 * stable_tokens / 1_000_000 - Cache reads (calls - 1 of them):
input_rate * 0.1 * stable_tokens * (calls - 1) / 1_000_000 - Variable tokens (full price every call):
input_rate * variable_tokens * calls / 1_000_000 - Sum them.
- Cache write (one-time):
Two cases run for you. Expected output:
uncached: 2.46
cached: 0.33
savings: 2.13
⌘↵ runs the editor.read, then continue.
Write estimate_cost(stable_tokens, variable_tokens, calls, cached, input_rate) that:
- Returns the total cost in dollars (a float, rounded to 2 places).
- When
cached=False:input_rate * (stable_tokens + variable_tokens) * calls / 1_000_000. - When
cached=True:- Cache write (one-time):
input_rate * 1.25 * stable_tokens / 1_000_000 - Cache reads (calls - 1 of them):
input_rate * 0.1 * stable_tokens * (calls - 1) / 1_000_000 - Variable tokens (full price every call):
input_rate * variable_tokens * calls / 1_000_000 - Sum them.
- Cache write (one-time):
Two cases run for you. Expected output:
uncached: 2.46
cached: 0.33
savings: 2.13
this step needs the editor
on desktop today; in the app (coming soon). save your spot and we'll bring you back here when you're ready.