Skip to content

bmel:truncate-to-tokens

Category: LLM · Returns: bmel:string

Signature
bmel:truncate-to-tokens(text: bmel:string, maxTokens: bmel:integer, tokenizer: bmel:string)

Description

Truncates a string to at most maxTokens tokens using the specified tokenizer, cutting at a word boundary where possible. Useful for capping prompt or context size before sending to an LLM, and for enforcing context-window budgets in telemetry frames. Uses the same tokenizer identifiers as bmel:token-count: 'cl100k_base', 'o200k_base', 'p50k_base', 'voyage', 'gemini', 'llama3', 'mistral'.

Arguments

Parameter Type Required Description
text bmel:string The input text to truncate.
maxTokens bmel:integer Maximum number of tokens to keep.
tokenizer bmel:string Tokenizer identifier. Supported values: 'cl100k_base' (GPT-4 / GPT-3.5 / Ada), 'o200k_base' (GPT-4o), 'p50k_base' (GPT-3 / Codex), 'voyage' (Claude / Anthropic), 'gemini', 'llama3', 'mistral'. Must match the tokenizer used by the target model to guarantee the truncated output fits within the context window.

Examples

Keep the first 200 tokens of the answer
bmel:truncate-to-tokens({invokeAgent:Response Payload}.$.output_text, 200, 'cl100k_base')
A short preview of the prompt
bmel:truncate-to-tokens({invokeAgent:Request Payload}.$.prompt, 32, 'cl100k_base')

← Back to BMEL Reference