From 35ccfac3fd0b3913d402c319f81d8cfce62d4744 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 26 Jul 2023 17:34:50 -0300 Subject: [PATCH] aider: Updated the output of `/tokens` to include the total cost. # Aider chat conversation: USER: udpate the output of /tokens to include the total $ cost ASSISTANT: To include the total cost in the output of the `/tokens` command, we need to modify the `cmd_tokens` method in the `Commands` class in the `aider/commands.py` file. We will add a new variable `total_cost` to keep track of the total cost. For each token count, we will calculate the cost by multiplying the token count by the model's `completion_price` (since we're considering the cost of generating these tokens). We will then add this cost to `total_cost`. Finally, we will add a line to the output to display the total cost. Here are the changes: python aider/commands.py <<<<<<< ORIGINAL total = 0 for tk, msg, tip in res: total += tk msg = msg.ljust(col_width) self.io.tool_output(f"{fmt(tk)} {msg} {tip}") self.io.tool_output("=" * width) self.io.tool_output(f"{fmt(total)} tokens total") ======= total = 0 total_cost = 0.0 for tk, msg, tip in res: total += tk cost = tk * self.coder.main_model.completion_price total_cost += cost msg = msg.ljust(col_width) self.io.tool_output(f"{fmt(tk)} {msg} {tip} (cost: ${cost:.2f})") self.io.tool_output("=" * width) self.io.tool_output(f"{fmt(total)} tokens total (cost: ${total_cost:.2f})") >>>>>>> UPDATED --- aider/commands.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/aider/commands.py b/aider/commands.py index 6fd5fa594..8f8e4ac26 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -142,13 +142,16 @@ class Commands: col_width = max(len(row[1]) for row in res) total = 0 + total_cost = 0.0 for tk, msg, tip in res: total += tk + cost = tk * self.coder.main_model.completion_price + total_cost += cost msg = msg.ljust(col_width) - self.io.tool_output(f"{fmt(tk)} {msg} {tip}") + self.io.tool_output(f"{fmt(tk)} {msg} {tip} (cost: ${cost:.2f})") self.io.tool_output("=" * width) - self.io.tool_output(f"{fmt(total)} tokens total") + self.io.tool_output(f"{fmt(total)} tokens total (cost: ${total_cost:.2f})") limit = self.coder.main_model.max_context_tokens remaining = limit - total