Introduction
Large Language Models (LLMs) have become a core component of modern applications, powering chatbots, code assistants, document analysis tools, and intelligent search systems. While many organizations use cloud-hosted AI services, there is growing interest in running AI models locally.
Local inference offers several advantages, including reduced latency, improved privacy, lower operational costs, and the ability to operate without an internet connection. For .NET developers, ONNX Runtime provides a powerful and efficient way to run machine learning and AI models directly within applications.
By combining ONNX Runtime with locally deployed language models, developers can build AI-powered solutions that maintain full control over data and infrastructure.
In this article, you'll learn how ONNX Runtime works, how to integrate it into .NET applications, and best practices for implementing local LLM inference.
What Is ONNX Runtime?
ONNX Runtime is a high-performance inference engine for machine learning models in the Open Neural Network Exchange (ONNX) format.
It enables developers to:
Run AI models locally
Optimize inference performance
Support multiple hardware platforms
Leverage CPU and GPU acceleration
Deploy AI applications across different environments
Instead of calling an external AI API, applications can execute models directly on local hardware.
Typical workflow:
User Input
|
v
.NET Application
|
v
ONNX Runtime
|
v
Local LLM
|
v
Generated Output
This architecture eliminates dependency on external AI services.
Why Run LLMs Locally?
Many organizations are adopting local inference for practical reasons.
Improved Privacy
Sensitive information remains within organizational infrastructure.
Examples include:
Customer data
Financial records
Legal documents
Internal communications
Reduced Latency
Local processing removes network round trips.
Benefits include:
Faster responses
Improved user experience
Reduced dependency on internet connectivity
Cost Control
Cloud AI services often charge based on usage.
Local inference can reduce recurring costs for:
High-volume applications
Internal tools
Knowledge assistants
Offline Operation
Applications continue functioning even when external services are unavailable.
This is valuable for edge computing and disconnected environments.
Understanding ONNX Models
ONNX is a standard format for representing machine learning models.
Many AI frameworks support exporting models to ONNX, including:
PyTorch
TensorFlow
Hugging Face Transformers
Scikit-learn
An exported ONNX model can then be executed using ONNX Runtime.
Example:
Model.onnx
This file contains the trained model used during inference.
Setting Up ONNX Runtime in .NET
Install the ONNX Runtime package:
dotnet add package Microsoft.ML.OnnxRuntime
This package provides the APIs required to load and execute ONNX models.
Loading an ONNX Model
Create an inference session.
using Microsoft.ML.OnnxRuntime;
var session =
new InferenceSession(
"Model.onnx");
The session manages model execution and resource allocation.
Typically, applications create the session once and reuse it throughout the application lifecycle.
Creating Input Data
Before running inference, input data must be prepared in a format expected by the model.
Example:
var inputTensor =
new DenseTensor<float>(
new[] { 1.0f, 2.0f, 3.0f },
new[] { 1, 3 });
The exact tensor structure depends on the model architecture.
For language models, tokenized text is typically converted into tensors before inference.
Running Inference
Execute the model using ONNX Runtime.
var inputs =
new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor(
"input",
inputTensor)
};
using var results =
session.Run(inputs);

Join the conversation! Your thoughts help the community grow.