Skip to content

Commit f3145cc

Browse files
committed
Fix session tokens calculation
1 parent 964cbb9 commit f3145cc

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Improve anthropic extraPayload requirement when adding models.
66
- Add message to when config failed to be parsed.
77
- Fix context completion for workspaces that are not git. #98
8+
- Fix session tokens calculation.
89

910
## 0.40.0
1011

src/eca/features/chat.clj

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
(:origin (first (filter #(= name (:name %)) all-tools))))
5353

5454
(defn ^:private usage-msg->usage
55+
"How this works:
56+
- tokens: the last message from API already contain the total
57+
tokens considered, but we save them for cost calculation
58+
- cost: we count the tokens in past requests done + current one"
5559
[{:keys [input-tokens output-tokens
5660
input-cache-creation-tokens input-cache-read-tokens]}
5761
full-model
@@ -64,18 +68,17 @@
6468
(when input-cache-read-tokens
6569
(swap! db* update-in [:chats chat-id :total-input-cache-read-tokens] (fnil + 0) input-cache-read-tokens))
6670
(let [db @db*
67-
message-input-cache-tokens (or input-cache-creation-tokens 0)
6871
total-input-tokens (get-in db [:chats chat-id :total-input-tokens] 0)
6972
total-input-cache-creation-tokens (get-in db [:chats chat-id :total-input-cache-creation-tokens] nil)
7073
total-input-cache-read-tokens (get-in db [:chats chat-id :total-input-cache-read-tokens] nil)
71-
total-input-cache-tokens (or total-input-cache-read-tokens 0)
7274
total-output-tokens (get-in db [:chats chat-id :total-output-tokens] 0)
7375
model-capabilities (get-in db [:models full-model])]
74-
(assoc-some {:message-output-tokens output-tokens
75-
:message-input-tokens (+ input-tokens message-input-cache-tokens)
76-
:session-tokens (+ total-input-tokens total-input-cache-tokens total-output-tokens)}
76+
(assoc-some {:session-tokens (+ input-tokens
77+
(or input-cache-read-tokens 0)
78+
(or input-cache-creation-tokens 0)
79+
output-tokens)}
7780
:limit (:limit model-capabilities)
78-
:message-cost (shared/tokens->cost input-tokens input-cache-creation-tokens input-cache-read-tokens output-tokens model-capabilities)
81+
:last-message-cost (shared/tokens->cost input-tokens input-cache-creation-tokens input-cache-read-tokens output-tokens model-capabilities)
7982
:session-cost (shared/tokens->cost total-input-tokens total-input-cache-creation-tokens total-input-cache-read-tokens total-output-tokens model-capabilities)))))
8083

8184
(defn ^:private tokenize-args [^String s]

0 commit comments

Comments
 (0)