llama_set_abort_callback: Set or clear the abort callback

View source: R/llama.R

llama_set_abort_callbackR Documentation

Set or clear the abort callback

Description

Registers an R function that is called periodically during generation. If the function returns 'TRUE', the current decode operation is aborted. Pass 'NULL' to remove the callback.

Usage

llama_set_abort_callback(ctx, fn)

Arguments

ctx

Context handle returned by [llama_new_context]

fn

A zero-argument R function returning a logical scalar, or 'NULL' to clear.

Details

Note: only one callback is active globally — setting a new one replaces the previous one across all contexts.

Value

No return value, called for side effects.

Examples

## Not run: 
# Abort after 2 seconds
deadline <- Sys.time() + 2
llama_set_abort_callback(ctx, function() Sys.time() > deadline)
result <- llama_generate(ctx, "Tell me a long story", max_new_tokens = 500L)
llama_set_abort_callback(ctx, NULL)

## End(Not run)

llamaR documentation built on May 28, 2026, 1:06 a.m.