LLM Engineering · Exercise 1 of 6Next
Beginner
Price a request and a chat
Write request_cost(tokens_in, tokens_out, price_in_per_mtok, price_out_per_mtok) that returns the cost in dollars. Prices are dollars per million tokens.
Write chat_input_tokens(turns, system, per_exchange, new_message, keep_last=None) that returns the total input tokens over a whole chat. Every turn sends the system prompt, the earlier exchanges that are kept (all of them when keep_last is None, otherwise only the last keep_last) and the new message.
What Check tests
- 1,200 input and 300 output tokens at 0.5 and 1.5 dollars per MTok cost 0.00105 dollars
- A million output tokens cost exactly the output price
- A one-turn chat sends the system prompt and one message
- A 10-turn chat that keeps everything sends 14,050 input tokens
- Keeping only the last 3 exchanges sends 9,640
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.