LLMs  

How to Build Local-First AI Applications Using ONNX Runtime and .NET

Introduction

Artificial Intelligence applications are often associated with cloud services and large language models running on remote servers. While cloud-based AI offers powerful capabilities, it also introduces challenges such as latency, internet dependency, privacy concerns, and ongoing API costs.

This is why many developers are adopting a local-first AI approach. Instead of sending data to external services, AI models run directly on the user's machine or within an organization's infrastructure.

One of the most effective ways to build local AI solutions in the .NET ecosystem is by using ONNX Runtime. It enables developers to run machine learning models efficiently across different platforms without requiring cloud connectivity.

In this article, you'll learn what ONNX Runtime is, why local-first AI matters, and how to build AI-powered applications using ONNX Runtime and .NET.

What Is ONNX Runtime?

ONNX Runtime is a high-performance inference engine for executing machine learning models in the Open Neural Network Exchange (ONNX) format.

The ONNX format provides a standardized way to represent machine learning models so they can run across different frameworks and platforms.

With ONNX Runtime, developers can execute models created using:

  • PyTorch

  • TensorFlow

  • Scikit-learn

  • XGBoost

  • Keras

  • Other machine learning frameworks

This allows machine learning engineers and .NET developers to collaborate using a common model format.

Understanding Local-First AI

A local-first AI application performs inference directly on the device where the application is running.

Instead of:

Application → Cloud API → AI Model → Response

The workflow becomes:

Application → Local AI Model → Response

This approach eliminates network dependencies and improves responsiveness.

Benefits of Local-First AI

Local AI provides several advantages.

Improved Privacy

Sensitive data never leaves the user's device.

This is particularly important for:

  • Healthcare applications

  • Financial systems

  • Enterprise software

  • Internal business tools

Reduced Latency

Responses are generated locally without network delays.

Lower Operational Costs

There are no recurring API charges for every request.

Offline Availability

Applications continue functioning even without internet access.

Better Control

Organizations maintain full control over models and data.

Why Use ONNX Runtime with .NET?

ONNX Runtime integrates well with modern .NET applications.

Benefits include:

  • Cross-platform support

  • High-performance inference

  • Hardware acceleration

  • Easy deployment

  • Minimal dependencies

  • Support for multiple AI model types

It works with:

  • ASP.NET Core

  • Blazor

  • WPF

  • Windows Forms

  • Console Applications

  • MAUI Applications

This flexibility makes it a strong choice for AI-powered .NET solutions.

Installing ONNX Runtime

Create a new console application:

dotnet new console

Install the ONNX Runtime package:

dotnet add package Microsoft.ML.OnnxRuntime

Once installed, your application is ready to load and execute ONNX models.

Understanding the Project Structure

A typical project may look like:

MyAIApp/
│
├── Models/
│   └── sentiment-model.onnx
│
├── Program.cs
│
└── MyAIApp.csproj

The ONNX model is stored locally and loaded during application startup.

Loading an ONNX Model

The first step is creating an inference session.

using Microsoft.ML.OnnxRuntime;

var session = new InferenceSession(
    "Models/sentiment-model.onnx");

The session loads the model into memory and prepares it for inference.

Running Model Inference

Once the model is loaded, input data can be passed to the runtime.

Example:

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;

var inputTensor = new DenseTensor<float>(
    new float[] { 0.5f, 0.2f, 0.8f },
    new[] { 1, 3 });

var inputs = new List<NamedOnnxValue>
{
    NamedOnnxValue.CreateFromTensor(
        "input",
        inputTensor)
};

using var results = session.Run(inputs);

foreach (var result in results)
{
    Console.WriteLine(result.Name);
}

This code sends data to the model and retrieves predictions.

Real-World Example: Sentiment Analysis

Suppose you're building a customer feedback application.

Users submit reviews such as:

The product quality is excellent and delivery was fast.

The ONNX model analyzes the text and returns:

Positive Sentiment

Similarly:

The application crashes frequently and feels slow.

May return:

Negative Sentiment

Because inference runs locally, user reviews never leave the device.

Building an ASP.NET Core AI API

Local AI models can also power web APIs.

Example:

app.MapPost("/predict", (PredictionRequest request) =>
{
    var prediction = RunModel(request.Text);

    return Results.Ok(new
    {
        Result = prediction
    });
});

Clients can submit requests and receive AI-generated predictions without relying on external AI services.

This approach is useful for enterprise environments that require strict data privacy.

Hardware Acceleration

ONNX Runtime supports hardware acceleration to improve performance.

Depending on the environment, inference can run on:

  • CPU

  • NVIDIA GPUs

  • AMD GPUs

  • DirectML

  • Specialized AI hardware

Benefits include:

  • Faster predictions

  • Lower CPU usage

  • Better scalability

For compute-intensive models, hardware acceleration can significantly improve response times.

Common Local-First AI Use Cases

ONNX Runtime can support many practical applications.

Document Classification

Automatically categorize documents locally.

Sentiment Analysis

Analyze customer feedback without external APIs.

Image Recognition

Process images directly on the device.

Recommendation Systems

Generate personalized recommendations.

Fraud Detection

Evaluate transactions locally before submission.

Predictive Analytics

Provide intelligent forecasts within business applications.

Best Practices

When building local-first AI applications, follow these recommendations.

Choose the Right Model Size

Smaller models generally provide faster inference and lower memory consumption.

Optimize Models

Use optimized ONNX models whenever possible.

Monitor Resource Usage

Track memory and CPU consumption during inference.

Secure Model Files

Protect model files from unauthorized access or modification.

Test Across Devices

Performance may vary depending on hardware capabilities.

Cache Model Sessions

Reuse inference sessions instead of repeatedly loading models.

Example:

private static readonly InferenceSession Session =
    new("Models/model.onnx");

This improves application performance.

Challenges to Consider

Although local AI offers many benefits, developers should understand its limitations.

Hardware Constraints

Some devices may not have sufficient resources for large models.

Model Updates

Updating deployed models requires a distribution strategy.

Storage Requirements

Large AI models can increase application size.

Accuracy Trade-Offs

Smaller local models may not always match the capabilities of large cloud-hosted models.

Balancing performance, size, and accuracy is an important architectural decision.

Conclusion

Local-first AI is becoming an increasingly important approach for developers who need privacy, low latency, offline functionality, and predictable operational costs. By combining ONNX Runtime with .NET, developers can build powerful AI applications that execute directly on user devices without relying on cloud-based inference services.

Whether you're creating sentiment analysis tools, document processing systems, recommendation engines, or intelligent enterprise applications, ONNX Runtime provides a flexible and high-performance foundation for local AI development. As organizations continue prioritizing privacy and efficiency, local-first AI solutions built with ONNX Runtime and .NET will become an increasingly valuable part of modern software architecture.