Skip to content

Commit ca40c50

Browse files
committed
Improve usages for contexts limits
1 parent eb6f210 commit ca40c50

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Fix session-tokens in usage notifications.
6+
- Support context limit on usage notifications.
7+
58
## 0.38.3
69

710
- Fix anthropic token renew.

docs/protocol.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,20 @@ interface UsageContent {
543543
* The cost of the whole chat session so far.
544544
*/
545545
sessionCost?: string;
546+
547+
/**
548+
* Informations about limits.
549+
*/
550+
limit: {
551+
/**
552+
* The context limit for this chat.
553+
*/
554+
context: number;
555+
/**
556+
* The output limit for this chat.
557+
*/
558+
output: number;
559+
}
546560
}
547561

548562
/**

src/eca/features/chat.clj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@
6868
total-input-tokens (get-in db [:chats chat-id :total-input-tokens] 0)
6969
total-input-cache-creation-tokens (get-in db [:chats chat-id :total-input-cache-creation-tokens] nil)
7070
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-creation-tokens 0)
72-
total-output-tokens (get-in db [:chats chat-id :total-output-tokens] 0)]
71+
total-input-cache-tokens (+ (or total-input-cache-creation-tokens 0)
72+
(or total-input-cache-read-tokens 0))
73+
total-output-tokens (get-in db [:chats chat-id :total-output-tokens] 0)
74+
model-capabilities (get-in db [:models full-model])]
7375
(assoc-some {:message-output-tokens output-tokens
7476
:message-input-tokens (+ input-tokens message-input-cache-tokens)
7577
:session-tokens (+ total-input-tokens total-input-cache-tokens total-output-tokens)}
76-
:message-cost (shared/tokens->cost input-tokens input-cache-creation-tokens input-cache-read-tokens output-tokens full-model db)
77-
:session-cost (shared/tokens->cost total-input-tokens total-input-cache-creation-tokens total-input-cache-read-tokens total-output-tokens full-model db)))))
78+
:limit (:limit model-capabilities)
79+
:message-cost (shared/tokens->cost input-tokens input-cache-creation-tokens input-cache-read-tokens output-tokens model-capabilities)
80+
:session-cost (shared/tokens->cost total-input-tokens total-input-cache-creation-tokens total-input-cache-read-tokens total-output-tokens model-capabilities)))))
7881

7982
(defn ^:private tokenize-args [^String s]
8083
(if (string/blank? s)

src/eca/features/commands.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,14 @@
183183
total-input-cache-creation-tokens (get-in db [:chats chat-id :total-input-cache-creation-tokens] nil)
184184
total-input-cache-read-tokens (get-in db [:chats chat-id :total-input-cache-read-tokens] nil)
185185
total-output-tokens (get-in db [:chats chat-id :total-output-tokens] 0)
186+
model-capabilities (get-in db [:models full-model])
186187
text (multi-str (str "Total input tokens: " total-input-tokens)
187188
(when total-input-cache-creation-tokens
188189
(str "Total input cache creation tokens: " total-input-cache-creation-tokens))
189190
(when total-input-cache-read-tokens
190191
(str "Total input cache read tokens: " total-input-cache-read-tokens))
191192
(str "Total output tokens: " total-output-tokens)
192-
(str "Total cost: $" (shared/tokens->cost total-input-tokens total-input-cache-creation-tokens total-input-cache-read-tokens total-output-tokens full-model db)))]
193+
(str "Total cost: $" (shared/tokens->cost total-input-tokens total-input-cache-creation-tokens total-input-cache-read-tokens total-output-tokens model-capabilities)))]
193194
{:type :chat-messages
194195
:chats {chat-id [{:role "system" :content [{:type :text :text text}]}]}})
195196
"config" {:type :chat-messages

src/eca/models.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
:web-search (contains? models-with-web-search-support (name model))
4848
:tools (get model-config "tool_call")
4949
:max-output-tokens (get-in model-config ["limit" "output"])}
50+
:limit {:context (get-in model-config ["limit" "context"])
51+
:output (get-in model-config ["limit" "output"])}
5052
:input-token-cost (some-> (get-in model-config ["cost" "input"]) float (/ one-million))
5153
:output-token-cost (some-> (get-in model-config ["cost" "output"]) float (/ one-million))
5254
:input-cache-creation-token-cost (some-> (get-in model-config ["cost" "cache_write"]) float (/ one-million))

src/eca/shared.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@
7373

7474
(defn multi-str [& strings] (string/join "\n" (remove nil? strings)))
7575

76-
(defn tokens->cost [input-tokens input-cache-creation-tokens input-cache-read-tokens output-tokens full-model db]
76+
(defn tokens->cost [input-tokens input-cache-creation-tokens input-cache-read-tokens output-tokens model-capabilities]
7777
(when-let [{:keys [input-token-cost output-token-cost
78-
input-cache-creation-token-cost input-cache-read-token-cost]} (get-in db [:models full-model])]
78+
input-cache-creation-token-cost input-cache-read-token-cost]} model-capabilities]
7979
(when (and input-token-cost output-token-cost)
8080
(let [input-cost (* input-tokens input-token-cost)
8181
input-cost (if (and input-cache-creation-tokens input-cache-creation-token-cost)

0 commit comments

Comments
 (0)