{"id":61276,"date":"2026-11-23T10:05:00","date_gmt":"2026-11-23T18:05:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=61276"},"modified":"2026-11-23T10:05:00","modified_gmt":"2026-11-23T18:05:00","slug":"local-models-dotnet-llama-windows","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/local-models-dotnet-llama-windows\/","title":{"rendered":"Run local models in your .NET apps with Llama on Windows"},"content":{"rendered":"<p>Running AI models locally lets you keep your prompts and responses on your PC, work offline after downloading a model, and experiment without paying per request. Getting started can take some setup: finding a model that fits your hardware, downloading it, and running a server your apps can call.<\/p>\n<p>Llama on Windows takes care of that for you. It&#8217;s a tray app that helps you find, download, and chat with open models on your PC. Once a model is running, your .NET apps can use it too.<\/p>\n<p>In this post, you&#8217;ll install Llama, download a model, and call it from a .NET 11 C# file-based app.<\/p>\n<p>You can find the complete code in the <a href=\"https:\/\/github.com\/luisquintanilla\/llama-windows-dotnet\">Llama on Windows .NET sample repository<\/a>.<\/p>\n<h2>What is Llama on Windows?<\/h2>\n<p>Llama is a Windows 11 app that lives in your system tray. It&#8217;s built with .NET 10 and WinUI 3, it&#8217;s powered by llama.cpp, and it&#8217;s brought to you by the Hugging Face and llama.cpp teams. From the tray, you can:<\/p>\n<ul>\n<li>Browse models recommended for your hardware and download them in one click.<\/li>\n<li>Search the Hugging Face Hub for thousands of other models.<\/li>\n<li>Chat with a model in the browser, or press <code>Alt+Space<\/code> from any app to open a chat overlay.<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2026\/11\/llama-windows-tray.webp\" alt=\"Llama tray flyout showing Gemma 4 running locally and other model families available to browse\" \/><\/p>\n<p>Under the hood, Llama manages a llama.cpp server for you. Models run on your PC and are stored in the standard Hugging Face cache, so other tools like the <code>llama<\/code> command line can use them too.<\/p>\n<p>Llama is open source and free to download at <a href=\"https:\/\/llama.app\/\">llama.app<\/a>. You can find the source code in the <a href=\"https:\/\/github.com\/ggml-org\/Llama-Windows\">Llama-Windows repository on GitHub<\/a>.<\/p>\n<h2>Install Llama<\/h2>\n<p>Llama is available in the <a href=\"https:\/\/apps.microsoft.com\/detail\/9PPPSM6Q21Q6\">Microsoft Store<\/a>, which means you can install it with winget from a terminal:<\/p>\n<pre><code class=\"language-powershell\">winget install 9PPPSM6Q21Q6 --source msstore<\/code><\/pre>\n<p>You can also get Llama from <a href=\"https:\/\/llama.app\/\">llama.app<\/a>.<\/p>\n<p>Once it&#8217;s installed, open <strong>Llama<\/strong> from the Start menu. The Llama icon appears in your system tray.<\/p>\n<h2>Download a model and start chatting<\/h2>\n<ol>\n<li>Select the Llama icon in the system tray.<\/li>\n<li>Pick a model family from the catalog, then choose a size and version that fit your PC. Llama shows a hardware-fit indicator next to each version. For this walkthrough, I&#8217;m using <strong>Gemma 4<\/strong>, <strong>E4B<\/strong>, <strong>Balanced (Q4_0)<\/strong>.<\/li>\n<li>Select the download arrow. The download can be several GB, depending on the model you choose.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2026\/11\/llama-windows-model-download.webp\" alt=\"Gemma 4 model page showing E4B selected and the Q8_0 and Q4_0 download options\" \/><\/p>\n<p>When the download completes, select the model to see its details. Choose a context length, using the memory-fit indicators as a guide, then select <strong>Chat with model<\/strong>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2026\/11\/llama-windows-model-details.webp\" alt=\"Downloaded Gemma 4 showing context-length choices and the Chat with model action\" \/><\/p>\n<p>Try asking a question in the browser chat. For example, ask for ideas for a small .NET app you could build with a local model.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2026\/11\/llama-windows-local-chat.webp\" alt=\"Llama browser chat with Gemma 4 suggesting a local CLI chat, a document summarizer, and a code explainer\" \/><\/p>\n<p>That&#8217;s it! You&#8217;re chatting with a model running on your PC.<\/p>\n<p>Llama loads models when you use them and unloads them after they&#8217;ve been idle for five minutes, so you don&#8217;t have to manage memory yourself. You can change the idle timeout in Llama&#8217;s settings.<\/p>\n<h2>Use Llama from your .NET apps<\/h2>\n<p>Chatting in the tray is a great way to try out models. When you&#8217;re ready to build something with them, you can call the same models from your code.<\/p>\n<p>Llama runs a llama.cpp server on <code>http:\/\/localhost:9931<\/code>. The server provides endpoints that follow the OpenAI API format, such as <code>\/v1\/chat\/completions<\/code> and <code>\/v1\/models<\/code>. That means you can use the OpenAI client libraries you already know. Your requests go to the server running on your machine, not to OpenAI, and you don&#8217;t need an API key.<\/p>\n<p>In .NET, you can use the <a href=\"https:\/\/www.nuget.org\/packages\/OpenAI\">OpenAI<\/a> library together with <a href=\"https:\/\/learn.microsoft.com\/dotnet\/ai\/microsoft-extensions-ai\">Microsoft.Extensions.AI<\/a> to work with Llama through <code>IChatClient<\/code>. This shared interface lets you build chat, streaming, and tool calling into your app without coupling that code to a specific model provider.<\/p>\n<h3>What you&#8217;ll need<\/h3>\n<ul>\n<li><a href=\"https:\/\/llama.app\/\">Llama<\/a>, running, with at least one model downloaded<\/li>\n<li><a href=\"https:\/\/dotnet.microsoft.com\/download\/dotnet\/11.0\">.NET 11 SDK<\/a><\/li>\n<\/ul>\n<p>You can install the .NET 11 SDK with winget as well:<\/p>\n<pre><code class=\"language-powershell\">winget install Microsoft.DotNet.SDK.11<\/code><\/pre>\n<h3>Create the app<\/h3>\n<p>The <a href=\"https:\/\/github.com\/luisquintanilla\/llama-windows-dotnet\">sample repository<\/a> includes the complete app. With file-based apps, you don&#8217;t need a project file. Create a file called <em>llama-chat.cs<\/em> and add the following code:<\/p>\n<pre><code class=\"language-csharp\">#!\/usr\/bin\/env dotnet\n#:package Microsoft.Extensions.AI@11.0.0\n#:package Microsoft.Extensions.AI.OpenAI@11.0.0\n\nusing System.ClientModel;\nusing System.ComponentModel;\nusing System.Runtime.InteropServices;\nusing System.Text.Json;\nusing Microsoft.Extensions.AI;\nusing OpenAI;\n\n\/\/ Llama serves an OpenAI-compatible API on localhost. Set LLAMA_ENDPOINT if you changed the port.\nvar endpoint = Environment.GetEnvironmentVariable(\"LLAMA_ENDPOINT\") ?? \"http:\/\/localhost:9931\/v1\";\nusing var http = new HttpClient();\n\n\/\/ Ask Llama which models it can serve. This also tells us whether it's running.\nList&lt;string&gt; models;\ntry\n{\n    models = [.. (await GetLlamaModelsAsync()).Select(m =&gt; m.GetProperty(\"id\").GetString()!)];\n}\ncatch (HttpRequestException)\n{\n    Console.WriteLine($\"Couldn't reach Llama at {endpoint}. Open Llama from the Start menu and try again.\");\n    return 1;\n}\n\nif (models.Count == 0)\n{\n    Console.WriteLine(\"Llama doesn't have any models yet. Open Llama from the system tray and download a model.\");\n    return 1;\n}\n\nConsole.WriteLine(\"Models available in Llama:\");\nmodels.ForEach(m =&gt; Console.WriteLine($\"  {m}\"));\n\n\/\/ Use the model passed as an argument, or the first one Llama reports.\nvar model = args.Length &gt; 0 ? args[0] : models[0];\nConsole.WriteLine($\"\\nChatting with {model}. Press Enter on an empty line to exit.\");\n\n\/\/ No API key is required by default, but the OpenAI client expects a value.\nIChatClient client = new OpenAIClient(new ApiKeyCredential(\"no-key-required\"), new OpenAIClientOptions { Endpoint = new Uri(endpoint) })\n    .GetChatClient(model)\n    .AsIChatClient()\n    .AsBuilder()\n    .UseFunctionInvocation()\n    .Build();\n\nvar options = new ChatOptions\n{\n    Tools = [AIFunctionFactory.Create(GetMachineInfo), AIFunctionFactory.Create(ListModels)]\n};\n\nList&lt;ChatMessage&gt; history = [new(ChatRole.System, \"You are a helpful assistant running locally on the user's PC. Keep answers short.\")];\n\nwhile (true)\n{\n    Console.Write(\"\\nYou: \");\n    var prompt = Console.ReadLine();\n    if (string.IsNullOrWhiteSpace(prompt))\n    {\n        break;\n    }\n\n    history.Add(new(ChatRole.User, prompt));\n\n    Console.Write(\"Llama: \");\n    List&lt;ChatResponseUpdate&gt; updates = [];\n    await foreach (var update in client.GetStreamingResponseAsync(history, options))\n    {\n        Console.Write(update.Text);\n        updates.Add(update);\n    }\n    Console.WriteLine();\n\n    history.AddMessages(updates);\n}\n\nreturn 0;\n\n\/\/ Calls GET \/v1\/models. Llama adds details such as load status to the standard OpenAI response.\nasync Task&lt;List&lt;JsonElement&gt;&gt; GetLlamaModelsAsync()\n{\n    using var json = JsonDocument.Parse(await http.GetStringAsync($\"{endpoint}\/models\"));\n    return [.. json.RootElement.GetProperty(\"data\").EnumerateArray().Select(m =&gt; m.Clone())];\n}\n\n[Description(\"Lists the models downloaded in Llama on this PC, whether each one is loaded in memory, and which one the user is chatting with.\")]\nasync Task&lt;string&gt; ListModels()\n{\n    var lines = (await GetLlamaModelsAsync()).Select(m =&gt;\n    {\n        var id = m.GetProperty(\"id\").GetString();\n        var status = m.TryGetProperty(\"status\", out var s) ? s.GetProperty(\"value\").GetString() : \"unknown\";\n        return $\"{id} ({status}){(id == model ? \", current chat\" : \"\")}\";\n    });\n    return string.Join(Environment.NewLine, lines);\n}\n\n[Description(\"Gets the name, operating system, processor count, and .NET version of the computer the user is chatting from.\")]\nstatic string GetMachineInfo() =&gt;\n    $\"{Environment.MachineName}, {RuntimeInformation.OSDescription}, {Environment.ProcessorCount} logical processors, .NET {Environment.Version}\";<\/code><\/pre>\n<h3>Run the app<\/h3>\n<p>Make sure Llama is running, then run the app:<\/p>\n<pre><code class=\"language-powershell\">dotnet run llama-chat.cs<\/code><\/pre>\n<p>The app lists the models Llama has and starts a chat with the first one. To pick a different model, pass its ID as an argument. You can copy a model&#8217;s ID from its details in the Llama tray.<\/p>\n<pre><code class=\"language-powershell\">dotnet run llama-chat.cs -- ggml-org\/gemma-4-E4B-it-GGUF:Q4_0<\/code><\/pre>\n<p>You don&#8217;t need to load the model first. Llama loads it on the first request, so that first response might take a little longer.<\/p>\n<p>The app gives the model two tools: <code>GetMachineInfo<\/code> and <code>ListModels<\/code>. It can call these tools to answer questions about your computer and downloaded models, or answer general questions without a tool. Here&#8217;s an excerpt from my run, with the computer name replaced by <code>MYPC<\/code>:<\/p>\n<pre><code class=\"language-text\">You: Which models do I have, and which one am I talking to? Use your tool.\nLlama: You have the following model: `ggml-org\/gemma-4-E4B-it-GGUF:Q4_0`.\n\nYou are currently chatting with **gemma-4-E4B-it**.\n\nYou: What computer am I on? Use your tool.\nLlama: You are on a **MYPC** running **Microsoft Windows 10.0.26200** with **16 logical processors** and **.NET 11.0.0**.\n\nYou: Why is the sky blue?\nLlama: The sky appears blue due to a phenomenon called **Rayleigh scattering**.<\/code><\/pre>\n<p>Tool calling depends on the model you choose. If a model ignores the tools, try a larger model or one that lists tool support on its Hugging Face model card.<\/p>\n<h3>How it works<\/h3>\n<p>Let&#8217;s walk through the main parts of the app.<\/p>\n<p><strong>Find the models.<\/strong> The app calls <code>GET \/v1\/models<\/code> to list the models Llama can serve. If the request fails, the app asks you to check that Llama is running. Llama adds a few details to the standard response, such as whether each model is loaded in memory.<\/p>\n<p><strong>Create the chat client.<\/strong> The app creates an <code>OpenAIClient<\/code> that points at Llama&#8217;s endpoint instead of OpenAI. <code>AsIChatClient()<\/code> turns it into an <code>IChatClient<\/code>, and <code>UseFunctionInvocation()<\/code> adds automatic tool calling.<\/p>\n<p><strong>Add tools.<\/strong> <code>AIFunctionFactory.Create<\/code> turns <code>GetMachineInfo<\/code> and <code>ListModels<\/code> into tools the model can call, and uses each method&#8217;s <code>Description<\/code> attribute to tell the model what the tool does. When the model asks to call a tool, <code>Microsoft.Extensions.AI<\/code> runs the method and sends the result back to the model for you. Both tools run on your PC, so the model can answer questions about your machine without anything leaving it.<\/p>\n<p><strong>Chat.<\/strong> <code>GetStreamingResponseAsync<\/code> streams the response so you see text as the model generates it. The app adds each response to the chat history so the model remembers the conversation.<\/p>\n<p>Because the chat loop only depends on <code>IChatClient<\/code>, it stays the same if you use a different model or provider later.<\/p>\n<h2>Watch the announcement<\/h2>\n<p>Watch the announcement of Llama on Windows in the .NET Conf 2026 keynote.<\/p>\n<p><iframe loading=\"lazy\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/SIBdHRrk8os?start=3207\" allowfullscreen><\/iframe><\/p>\n<h2>Next steps<\/h2>\n<p>We&#8217;re excited to see what you build with local models and .NET. Here&#8217;s where to go next:<\/p>\n<ul>\n<li>Install <a href=\"https:\/\/llama.app\/\">Llama<\/a> and download your first model.<\/li>\n<li><a href=\"https:\/\/github.com\/luisquintanilla\/llama-windows-dotnet\">Try the .NET sample<\/a>: run <code>dotnet run llama-chat.cs<\/code> to chat with your local model and try the tools.<\/li>\n<li>Read the <a href=\"https:\/\/llama.app\/docs\/api\">Llama API documentation<\/a> to learn about other endpoints, such as embeddings and structured output.<\/li>\n<li>Explore the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/ai\/microsoft-extensions-ai\">Microsoft.Extensions.AI documentation<\/a> to learn more about <code>IChatClient<\/code>.<\/li>\n<li>Take it further by combining Llama with cloud models using <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/routing-and-failover-for-microsoft-extensions-ai\/\">Routing and Failover for Microsoft.Extensions.AI<\/a>.<\/li>\n<\/ul>\n<p>Thanks to the Hugging Face and llama.cpp teams for building Llama on Windows. For issues or feature requests for Llama itself, open an issue in the <a href=\"https:\/\/github.com\/ggml-org\/Llama-Windows\/issues\">Llama-Windows repository<\/a>. Share what you&#8217;re building with local models and .NET in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Install Llama on Windows from Hugging Face and the llama.cpp team, download an open model, and call it from a .NET 11 C# file-based app.<\/p>\n","protected":false},"author":26108,"featured_media":61277,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685,7781],"tags":[568,8244,8243,8241,8242,8051,7885],"class_list":["post-61276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","category-ai","tag-ai","tag-file-based-apps","tag-hugging-face","tag-llama","tag-llama-cpp","tag-local-models","tag-microsoft-extensions-ai"],"acf":[],"blog_post_summary":"<p>Install Llama on Windows from Hugging Face and the llama.cpp team, download an open model, and call it from a .NET 11 C# file-based app.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/61276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/26108"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=61276"}],"version-history":[{"count":2,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/61276\/revisions"}],"predecessor-version":[{"id":61319,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/61276\/revisions\/61319"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/61277"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=61276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=61276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=61276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}