LlamaEngine
llama.cpp text-generation engine: streaming generation, KV-cache sessions, grammar / JSON-schema constrained decoding and embeddings.
Create instances with the LlamaEngine factory function. Engines are ordinary objects — never singletons — and any number of them (with the same or different models and backends) can coexist and run concurrently: each owns its native model, KV-cache context and a dedicated single-threaded dispatcher (SerialExecutor) that serializes loading, every inference call and the native release in close.
Session. The engine keeps one conversation in its KV cache: the tokens of the last prompt and of everything generated after it. A new generate reuses the longest common token prefix with that session (GenerationOptions.reusePromptCache), so continuing a conversation only evaluates the new text. saveSession / restoreSession serialize that KV cache to a ByteArray and back, on this or another engine with the same model.
Images. With a projector (LlamaEngineOptions.projectorPath) prompts may carry in-memory LlamaImages (vision-language models such as LLaVA, Gemma 3, Qwen-VL or SmolVLM): see generate with images.
Cancellation is cooperative and prompt: cancelling the collector of generate sets the native abort flag that llama.cpp polls inside every ggml graph computation (llama_set_abort_callback), so a running decode stops within one graph step. The engine stays usable afterwards.
Types
Constants and factory functions of LlamaEngine.
Properties
The backend llama.cpp actually runs on (never ComputeBackend.AUTO).
The GPU llama.cpp offloads to, or null on the CPU (see InferenceEngine.activeDeviceName).
The backend configuration this engine was loaded with.
Why activeBackend is not the requested backend, or null (see InferenceEngine.backendFallbackReason).
The KV-cache configuration this engine was created with.
Effective context window in tokens (llama.cpp may round LlamaContextConfig.contextLength up).
true while a generation or embedding is executing native code on this engine.
Static properties of the loaded model.
LLM-specific construction options.
Number of tokens (KV-cache positions, including image embeddings) currently held in the session.
true if a projector (LlamaEngineOptions.projectorPath) with vision input is loaded: images are accepted.
true if at least one GPU device was handed to llama.cpp; always false for ComputeBackend.CPU_ONLY.
Functions
Requests the native abort of the generation or embedding currently executing, if any; a generate flow then ends with a terminal TokenEvent.Error (InferenceCancelledException). Safe from any thread. Queued calls are not affected.
Empties the session (KV cache and token history).
Closes the engine: aborts a running generation, rejects queued and future calls with IllegalStateException, frees the sampler, contexts and model on the engine thread, and stops that thread. Idempotent and safe from any thread.
closes the engine and suspends until its native memory has been freed on the engine thread (after a running call has been aborted and left native code). Idempotent; cancelling the caller stops the wait, not the release. See InferenceEngine.closeAndAwait.
Concatenates the pieces of tokens and decodes them as UTF-8.
Computes the embedding of text on a dedicated embedding context (created on first use, separate from the generation session, so embeddings never disturb it). The vector is pooled with LlamaContextConfig.embeddingPooling (for causal LMs usually the last token's hidden state).
Streams the continuation of prompt as a cold Flow of TokenEvents. Each collection runs one generation on the engine thread (collections and other calls on the same engine are serialized).
Streams the continuation of a multimodal prompt with in-memory images, like generate without images. Requires a projector (LlamaEngineOptions.projectorPath, supportsVision); without one the flow reports InvalidInputException.
Runs generate to completion and returns the generated text.
Replaces the session with one saved by saveSession. A following generate whose prompt starts with the restored tokens only evaluates the remainder.
Serializes the session (token history + KV cache of the conversation) after all queued work.