Complete reference for the cl-llama-cpp package. For calling the C API directly,
see Low-level %llama Access.
Every high-level wrapper translates between Lisp NIL and C null pointers
automatically. Pass NIL for optional pointer arguments (e.g. the fn argument to
set-abort-callback, the batch threadpool in attach-threadpool). Receive NIL for
nullable returns (e.g. model-chat-template when no template exists,
backend-dev-by-name when not found). Allocation failures signal a condition rather
than returning NIL. You never need to call cffi:null-pointer or
cffi:null-pointer-p.
Parameters that are semantically boolean in the C API (:embeddings, :vocab-only,
:offload-kqv, :no-perf, etc.) accept both Lisp booleans (T / NIL) and raw C
integers (1 / 0). Integers pass through unchanged; T becomes 1 and NIL
becomes 0. Other types signal a type-error. This applies to all :char-typed
boolean fields in llama_model_params and llama_context_params when passed as
keyword overrides to with-model and with-context.
The high-level API represents foreign resources as typed handle structs rather than
raw C pointers. Use the predicates to test handle variables; use the pointer accessors
when dropping down to %llama: calls.
| Handle type | Predicate | Pointer accessor | Created by |
|---|---|---|---|
llama-model | llama-model-p | llama-model-pointer | with-model, make-model |
llama-context | llama-context-p | llama-context-pointer | with-context, make-context |
llama-sampler | llama-sampler-p | llama-sampler-pointer | with-sampler-chain, make-grammar-sampler, make-*-sampler, etc. |
gguf-context | gguf-context-p | gguf-context-pointer | with-gguf |
ggml-backend-device | ggml-backend-device-p | ggml-backend-device-pointer | backend-dev-get, backend-dev-by-name/type, backend-reg-dev-get |
ggml-backend-registry | ggml-backend-registry-p | ggml-backend-registry-pointer | backend-reg-get, backend-reg-by-name |
Backend device and registry handles are borrowed references — GGML owns the
underlying objects and no free is ever needed.
with-model (var path &rest params) &body body— Load a GGUF model, bind tovaras allama-modelhandle, free on exit (including non-local exit). Keyword params overridellama_model_default_params(e.g.:n-gpu-layers 99). Signalsmodel-load-erroron failure. Delegates tomake-model/free-modelinternally.make-model path &rest params— Standalone model constructor. Same parameters aswith-model. Returns allama-modelhandle with a GC finalizer: if the handle is garbage-collected without being freed, the finalizer callsllama_model_freeas a safety net. The caller owns the handle and should eventually callfree-model. Signalsmodel-load-erroron failure.free-model model— Free the foreign memory held bymodeland cancel its GC finalizer. Idempotent — calling on an already-freed model is a no-op. ReturnsNIL.with-context (var model &rest params) &body body— Create an inference context frommodel, bind tovaras allama-contexthandle, free on exit. Keyword params overridellama_context_default_params(e.g.:n-ctx 2048). Signalscontext-creation-erroron failure. Accepts two extra keywords not in the C params::validation(:off,:warn,:error) and:vram-budget(bytes) run a pre-creation resource check —:warnsignalsconfiguration-unsafe-warningand proceeds,:errorsignalsconfiguration-unsafe-errorand aborts. Without:vram-budget, free VRAM is auto-detected from the GPU. Delegates tomake-context/free-contextinternally.make-context model &rest params— Standalone context constructor. Same parameters aswith-context. Returns allama-contexthandle with a GC finalizer. The caller owns the handle and should eventually callfree-context. Signalscontext-creation-erroron failure.free-context ctx— Free the foreign memory held byctx, cancel its GC finalizer, and clean up any registered abort callback. Idempotent. ReturnsNIL.with-gguf (var path &key no-alloc) &body body— Open a GGUF file atpath, bind tovaras agguf-contexthandle, executebody, free on exit.:no-alloc tskips ggml tensor allocation (faster for metadata-only reads; does not require a loaded backend). Signalsgguf-load-erroron failure. Does not require the llama backend to be initialized.- =with-sampler-chain (var &key temp top-k top-p min-p seed greedy model grammar
grammar-lazy grammar-trigger-words infill typical-p xtc-probability xtc-threshold
top-n-sigma mirostat mirostat-v2 mirostat-tau mirostat-eta repeat-penalty
frequency-penalty presence-penalty penalty-last-n dry-multiplier dry-base
dry-allowed-length dry-penalty-last-n dry-seq-breakers logit-bias dynamic-temp-range
dynamic-temp-exponent adaptive-p adaptive-p-decay sampler-config) &body body= —
Allocate a sampler chain, bind to
varas allama-samplerhandle, free on exit. With no keyword arguments, allocates an empty chain — populate it insidebodyusingsampler-chain-add. With keyword arguments, pre-builds the chain using the same options asgenerate(see Extended Sampler Keywords below). Grammar,:mirostat,:dry-multiplier, and:logit-biasrequire:model.:mirostatand:mirostat-v2are mutually exclusive. Pass:sampler-configto supply defaults; explicit keyword arguments always override the config.
ensure-backend— Ensure the llama backend is initialized. Thread-safe; safe to call from any thread. Establishes a permanent hold: the backend will not be freed when an enclosingwith-backendscope exits. The recommended pattern for multi-threaded use is to callensure-backendonce from the main thread before spawning worker threads. Subsequent calls are no-ops.with-backend (&key numa) &body body— Initialize the llama backend, executebody, then callbackend-freeon exit (including non-local exit). Nesting and concurrent calls from multiple threads are both safe: only the first call to find the backend uninitialized initializes it; the last active scope to exit frees it. Ifensure-backendwas called first,with-backendwill not free the backend on exit. When:numais a GGML NUMA strategy keyword (:distribute,:isolate,:numactl,:mirror), callsllama-numa-initbefore executingbody.set-n-threads ctx n-threads n-threads-batch— Set thread counts onctxfor single-token decoding (n-threads) and batch decoding (n-threads-batch). Both are required in one call. Takes effect on the next decode. Both arguments must be non-negative integers.set-warmup ctx warmup-p— Enable or disable the warmup pass onctx. Disabling after the first decode avoids redundant computation.set-causal-attn ctx causal-attn-p— Enable or disable causal (autoregressive) attention onctx. Disable for bidirectional attention models; takes effect on the next decode.set-embeddings ctx embeddings-p— Set the embedding-output flag onctx. Must match the mode the context was created with: enable only on contexts created with:embeddingsnon-nil, disable only on contexts created without it.synchronize ctx— Block until all pending async compute onctxis complete. High-level accessors (e.g.embed) call this automatically before reading results; use it explicitly in batched pipelines that need finer-grained control.set-abort-callback ctx fn— Register a Lisp function as the abort callback onctx.fnis called with no arguments and should returnTto abort the current decode orNILto continue. PassNILto clear a previously registered callback. The callback runs on a ggml worker thread: lexical variables captured in a closure are visible there, but Lisp dynamic (special) bindings from other threads are not. Errors infnare caught, stored in*last-abort-callback-error*, and generation continues (callback returnsNIL). ReturnsNIL.get-abort-callback ctx— Return the Lisp abort callback registered onctx, orNILif none is set. Thread-safe.*last-abort-callback-error*— The last error condition caught inside the abort-callback panic boundary, orNIL.attach-threadpool ctx threadpool &optional threadpool-batch— Attach an externalggml-threadpooltoctx.threadpool-batchoptionally specifies a separate threadpool for batch operations; when omitted,threadpoolis used for both. The caller retains ownership;detach-threadpooldoes not free the threadpool.detach-threadpool ctx— Detach the threadpool fromctx. Does not free it.
tokenize model text &key add-special parse-special— Tokenizetextinto a vector of token integers. Signalstype-erroriftextis not a string.detokenize model tokens &key remove-special unparse-special— Convert a token vector back to a string. Signalstype-erroriftokensis not a vector.prefill ctx tokens &key (seq-id 0)— Decodetokensinto the KV cache ofctxwithout sampling. Returns the number of tokens decoded as a fixnum. Does not clear the cache first — the caller controls that. Whenseq-idis 0, uses the fastbatch-get-onepath (auto-advancing positions). For non-zeroseq-id, uses the full batch API with explicit position management. Signalsinput-validation-erroriftokensis not a non-empty, non-string vector.- =generate ctx prompt &key max-tokens temp top-k top-p min-p seed parse-special
prompt-tokens token-callback grammar grammar-root typical-p xtc-probability
xtc-threshold top-n-sigma mirostat mirostat-v2 mirostat-tau mirostat-eta
repeat-penalty frequency-penalty presence-penalty penalty-last-n dry-multiplier
dry-base dry-allowed-length dry-penalty-last-n dry-seq-breakers logit-bias
dynamic-temp-range dynamic-temp-exponent adaptive-p adaptive-p-decay sampler
sampler-config reset-context= — Generate text continuing
prompt. Returns three values: the generated string, a stop reason (:eog,:length, or:callback), and a(simple-array fixnum (*))of the sampled token ids.Notable keyword behaviours:
:reset-context(defaultT) — whenNIL, the KV cache is not cleared before decoding; the caller is responsible for ensuring the cache is consistent.:prompt-tokens— use a pre-tokenized token vector instead of tokenizingprompt.:token-callback— called with each decoded token string; returnNILto stop generation early (stop reason:callback).:grammar— constrain output to a GBNF grammar string;:grammar-rootdefaults to"root".:sampler— allama-samplerhandle fromwith-sampler-chain;generateborrows it without freeing. A warning is issued if other sampler keywords are also supplied.:sampler-config— a plist frommake-sampler-config; supplies defaults for any sampler parameter not given explicitly; explicit keywords always take priority.:seed— an integer for deterministic output (default42),:randomfor nondeterministic sampling (the C layer draws a fresh seed internally), ornil(same as:random). When:randomis used, the exact chosen seed is not observable to the caller.
See Extended Sampler Keywords below for the full sampler keyword list.
generate-parallel ctx prompts &key max-tokens temp top-k top-p min-p seed parse-special…all extended sampler keywords… — Generate text for multiplepromptssimultaneously in shared forward passes using the batch API. Each element ofpromptsis a string or a pre-tokenized token vector. All sequences share the same sampler configuration; each gets a unique seed derived fromseed+ sequence-index. When:seedis:randomornil, each sequence gets an independent nondeterministic seed from the C layer instead. The context must be created with:n-seq-max>=(length prompts). Returns two values: a list of generated strings and a list of stop reasons (:eogor:length).embed ctx text &key normalize— Compute embeddings fortext. Returns a vector of single-floats. The context must be created with:embeddings t(or1). Automatically synchronizes pending async compute before reading from the GPU. Signalstype-erroriftextis not a string; signalsinput-validation-erroriftextis empty.
Accepted by generate, generate-parallel, with-sampler-chain, and
build-sampler-chain. Samplers compose in a fixed order: logit bias → penalties →
DRY → grammar → sampling strategy.
Token filtering:
| Keyword | Default | Description |
|---|---|---|
:typical-p | Keep tokens whose information content is near the expected | |
:xtc-probability | Probability of excluding top choices (XTC) | |
:xtc-threshold | 0.5 | Minimum probability for a token to be excludable |
:top-n-sigma | Keep tokens within N standard deviations of the top logit | |
:adaptive-p | Dynamic top-p target probability (experimental) | |
:adaptive-p-decay | 0.0 | Rate at which adaptive-p converges to target |
Repetition control:
| Keyword | Default | Description |
|---|---|---|
:repeat-penalty | Penalise exact token repeats (1.0 = no penalty) | |
:frequency-penalty | Penalty scaling with token frequency (0.0 = off) | |
:presence-penalty | Flat penalty if token appeared at all (0.0 = off) | |
:penalty-last-n | 64 | How many recent tokens to consider for penalties |
:dry-multiplier | DRY sequence-repetition penalty strength (requires :model) | |
:dry-base | 1.75 | DRY exponential base |
:dry-allowed-length | 2 | Minimum sequence length before DRY applies |
:dry-penalty-last-n | -1 | Token window for DRY (-1 = use context size) |
:dry-seq-breakers | List of strings that reset DRY matching (e.g. ("\n" ".")) | |
:logit-bias | Alist of (token-id . bias) pairs (requires :model) |
Sampling strategy:
| Keyword | Default | Description |
|---|---|---|
:mirostat | Enable Mirostat V1 (requires :model; replaces normal path) | |
:mirostat-v2 | Enable Mirostat V2 (replaces normal path) | |
:mirostat-tau | 5.0 | Target surprise level (lower = more focused) |
:mirostat-eta | 0.1 | Learning rate for mirostat adaptation |
:dynamic-temp-range | +/- range for entropy-adaptive temperature | |
:dynamic-temp-exponent | 1.0 | Exponent shaping the dynamic temperature curve |
- =make-sampler-config &key temp top-k top-p min-p seed greedy grammar grammar-root
grammar-lazy grammar-trigger-words grammar-trigger-patterns grammar-trigger-tokens
infill typical-p xtc-probability xtc-threshold top-n-sigma mirostat mirostat-v2
mirostat-tau mirostat-eta repeat-penalty frequency-penalty presence-penalty
penalty-last-n dry-multiplier dry-base dry-allowed-length dry-penalty-last-n
dry-seq-breakers logit-bias dynamic-temp-range dynamic-temp-exponent adaptive-p
adaptive-p-decay= — Bundle sampler parameters into a reusable plist. Returns a
plist containing only the keys explicitly provided — absent parameters carry no
default. Pass the result as
:sampler-configtogenerateorwith-sampler-chain; explicit keyword arguments at the call site always take priority over the config.:seedaccepts integers,:random, ornil— sentinels are stored verbatim and resolved when the config is consumed. Unknown keywords signalPROGRAM-ERROR. Does not accept:model— resource handles have independent lifetimes and belong at the call site. The returned value is a plain plist;getfworks directly on it.
sampler-chain-add chain sampler— Addsamplertochain. Both arguments may be typedllama-samplerhandles or raw CFFI pointers. The chain takes ownership: freeing the chain frees all added samplers. ReturnsNIL.sampler-seed sampler— Return the current RNG seed fromsampleras an integer.resolve-seed seed— Resolve a seed argument: integers pass through,:randomornilmap toLLAMA_DEFAULT_SEED(0xFFFFFFFF, telling the C layer to draw a fresh random seed). Other types signalinput-validation-error. Used internally bygenerateandbuild-sampler-chain; exported for callers building custom chains.free-sampler sampler— Free allama-samplerhandle created by anymake-*-samplerfunction. Do not call on samplers that have been added to a chain — the chain owns and frees those. ReturnsNIL.
Each returns a typed llama-sampler handle. The caller owns it and must either pass
it to sampler-chain-add (transferring ownership to the chain) or free it with
free-sampler. These enable building chains with full control over sampler ordering
without any %llama: calls.
make-greedy-sampler— Always picks the highest-probability token.make-dist-sampler &optional (seed 42)— Random sampling weighted by softmax probabilities.seedmay be an integer,:random, ornil.make-top-k-sampler k— Restrict candidates to thekhighest-probability tokens.make-top-p-sampler p &optional (min-keep 1)— Keep tokens until cumulative probability >=p(nucleus sampling).make-min-p-sampler p &optional (min-keep 1)— Remove tokens with probability <p× max-prob.make-typical-sampler p &optional (min-keep 1)— Locally typical sampling with probability massp.make-temp-sampler temp— Scale logits bytempbefore softmax.make-temp-ext-sampler temp delta &optional (exponent 1.0)— Dynamic temperature: basetemp, rangedelta, curveexponent.make-xtc-sampler probability threshold &optional (min-keep 1) (seed 42)— XTC: trim high-probability tokens abovethresholdwith probabilityprobabilityper step.seedmay be an integer,:random, ornil.make-top-n-sigma-sampler sigma— Keep tokens withinsigmastandard deviations of the max logit.make-mirostat-v2-sampler seed tau eta— Mirostat V2 targeting perplexitytauwith learning rateeta.seedmay be an integer,:random, ornil.
format-chat model messages &key template add-assistant-prefix— Formatmessages(a list of(:role "..." :content "...")plists) into a prompt string using the model’s chat template. Signalsinput-validation-errorifmessagesis empty or any message is missing a:roleor:contentstring. Automatically maps the"assistant"role for models that expect"model"(e.g. Gemma).tokenize-chat model messages &key template add-assistant-prefix— Likeformat-chat, but returns a token vector with template markers parsed as special tokens and message content parsed as regular text. Prevents model-generated text resembling special tokens from corrupting subsequent turns. Use withgenerate’s:prompt-tokensparameter.model-chat-template model &optional name— Return the chat template string embedded in the model, orNILif none is present.list-chat-templates— Return a list of built-in chat template name strings.
A stateful abstraction that tracks exactly which tokens are resident in the KV cache
and decodes only the delta each turn. The session borrows a llama-context — the
caller is responsible for the context’s lifetime.
make-chat-session ctx &key template system-prompt— Create a chat session backed byctx. Clears the KV cache to start clean.:templateoverrides the model’s embedded chat template.:system-prompt, when given, seedsmessageswith an initial(:role "system" :content ...)entry. Signalsinput-validation-errorifctxis not allama-context.chat-session-send session content &rest generate-keys→(values reply stop-reason)— Append a user turn and generate a reply. Only the new tokens since the last turn are decoded; the cached prefix is reused. Additional keyword arguments are forwarded togenerate(e.g.:max-tokens,:temp,:seed,:sampler);:prompt-tokensand:reset-contextare stripped — the session manages those internally. Ondecode-error(context window exhausted), rolls back the KV cache andmessageslist atomically to the pre-call state before re-signaling, so the session remains consistent. Signalsinput-validation-errorifcontentis not a non-empty string.chat-session-reset session &key keep-system— Clear the KV cache and reset the conversation. When:keep-system tand the first message has role"system", that message is retained; the next turn re-seeds the cache from it. ReturnsNIL.chat-session-p session— ReturnTifsessionis achat-session.chat-session-context session— Return thellama-contextbacking the session.chat-session-model session— Return thellama-model(derived from the context).chat-session-messages session— Return the current message list as(:role … :content …)plists.
make-grammar-sampler model grammar &key root— Create a grammar sampler from a GBNFgrammarstring androotrule (default"root"). Returns allama-samplerhandle; caller must either free it withfree-sampleror pass it tosampler-chain-add(which transfers ownership). Signalsgrammar-errorif the grammar is empty or invalid.make-grammar-sampler-lazy model grammar &key root trigger-words— Create a lazy grammar sampler that stays inactive until a trigger word appears in the output.:trigger-wordsis a list of strings (e.g.'("{")). Returns allama-samplerhandle.make-infill-sampler model— Create an infill sampler for fill-in-the-middle generation. Returns allama-samplerhandle.with-grammar-sampler (var model grammar &key root) &body body— Create a grammar sampler, bind tovar, free on exit.
with-lora (var model path) &body body— Load a LoRA adapter GGUF frompathformodel, bind tovar, free on exit. Signalslora-load-erroron failure.apply-lora ctx adapter &key scale— Set the active LoRA adapter onctx.:scale(default 1.0) controls adapter strength. Replaces any previously applied adapter. Signalslora-apply-erroron failure.lora-metadata adapter— Return metadata fromadapteras an alist of(key . value)string pairs.
clear-kv-cache ctx— Clear all KV cache state forctx.kv-cache-seq-rm ctx seq-id p0 p1— Remove cached tokens in positions[p0, p1)forseq-id.-1forp0means from the start;-1forp1means to the end. ReturnsTif cells were removed,NILif no matching data.kv-cache-seq-cp ctx src-seq dst-seq p0 p1— Copy cached data fromsrc-seqtodst-seqfor positions[p0, p1).kv-cache-seq-keep ctx seq-id— Keep onlyseq-id’s cached data, removing all other sequences.kv-cache-seq-add ctx seq-id p0 p1 delta— Shift positions in[p0, p1)forseq-idbydelta.kv-cache-seq-div ctx seq-id p0 p1 d— Divide positions in[p0, p1)forseq-idbyd. Signalsinput-validation-errorifdis zero.kv-cache-pos ctx seq-id— Return the minimum and maximum cached positions forseq-idas(values min max).kv-cache-can-shift-p ctx— ReturnTifctx’s memory supports position shifting,NILotherwise.
Save and restore full context state (including KV cache) to disk. Enables prompt caching across process restarts.
save-session ctx path &optional tokens— Save full context state to a session file atpath.tokensis an optional vector of token integers stored alongside the state. Signalssession-save-erroron failure.load-session ctx path— Load context state from a session file atpath. Returns a vector of cached token integers stored with the state. Signalssession-load-erroron failure.save-session-seq ctx path seq-id &optional tokens— Save a single sequence’s state to a file.load-session-seq ctx path seq-id— Load a single sequence’s state from a file. Returns a vector of cached token integers.
Serialize context state to Lisp octet vectors. Enables in-process snapshotting, context forking, and custom storage backends.
save-state ctx— Serialize full context state to an octet vector.load-state ctx state-bytes— Restore context state from an octet vector. Returns the number of bytes consumed.save-state-seq ctx seq-id &key flags— Serialize one sequence’s state to an octet vector. When:flagsis provided, uses the extended variant withllama_state_seq_flags.load-state-seq ctx seq-id state-bytes &key flags— Restore one sequence’s state from an octet vector. Returns the number of bytes consumed.
For most use cases, generate-parallel is sufficient. These lower-level functions are
available when you need direct control over token-level batching, such as custom
decoding loops or embedding batches.
with-batch (var n-tokens &key n-embd n-seq-max) &body body— Allocate a batch with capacity forn-tokens, bind tovar, free on exit.:n-embdnon-zero allocates embedding slots instead of token slots.:n-seq-maxis the maximum sequence IDs per token (default 1). Signalsbatch-init-errorifn-tokens<= 0 or allocation fails.batch-add-token batch token pos seq-ids &key logits— Add one token tobatchat positionposfor the given sequence(s).seq-idsis an integer or a list of integers.:logits trequests logit computation for this token. Signalsbatch-overflow-errorifbatchis at capacity. Signalsinput-validation-erroriftokenorposis negative.batch-add-embedding batch embedding pos seq-ids &key logits— Add one embedding vector tobatch.embeddingmust match then-embdused when creating the batch. Signalsinput-validation-errorifposis negative orembeddinglength mismatchesn-embd.batch-add-sequence batch tokens seq-id &key start-pos logits— Add a vector oftokenswith sequential positions.:logitscontrols which tokens get logit computation::last(default),:all, ornil. Signalsinput-validation-erroriftokensis empty orstart-posis negative.batch-clear batch— Resetbatch’s active token count to 0. Existing slot contents remain allocated and will be overwritten by subsequent add calls.batch-token-count batch— Return the current number of tokens inbatch.batch-decode ctx batch— Decodebatchusing contextctx. Signalsdecode-erroron failure.batch-encode ctx batch— Encodebatchusing contextctx. Signalsdecode-erroron failure.
model-description model— Return the model’s description string (e.g."gemma3 1B Q4_K - Medium").model-metadata model— Return all GGUF metadata as an alist of(key . value)string pairs.model-info model— Return a plist of model architecture properties::n-params,:n-layers,:n-ctx-train,:size-bytes,:n-heads,:n-heads-kv,:n-embd-in,:n-embd-out,:n-swa,:rope-type,:rope-freq-scale,:n-cls-out,:encoder-p,:decoder-p,:recurrent-p,:hybrid-p,:diffusion-p.model-cls-label model index— Return the classification label string atindex, orNILif unavailable.context-info ctx— Return a plist of context configuration::n-ctx,:n-batch,:n-ubatch,:n-seq-max,:n-threads,:n-threads-batch,:pooling-type.system-info— Return a string describing the llama.cpp build and CPU capabilities.
Inspect GGUF files without loading a model or initializing the backend. The lifecycle
macro is with-gguf — see Resource Management above.
gguf-version gguf— Return the GGUF format version as an integer (typically 3).gguf-alignment gguf— Return the tensor alignment value in bytes.gguf-data-offset gguf— Return the byte offset of the tensor data section.
gguf-n-kv gguf— Return the number of key/value metadata entries.gguf-find-key gguf key— Return the integer index of metadata keykey, orNIL.gguf-key gguf key-id— Return the key name string at indexkey-id.gguf-kv-type gguf key-id— Return the type keyword for entrykey-id::uint8,:int8,:uint16,:int16,:uint32,:int32,:float32,:bool,:string,:array,:uint64,:int64,:float64.
gguf-val gguf key-id— Return the CL value for a scalar KV entry. Dispatches on type: integer types → integers,:float32→single-float,:float64→double-float,:bool→T/NIL,:string→ string. Returns keyword:ARRAYfor array entries; use the array accessors below.gguf-type-name type— Return a human-readable string for a gguf-type keyword.gguf-metadata gguf— Return all KV metadata as an alist of(key . value)pairs. Scalar values are resolved to CL types. Array entries appear as(key . (:array . N))whereNis the element count.
gguf-arr-type gguf key-id— Return the element type keyword for an array KV entry.gguf-arr-n gguf key-id— Return the element count of an array KV entry.gguf-arr-data gguf key-id— Return(values foreign-pointer cffi-element-type count)for a numeric array entry.cffi-element-typeis a CFFI primitive keyword (e.g.:float,:unsigned-int) ready forcffi:mem-aref. Not valid for:stringarrays.gguf-arr-str gguf key-id index— Return the string atindexwithin a string array KV entry.
gguf-n-tensors gguf— Return the number of tensors described in the file.gguf-find-tensor gguf name— Return the integer index of tensorname, orNIL.gguf-tensor-name gguf tensor-id— Return the tensor’s name string.gguf-tensor-type gguf tensor-id— Return the tensor’sggml-typekeyword (e.g.:f32,:q4-0,:bf16).gguf-tensor-offset gguf tensor-id— Return the byte offset of the tensor within the data section.gguf-tensor-size gguf tensor-id— Return the tensor’s byte size.gguf-tensor-info gguf tensor-id— Return a plist(:name … :type … :offset … :size …).gguf-tensors gguf— Return a list ofgguf-tensor-infoplists, one per tensor.
context-perf ctx— Return context performance data as a plist::t-start-ms,:t-load-ms,:t-p-eval-ms,:t-eval-ms,:n-p-eval,:n-eval,:n-reused. Timing values are doubles in milliseconds; counts are integers.print-context-perf ctx— Print context performance statistics to stderr.reset-context-perf ctx— Zero all context performance counters.print-perf ctx— Alias forprint-context-perf.reset-perf ctx— Alias forreset-context-perf.sampler-perf chain— Return sampler performance data as a plist::t-sample-ms,:n-sample.print-sampler-perf chain— Print sampler performance statistics to stderr.reset-sampler-perf chain— Zero all sampler performance counters.with-perf (ctx) &body body— Reset context performance counters, executebody, then print performance to stderr. Printing happens even on non-local exit. Returns the values ofbody.
set-log-callback fn— Route all llama.cpp log messages to Lisp functionfn, called as(fn level text)wherelevelis an integer (1=debug, 2=info, 3=warn, 4=error) andtextis the message string. PassNILto restore the default C stderr logger. Thread-safe: the callback is captured atomically before each dispatch. Errors infnare silently caught to prevent unwinding into C.get-log-callback— Return the current Lisp log callback, orNIL. Thread-safe.
time-us— Return the current wall-clock time in microseconds as an integer.system-capabilities— Return a plist of system capability flags::mmap(boolean),:mlock(boolean),:gpu-offload(boolean),:rpc(boolean),:max-devices(integer),:n-backend-devs(integer),:n-backend-regs(integer),:has-gpu(boolean). The last three reflect the live backend device registry.
Requires the backend to be initialized (call ensure-backend or use with-backend
first). Returned handles are borrowed references — no free is ever needed.
backend-dev-count— Return the total number of registered backend devices.backend-dev-get index— Return theggml-backend-deviceatindex(0-based), orNILif the C pointer is null. Signals an error ifindexis out of range.backend-dev-by-name name— Return theggml-backend-devicenamedname, orNIL.backend-dev-by-type type— Return the firstggml-backend-deviceof the giventypekeyword (:cpu,:gpu,:igpu,:accel,:meta), orNIL.
backend-dev-name device— Return the name string ofdevice.backend-dev-description device— Return the description string ofdevice.backend-dev-type device— Return the type keyword::cpu,:gpu,:igpu,:accel, or:meta.backend-dev-memory device— Return(values free-bytes total-bytes). GPU devices report VRAM; the CPU device typically reports system RAM.backend-dev-props device— Return all device properties as a plist::name,:description,:memory-free,:memory-total,:type,:device-id,:async,:host-buffer,:buffer-from-host-ptr,:events.
backend-reg-count— Return the total number of registered backend registries.backend-reg-get index— Return theggml-backend-registryatindex, orNIL. Signals an error ifindexis out of range.backend-reg-by-name name— Return the registry namedname, orNIL.backend-reg-name reg— Return the name string ofreg.backend-reg-dev-count reg— Return the number of devices belonging toreg.backend-reg-dev-get reg index— Return theggml-backend-deviceatindexwithinreg, orNIL. Signals an error ifindexis out of range.
gpu-devices— Return a list ofbackend-dev-propsplists for all devices of type:gpuor:igpu. Returns()on CPU-only builds.detect-free-vram— Return total free VRAM in bytes across all GPU/IGPU devices, orNILif no GPU devices are registered.detect-total-vram— Return total VRAM capacity in bytes across all GPU/IGPU devices, orNILif no GPU devices are registered.
Estimate memory use from GGUF metadata and validate a configuration against a VRAM
budget before loading a model or creating a context. All sizes are in bytes. Snapshot
detect-free-vram before loading for pre-load planning; after loading, the returned
value reflects remaining headroom only.
estimate-memory model &key n-ctx type-k type-v— Return a plist(:model-size :kv-cache :compute :total)estimating memory formodel.:n-ctxdefaults to the model’s training context length;:type-k/:type-vdefault to:f16(the KV-cache quantization types). The:computeterm is a fixed heuristic, so:totalis an approximation.explain-memory-usage model &key n-ctx type-k type-v stream— Print a human-readable memory breakdown to:stream(default*standard-output*). ReturnsNIL.validate-configuration model &key n-ctx type-k type-v n-gpu-layers vram-budget— Return a plist(:status :safe/:unsafe/:unknown :reason string). Without:vram-budget, free VRAM is auto-detected; status is:unknownonly when no GPU is present. When:n-gpu-layersis supplied, only the GPU share of model weights (plus KV cache and compute) is checked against the budget.feasibility-report model &key n-ctx type-k type-v n-gpu-layers vram-budget stream— Print an estimate + validation report to:streamand return the combined estimate/validation plist. When:vram-budgetis omitted, prints a detected VRAM header.suggest-configuration model &key n-ctx n-gpu-layers vram-budget— Return a plist(:n-ctx n :n-gpu-layers n)that fits within:vram-budget, orNILif none is found. Without:vram-budget, free VRAM is auto-detected; returnsNILon CPU-only builds. Reduces:n-gpu-layersfirst, then halves:n-ctx.
Runtime guardrails: with-context accepts :validation (:off, :warn, :error)
and :vram-budget. :warn signals configuration-unsafe-warning (a warning) and
proceeds; :error signals configuration-unsafe-error and aborts context creation.
All conditions inherit from llama-error.
| Condition | Slot(s) | When |
|---|---|---|
model-load-error | path | Model file cannot be loaded |
gguf-load-error | path | GGUF file cannot be opened (with-gguf) |
context-creation-error | Context allocation fails | |
tokenization-error | text | Tokenization fails |
decode-error | code | llama_decode returns non-zero |
chat-template-error | Chat template rendering fails | |
lora-load-error | path | LoRA adapter file cannot be loaded |
lora-apply-error | code | set-adapters-lora returns non-zero |
session-save-error | path | Session file save fails |
session-load-error | path | Session file load fails |
grammar-error | grammar | Empty or invalid GBNF grammar string |
batch-init-error | n-tokens | Batch allocation fails |
batch-overflow-error | capacity, token-count | Batch add exceeds capacity |
configuration-unsafe-error | reason | with-context :validation :error and config exceeds VRAM budget |
input-validation-error | function-name, argument, value, reason | Pre-flight check fails (negative index, empty input, etc.) |
configuration-unsafe-warning (signaled by with-context :validation :warn) is a
warning, not a llama-error; read its reason with
configuration-unsafe-warning-reason.
input-validation-error carries four slots: input-validation-error-function (which
wrapper signaled), input-validation-error-argument (which parameter),
input-validation-error-value (the rejected value), input-validation-error-reason
(human-readable explanation). Simple type mismatches signal standard type-error via
check-type instead.
Every resource-loading condition establishes domain-specific restarts for the
interactive debugger. Restart names are intentionally unexported; for programmatic use
from handler-bind, reference them as cl-llama-cpp::restart-name.
| Condition | Restart | Description |
|---|---|---|
model-load-error | retry-with-layers (n) | Retry loading with a new :n-gpu-layers value |
use-cpu-only () | Retry with :n-gpu-layers 0 | |
use-different-path (new-path) | Retry loading from a different file path | |
context-creation-error | retry-with-smaller-ctx (n) | Retry context creation with a smaller :n-ctx value |
retry-with-params (plist) | Retry with a completely new params plist | |
lora-load-error | use-different-path (new-path) | Retry loading the LoRA adapter from a different path |
skip-lora () | Continue without a LoRA adapter (body receives NIL) | |
lora-apply-error | use-different-scale (s) | Retry applying the adapter at a different scale |
skip-apply () | Continue without applying the LoRA adapter | |
session-save-error | use-different-path (new-path) | Retry saving to a different path |
skip-save () | Continue without saving (returns NIL) | |
session-load-error | use-different-path (new-path) | Retry loading from a different path |
start-fresh () | Skip loading; return an empty token vector | |
grammar-error | skip-grammar () | Continue without grammar constraint (returns NIL) |
use-different-grammar (g) | Retry with a different GBNF grammar string | |
chat-template-error | use-default-template () | Retry format-chat using the model’s built-in template |
Notes:
- Restarts that read user input do so from
*query-io*, so they work in the interactive debugger and via(invoke-restart-interactively ...). save-session-seqandload-session-seqexpose the same restarts as their non-seq counterparts.make-grammar-sampler-lazyandmake-infill-sampleralso establish the grammar restarts (skip-grammar/use-different-grammar).with-loraandwith-grammar-samplerguard their cleanup so askip-*restart that returnsNILdoes not attempt to free a null pointer.