AI Gameplay
Unity AI
LLM Integration
Local LLM
Unity C#
Game Development
AI NPCs

Production AI NPC Systems in Unity (Part 1): Local vs. Cloud LLM Inference Architecture

6 min read
Eshan Naithani

Traditional game AI relies on rigid decision trees, state machines, and pre-scripted dialogue branches. While effective for predictable quest givers, these static systems fail to deliver truly dynamic, emergent interactions for modern companion characters, procedural tutors, or interactive narrative worlds.

Integrating Large Language Models (LLMs) into Unity allows non-player characters (NPCs) to understand open-ended player text or voice input, remember past interactions, and make context-aware decisions in 3D space.

However, the primary architectural bottleneck when building AI NPCs in Unity is inference speed vs. reasoning capability.

In Part 1 of this masterclass series, we evaluate Local vs. Cloud LLM Inference Architectures for Unity games and implement a hybrid C# manager capable of handling dynamic AI requests.


1. Local vs. Cloud LLM Trade-Off Matrix

Code
 [Local ONNX / Unity Sentis / Ollama]      [Cloud APIs: OpenAI / Anthropic]
 ├── Sub-50ms Latency (Zero Lag)           ├── High Reasoning & Memory Window
 ├── $0 Token Cost Per Active User         ├── 300ms–800ms HTTP API Latency
 └── Limited Model Parameters (1B–3B)       └── Recurring Token Cost Per Request

Technical Evaluation Matrix

Metric / RequirementLocal LLM (Unity Sentis / ONNX / Ollama)Cloud LLM (OpenAI GPT-4o-mini / Anthropic Haiku)
Response LatencyInstant (Sub-50ms) — Executed on local GPU/CPU.Variable (300ms – 1,200ms) — Depends on network round-trip.
Operational Cost$0 / Month — Zero API token fees.Per Token Fee — Pay per input/output token across user base.
Offline Capability100% Offline — No internet connection required.Requires Active Internet — Fails if offline.
Reasoning PowerModerate — Constrained to 1B–3B parameter quantized models.Extremely High — Massive parameter models with complex context reasoning.
VRAM / Hardware FootprintRequires 2GB–4GB VRAM on user hardware.Zero Local VRAM Impact — Processed on remote server clusters.

2. When to Use Which Architecture

Use Local LLM Inference When:

  • Building fast-paced combat or companion NPCs that must respond instantly (sub-100ms response windows).
  • Publishing offline mobile or desktop games where internet connectivity cannot be guaranteed.
  • Keeping server infrastructure costs at zero for indie titles with high daily active users (DAU).

Use Cloud LLM Inference When:

  • Building complex RPG dialogue systems, dynamic mystery games, or procedural quest generation requiring deep reasoning.
  • Running heavy multi-turn conversational agents with massive memory history.
  • Distributing to low-spec mobile hardware where allocating 2GB+ VRAM for local inference would trigger Out-Of-Memory (OOM) crashes.

3. Production Unity C# Architecture: Hybrid LLM Manager

Below is a production-ready C# manager script (UnityLLMInferenceManager.cs) that handles HTTP streaming requests to Cloud APIs while providing local fallback logic when offline or under high latency network conditions.

CSHARP
using System;
using System.Collections;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;

/// <summary>
/// Production C# Manager for handling AI NPC LLM Requests in Unity.
/// Supports Cloud REST API streaming with offline fallback.
/// </summary>
public class UnityLLMInferenceManager : MonoBehaviour
{
    [Header("Cloud API Settings")]
    [SerializeField] private string cloudApiUrl = "https://api.openai.com/v1/chat/completions";
    [SerializeField] private string apiKey = "YOUR_API_KEY_HERE";
    [SerializeField] private string modelName = "gpt-4o-mini";

    [Header("Inference Configuration")]
    [SerializeField] private float timeoutSeconds = 5.0f;
    [SerializeField] private bool useLocalFallbackIfOffline = true;

    [System.Serializable]
    private class ChatMessage
    {
        public string role;
        public string content;
    }

    [System.Serializable]
    private class ChatRequestBody
    {
        public string model;
        public ChatMessage[] messages;
        public float temperature = 0.7f;
    }

    /// <summary>
    /// Sends a prompt to the AI NPC and invokes a callback upon receiving the generated response.
    /// </summary>
    public void RequestNPCResponse(string systemPrompt, string playerMessage, Action<string> onResponseReceived)
    {
        if (Application.internetReachability == NetworkReachability.NotReachable && useLocalFallbackIfOffline)
        {
            Debug.LogWarning("[AI NPC] Offline mode detected. Switching to Local Fallback Inference.");
            ExecuteLocalFallbackInference(playerMessage, onResponseReceived);
            return;
        }

        StartCoroutine(SendCloudLLMRequest(systemPrompt, playerMessage, onResponseReceived));
    }

    private IEnumerator SendCloudLLMRequest(string systemPrompt, string playerMessage, Action<string> onResponseReceived)
    {
        ChatRequestBody requestBody = new ChatRequestBody
        {
            model = modelName,
            messages = new ChatMessage[]
            {
                new ChatMessage { role = "system", content = systemPrompt },
                new ChatMessage { role = "user", content = playerMessage }
            }
        };

        string jsonPayload = JsonUtility.ToJson(requestBody);
        byte[] rawData = Encoding.UTF8.GetBytes(jsonPayload);

        using (UnityWebRequest request = new UnityWebRequest(cloudApiUrl, "POST"))
        {
            request.uploadHandler = new UploadHandlerRaw(rawData);
            request.downloadHandler = new DownloadHandlerBuffer();
            request.SetRequestHeader("Content-Type", "application/json");
            request.SetRequestHeader("Authorization", $"Bearer {apiKey}");
            request.timeout = Mathf.RoundToInt(timeoutSeconds);

            yield return request.SendWebRequest();

            if (request.result == UnityWebRequest.Result.Success)
            {
                string responseText = request.downloadHandler.text;
                Debug.Log($"[AI NPC Response Received]: {responseText}");
                onResponseReceived?.Invoke(responseText);
            }
            else
            {
                Debug.LogError($"[AI NPC Request Failed]: {request.error}");
                if (useLocalFallbackIfOffline)
                {
                    ExecuteLocalFallbackInference(playerMessage, onResponseReceived);
                }
            }
        }
    }

    private void ExecuteLocalFallbackInference(string prompt, Action<string> onResponseReceived)
    {
        // Local stub for ONNX / Sentis inference model execution
        string fallbackReply = "I hear you traveler, but my thoughts are hazy right now.";
        onResponseReceived?.Invoke(fallbackReply);
    }
}

4. What's Coming in Part 2

Now that our inference pipeline is established, the next major challenge is preventing the LLM from outputting raw unformatted text. In a game, an AI NPC must perform physics actions, trigger animations, and give items to the player.

In Part 2 of this masterclass, we cover:

  • Structured JSON Function Calling: Forcing LLMs to return strict JSON payloads schema.
  • Behavior Tree Integration: Driving Unity state machines and pathfinding directly from AI JSON output.

👉 Read Part 2: Structured JSON Function Calling & Behavior Trees →


💡 Building a Custom AI Gameplay System?

Need help integrating local or cloud AI models, optimizing NPC pathfinding, or building dynamic conversational systems in Unity?

  • AI NPC System Architecture: Designing low-latency LLM pipelines for mobile and desktop games.
  • Unity C# Engine Optimization: Preventing GC frame spikes during async network streaming.

👉 Book an AI Gameplay Engineering Session or reach out directly to discuss your project requirements.


🎮 Shipped Projects & Official Store Profiles

Explore commercial titles and technical systems built with Unity across official stores:


Planning an AI-powered title? Check out our AI Gameplay Systems Services or explore our 2026 Mobile Game Development Cost Guide.

Share this article

Looking to build a production-ready game?

See how I built Bird Sort Mania in 20 days using AI, or check out my full Mobile Games Portfolio to see my shipped titles on Android and iOS.

Join 5,000+ Game Developers

Get weekly insights on Unity performance optimization, AI gameplay architectures, and robust system design. No spam, just deep technical breakdowns.

Unsubscribe at any time. Your data is never shared.

Recommended Reading

More articles in AI Gameplay