TensorStack.LlamaCpp 0.15.0

Prefix Reserved
dotnet add package TensorStack.LlamaCpp --version 0.15.0
                    
NuGet\Install-Package TensorStack.LlamaCpp -Version 0.15.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TensorStack.LlamaCpp" Version="0.15.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TensorStack.LlamaCpp" Version="0.15.0" />
                    
Directory.Packages.props
<PackageReference Include="TensorStack.LlamaCpp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TensorStack.LlamaCpp --version 0.15.0
                    
#r "nuget: TensorStack.LlamaCpp, 0.15.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TensorStack.LlamaCpp@0.15.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TensorStack.LlamaCpp&version=0.15.0
                    
Install as a Cake Addin
#tool nuget:?package=TensorStack.LlamaCpp&version=0.15.0
                    
Install as a Cake Tool

TensorStack.LlamaCpp

Basic .NET Wrapper API for llama.cpp

Nuget Package

LlamaCpp Badge

Runs GGUF language models (Qwen 3.x, Gemma 4) with streaming, thinking, tool calls and image/audio input. Thinking and tool calls are returned in the same normalized form for every model: <think>...</think> and <tool_call>...</tool_call>.

Chat Example

Generate text using Qwen 3.5, tokens are reported to the progress callback as they are generated

using (var backend = new LlamaCppBackend(VendorType.Nvidia))
{
    await backend.InitializeAsync();

    var progress = new Progress<PipelineProgress>(x => Console.Write(x.Message));
    using (var pipeline = new LlamaCppPipeline(backend, progress))
    {
        await pipeline.LoadAsync(new LlamaCppOptions
        {
            ModelPath = "models/Qwen3.5-9B-GGUF/Qwen3.5-9B-Q8_0.gguf",
            Template = PromptTemplate.Qwen35
        });

        TextInput[] result = await pipeline.GenerateTextAsync(new GenerateTextOptions
        {
            IsThinkingEnabled = true,
            Conversation =
            [
                new ConversationMessage(ConversationRole.System, "You are a helpful assistant."),
                new ConversationMessage(ConversationRole.User, "What is the capital of France?")
            ]
        });
    }
}

Tool Call Example

Tools are OpenAI style function schemas. When the model calls a tool the result contains a <tool_call> block, add the call and its result to the conversation and generate again

using (var backend = new LlamaCppBackend(VendorType.Nvidia))
{
    await backend.InitializeAsync();

    using (var pipeline = new LlamaCppPipeline(backend))
    {
        await pipeline.LoadAsync(new LlamaCppOptions
        {
            ModelPath = "models/Qwen3.5-9B-GGUF/Qwen3.5-9B-Q8_0.gguf",
            Template = PromptTemplate.Qwen35
        });

        var options = new GenerateTextOptions
        {
            Tools = [File.ReadAllText("calculator.json")],
            Conversation = [new ConversationMessage(ConversationRole.User, "Multiply 12.5 by 34")]
        };
        TextInput[] result = await pipeline.GenerateTextAsync(options);

        // The model asked for the tool, run it and return the result
        options.Conversation.Add(new ConversationMessage(ConversationRole.Assistant, "", ToolCalls: ["""{"name":"calculator","arguments":{"op":"multiply","a":12.5,"b":34}}"""]));
        options.Conversation.Add(new ConversationMessage(ConversationRole.Tool, "425"));
        result = await pipeline.GenerateTextAsync(options);
    }
}

Vision Example

Describe an image using Gemma 4, image and audio input needs the multimodal projector (mmproj) of the model

using (var backend = new LlamaCppBackend(VendorType.Nvidia))
{
    await backend.InitializeAsync();

    using (var pipeline = new LlamaCppPipeline(backend))
    {
        await pipeline.LoadAsync(new LlamaCppOptions
        {
            ModelPath = "models/Gemma-4-E4B-IT-GGUF/gemma-4-E4B-it-Q8_0.gguf",
            ProjectorPath = "models/Gemma-4-E4B-IT-GGUF/mmproj-gemma-4-E4B-it-Q8_0.gguf",
            Template = PromptTemplate.Gemma4
        });

        TextInput[] result = await pipeline.GenerateTextAsync(new GenerateTextOptions
        {
            InputImages = [image],
            Conversation = [new ConversationMessage(ConversationRole.User, "Describe this image.", ImageIndex: [0])]
        });
    }
}

Options

LlamaCppOptions are set when the model is loaded.

Option Description
Template The prompt format of the model family: Qwen30, Qwen35, Qwen36, Qwen38, Gemma4
ProjectorPath The multimodal projector (mmproj GGUF), required for image and audio input
Device / DeviceName The GPU to use, by index or by name
GpuLayers Layers to offload to the GPU, -1 for all, 0 for CPU only
ContextSize The context size in tokens, the conversation and the generated text must fit in it, GenerateTextOptions.ContextSize changes it per request without reloading the model
CacheType The key/value cache storage type, Q8_0 and Q4_0 use less memory

The conversation is cached between calls, so a follow up message only evaluates the new part of the conversation. Models with recurrent state (Qwen 3.5 and later) can not rewind their cache and evaluate the whole conversation each call.

Product Compatible and additional computed target framework versions.
.NET net10.0-windows10.0.17763 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.15.0 80 11/18/2026