@elizaos/core v0.1.7 / trimTokens
trimTokens(
context
,maxTokens
,runtime
):Promise
<string
>
Trims the provided text context to a specified token limit using a tokenizer model and type.
The function dynamically determines the truncation method based on the tokenizer settings provided by the runtime. If no tokenizer settings are defined, it defaults to using the TikToken truncation method with the "gpt-4o" model.
• context: string
The text to be tokenized and trimmed.
• maxTokens: number
The maximum number of tokens allowed after truncation.
• runtime: IAgentRuntime
The runtime interface providing tokenizer settings.
Promise
<string
>
A promise that resolves to the trimmed text.
trimTokens
Throws an error if the runtime settings are invalid or missing required fields.
const trimmedText = await trimTokens("This is an example text", 50, runtime);
console.log(trimmedText); // Output will be a truncated version of the input text.