← Lesson
Tokens and Cost
Joeven
Run
Reset
Python loads on first run
INPUT_PER_M = 3.00 OUTPUT_PER_M = 15.00 def rough_tokens(text): n = 0 for w in text.split(): n += max(1, (len(w) + 3) // 4) return max(n, 1) def cost(prompt, completion): pt = rough_tokens(prompt) ct = rough_tokens(completion) dollars = pt * INPUT_PER_M / 1000000 + ct * OUTPUT_PER_M / 1000000 return pt, ct, dollars prompt = "system: be brief user: status of job 17?" completion = "I'll call get_job." print("single turn", cost(prompt, completion)) obs = "tool result: " + ("timeout " * 40) steps = 8 running = prompt bill = 0.0 for step in range(steps): pt, ct, d = cost(running, completion) bill += d running += " " + obs + " " + completion print("step", step + 1, "prompt_tok", pt, "usd", round(d, 6)) print("loop total usd (toy rates)", round(bill, 6)) print("final prompt tokens", rough_tokens(running))
Run to execute this in your browser. Nothing is sent to a server.