Alpha software: this package is in early development. APIs may change between versions.
macOS only: Apple Silicon and Intel Macs are supported. Windows and Linux are not currently supported.
A llama.cpp provider for the Vercel AI SDK. It runs local GGUF models inside Node.js through native C++ bindings, without a separate inference server.
The provider implements AI SDK language and embedding model interfaces for local agents, including tool calling, structured output support, image inputs for multimodal models, reasoning extraction, and embeddings.
- macOS on Apple Silicon or Intel.
- Node.js >= 18.
- CMake >= 3.15.
- Xcode Command Line Tools.
xcode-select --install
brew install cmakenpm install @lgrammel/llama-cpp-providerInstallation downloads the pinned llama.cpp revision, builds it with Metal support, and compiles the native Node.js addon.
Download a GGUF model locally, pass its path to llamaCpp, and use it with ToolLoopAgent. For interactive chat examples, use @lgrammel/agent-tui.
import { runAgentTUI } from "@lgrammel/agent-tui";
import { ToolLoopAgent } from "ai";
import { llamaCpp } from "@lgrammel/llama-cpp-provider";
const model = llamaCpp({
modelPath: "./models/llama-3.2-1b-instruct.Q4_K_M.gguf",
});
const agent = new ToolLoopAgent({
model,
instructions: "You are a concise local assistant.",
});
try {
await runAgentTUI({ name: "Local assistant", agent });
} finally {
await model.dispose();
}Always call dispose() when you are done with a model so native CPU/GPU resources are released.
Creates an AI SDK language model for ToolLoopAgent and other AI SDK consumers.
import { ToolLoopAgent } from "ai";
import { llamaCpp } from "@lgrammel/llama-cpp-provider";
const model = llamaCpp({
modelPath: "./models/your-model.gguf",
mmprojPath: "./models/mmproj.gguf",
contextSize: 4096,
gpuLayers: 99,
threads: 8,
debug: false,
logPrompts: false,
model: {
chatTemplate: "auto",
reasoning: {},
},
});
const agent = new ToolLoopAgent({ model });Important options:
modelPathis required and must point to a local GGUF model file.mmprojPathpoints to a multimodal projector GGUF file and is required for image inputs.contextSizedefaults to2048. Higher values can use significant memory.gpuLayersdefaults to99, which offloads all available layers to GPU. Use0to disable GPU offload.threadsdefaults to4.debugenables verbose llama.cpp output.logPromptsprints the final chat-template-rendered prompt sent to llama.cpp to stderr. It can include private user data and is intended for local debugging only.model.chatTemplatedefaults to"auto", which uses the template embedded in the GGUF file. You can also pass a llama.cpp template name such as"llama3","chatml", or"gemma".model.reasoningextracts thinking text into AI SDK reasoning parts.memorySafetycan reject or clamp context sizes that are estimated to exceed available memory when model memory metadata is provided.
Standard AI SDK generation settings are supported by the language model, including maxOutputTokens, temperature, topP, topK, and stopSequences.
Creates an AI SDK embedding model for embed and embedMany. It uses the same base loading options as llamaCpp(config).
- macOS only.
- ESM only.
- Model and projector paths must point to local GGUF files.
- Image inputs require a compatible multimodal model and
mmprojPath.
See CONTRIBUTING.md for development setup and contribution guidelines.
MIT