Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix: free tiktoken encoder to prevent OOM issues (#897)

Max Deichmann6d4040d5ed1a
diff --git a/src/features/ingest/lib/usage.ts b/src/features/ingest/lib/usage.ts
index 8921750a4e..ace1a0b2e6 100644
--- a/src/features/ingest/lib/usage.ts
+++ b/src/features/ingest/lib/usage.ts
@@ -108,6 +108,9 @@ function openAiChatTokenCount(params: TokenCalculationParams) {
});
});
num_tokens += 3; // every reply is primed with <| start |> assistant <| message |>
+ // https://github.com/dqbd/tiktoken/issues/72
+ // we need to ensure to deallocate memory from the encoder
+ encoding.free();
return num_tokens;
}
@@ -132,6 +135,8 @@ const claudeStringTokenCount = (p: { model: string; text: string }) => {
const getTokens = (name: TiktokenEncoding, text: string) => {
const encoding = get_encoding(name);
const tokens = encoding.encode(text);
+ // https://github.com/dqbd/tiktoken/issues/72
+ // we need to ensure to deallocate memory from the encoder
encoding.free();
return tokens.length;
};