Command Palette

Search for a command to run...

Hectal
Phase 6: Cost, Latency and Limits
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. 1,200 input and 300 output tokens at 0.5 and 1.5 dollars per MTok cost 0.00105 dollars
  2. A million output tokens cost exactly the output price
  3. A one-turn chat sends the system prompt and one message
  4. A 10-turn chat that keeps everything sends 14,050 input tokens
  5. 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.