Skip to content

Commit 8e99161

Browse files
committed
feat: use real openai data for token usage calculation
1 parent bcf22c9 commit 8e99161

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

src/application/open-ai/open-ai-command.handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export class OpenAICommandHandler {
4040
message: text,
4141
// We rely on the fact that only 1 completion is done, this number could be wrong
4242
// if we used `best_of` and `n` parameters.
43-
tokens: usage.completionTokens,
43+
messageTokens: usage.completionTokens,
44+
totalTokensSpent: usage.totalTokens,
4445
});
4546
} catch (err: any) {
4647
await this.conversationCommandBus.send({

src/config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ export default {
1010
conversation: {
1111
// which persona to use for conversations see `src/domain/persona/index.ts`
1212
personaConfigName: "slack-software-eng",
13-
// ends the conversation when total conversation tokens goes over `maxConversationTokens`
14-
maxConversationTokens: Number.MAX_SAFE_INTEGER,
13+
// ends the conversation when sum of total tokens goes over `maximumSpentTokens`
14+
// all summarization and completion requests are counted towards the total tokens
15+
// persona adds overhead to each request
16+
maximumSpentTokens: Number.MAX_SAFE_INTEGER,
1517
},
1618
};

src/domain/conversation/conversation.aggregate.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class ConversationAggregate {
3737
private status: ConversationStatus;
3838
private readonly messages: ConversationMessage[];
3939
private aiStatus: ConversationAIStatus;
40-
private totalTokens: number;
40+
private totalTokensSpent: number;
4141

4242
private newEvents: ConversationEvent[] = [];
4343

@@ -49,7 +49,7 @@ export class ConversationAggregate {
4949
this.status = { status: "ONGOING" };
5050
this.messages = [];
5151
this.aiStatus = { status: "IDLE" };
52-
this.totalTokens = 0;
52+
this.totalTokensSpent = 0;
5353

5454
if (createEvent) {
5555
this.createAndApply(createEvent);
@@ -121,8 +121,9 @@ export class ConversationAggregate {
121121
message: {
122122
id: cmd.correlationId,
123123
text: cmd.message,
124-
tokens: cmd.tokens,
124+
tokens: cmd.messageTokens,
125125
},
126+
totalTokensSpent: cmd.totalTokensSpent,
126127
});
127128

128129
this.endConversationIfWentOverLimit();
@@ -166,14 +167,14 @@ export class ConversationAggregate {
166167
}
167168

168169
private endConversationIfWentOverLimit(): boolean {
169-
if (this.totalTokens > config.conversation.maxConversationTokens) {
170+
if (this.totalTokensSpent > config.conversation.maximumSpentTokens) {
170171
this.createAndApply({
171172
type: "CONVERSATION_ENDED",
172173
conversationId: this.conversationId,
173174
reason: {
174175
type: "MAXIMUM_CONVERSATION_TOKENS_REACHED",
175-
maxConversationTokens: config.conversation.maxConversationTokens,
176-
totalTokens: this.totalTokens,
176+
maximumSpentTokens: config.conversation.maximumSpentTokens,
177+
totalTokensSpent: this.totalTokensSpent,
177178
},
178179
});
179180

@@ -185,13 +186,12 @@ export class ConversationAggregate {
185186

186187
private addConversationMessage({
187188
message,
188-
tokens,
189-
}: {
189+
}: // tokens,
190+
{
190191
message: ConversationMessage;
191192
tokens: number;
192193
}): void {
193194
this.messages.push(message);
194-
this.totalTokens += tokens;
195195
}
196196

197197
private createAndApply(
@@ -223,6 +223,7 @@ export class ConversationAggregate {
223223
switch (event.type) {
224224
case "BOT_RESPONSE_ADDED": {
225225
this.aiStatus = { status: "IDLE" };
226+
this.totalTokensSpent += event.totalTokensSpent;
226227

227228
return this.addConversationMessage({
228229
message: {

src/domain/conversation/conversation.commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export type ProcessCompletionResponseCommand =
2323
botResponseType: "BOT_RESPONSE_SUCCESS";
2424
correlationId: string;
2525
message: string;
26-
// how many tokens is the `message`
27-
tokens: number;
26+
messageTokens: number;
27+
totalTokensSpent: number;
2828
})
2929
| (BaseCommand & {
3030
type: "PROCESS_COMPLETION_RESPONSE_COMMAND";

src/domain/conversation/conversation.events.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ export type BotResponseAdded = BaseEvent & {
3131
text: string;
3232
tokens: number;
3333
};
34+
totalTokensSpent: number;
3435
};
3536

3637
export type ConversationEnded = BaseEvent & {
3738
type: "CONVERSATION_ENDED";
3839
reason:
3940
| {
4041
type: "MAXIMUM_CONVERSATION_TOKENS_REACHED";
41-
maxConversationTokens: number;
42-
totalTokens: number;
42+
maximumSpentTokens: number;
43+
totalTokensSpent: number;
4344
}
4445
| {
4546
type: "BOT_RESPONSE_ERROR";

0 commit comments

Comments
 (0)