Using Agents Well33 of 37
Optional, for readers who know a little Python.
In Paying by the Token you saw that every turn of a conversation sends the whole history again, and in The Same Start Every Time that caching makes the repeated part cheap. Here you work out how much difference that makes.
Write the function session_cost(price, new_tokens). It gets:
price: the price in dollars per million input tokens, like 4.0new_tokens: a list of how many tokens each turn adds, oldest first, like [30000, 2000, 2000]Use this simple model of a session:
[30000, 2000, 2000], the turns send 30,000, then 32,000, then 34,000 tokens: 96,000 input tokens in all.Return the total number of input tokens, the cost without caching and the cost with caching. The code at the bottom prints them.
Example input (the price, then each turn's new tokens):
4
30000 2000 2000Output:
turns: 3
input tokens sent: 96000
without caching: 0.3840 dollars
with caching: 0.1608 dollarsThis model leaves out three things real caching has: writing to the cache costs a little more than normal input, the cache expires after a few minutes without use, and output tokens are paid separately. None of them changes the big picture. Once you've passed, try the 40-turn session in the tests and compare the two costs.
Sign in to try this exercise.