Guaranteed 15% off your current AI inference bill for team spending up to $20000 / month.

Book a call →
Back to Blogs
Engineering

Building Gaming Tools with LLM: A Step-by-Step Guide

Today we are building an NPC dialogue generator for RPGs that reacts to faction allegiance, player reputation, and quest state. It is a tool you can drop into...

Building Gaming Tools with LLM: A Step-by-Step Guide

Today we are building an NPC dialogue generator for RPGs that reacts to faction allegiance, player reputation, and quest state. It is a tool you can drop into a Unity or Godot pipeline to generate contextual in-game dialogue without maintaining massive branching script files. I use it to prototype quest lines in an afternoon instead of a week.

What you'll need

You will need Python 3.10 or newer, the OpenAI SDK, and an API key from https://portal.oxlo.ai. Oxlo.ai uses flat per-request pricing, so generating a hundred lines of NPC dialogue costs the same whether your context window is tiny or packed with lore. That matters when you are feeding entire quest journals into the prompt. Install the SDK with pip.

pip install openai

Step 1: Connect to Oxlo.ai

First, initialize the client. Oxlo.ai is fully OpenAI SDK compatible, so the only difference is the base URL and your Oxlo.ai API key. I will use Llama 3.3 70B as the workhorse model because it handles creative writing and instruction following reliably at a flat per-request rate. I keep the client global so I do not pay a cold-start penalty. Oxlo.ai serves popular models with no cold starts, which keeps iteration loops fast when I am tweaking prompts.

from openai import OpenAI

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

SYSTEM_PROMPT = "You are an NPC dialogue generator for a dark-fantasy RPG."
user_message = "Generate a greeting for a grumpy blacksmith."

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": user_message},
    ],
)

print(response.choices[0].message.content)

Step 2: Write the system prompt

The system prompt is the contract. It tells the model who the NPC is, what world they inhabit, and what variables to respect. Keeping this in a separate string makes it easy to swap personas without touching business logic. I keep one master prompt and inject character sheets at runtime.

SYSTEM_PROMPT = """You are an NPC dialogue generator for a dark-fantasy RPG.
Rules:
- Respond in character as the assigned NPC.
- Factor in the provided faction, mood, and player_reputation.
- Keep lines to 1-3 sentences so they fit UI text boxes.
- Never break the fourth wall or mention you are an AI.

Output only the spoken dialogue. No stage directions."""

Step 3: Build the generator function

Now we wrap the raw call with a function that accepts game state. This keeps our game loop clean. We format the user message as a JSON-like string so the model can parse the variables clearly. I have found that explicit key-value pairs reduce hallucinated context. Using json.dumps keeps special characters escaped. I learned that lesson after an apostrophe in an NPC name broke a manual template. I also cap max_tokens at 150 so the model does not ramble past UI limits.

import json

def generate_line(npc_name, faction, mood, player_reputation, prompt_context):
    user_message = json.dumps({
        "npc": npc_name,
        "faction": faction,
        "mood": mood,
        "player_reputation": player_reputation,
        "context": prompt_context
    }, indent=2)

    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": user_message},
        ],
        temperature=0.8,
        max_tokens=150,
    )
    return response.choices[0].message.content.strip()

Step 4: Add structured quest metadata

Raw text is fine for prototyping, but our game engine needs flags. Does this line advance a quest? What emotion should the voice actor use? We switch to JSON mode and DeepSeek V3.2 because it follows structured output instructions precisely. Oxlo.ai supports JSON mode on compatible models, so we just add response_format. The model now returns predictable keys our C# or GDScript parser can consume without regex. If the model returns malformed JSON, Oxlo.ai's flat pricing means I can retry the same request instantly without worrying about token costs doubling on the second attempt.

def generate_structured_line(npc_name, faction, mood, player_reputation, context):
    user_message = json.dumps({
        "npc": npc_name,
        "faction": faction,
        "mood": mood,
        "player_reputation": player_reputation,
        "context": context
    }, indent=2)

    structured_system = SYSTEM_PROMPT + "\nRespond with valid JSON containing keys: dialogue (string), emotion (string), advances_quest (boolean)."

    response = client.chat.completions.create(
        model="deepseek-v3.2",
        messages=[
            {"role": "system", "content": structured_system},
            {"role": "user", "content": user_message},
        ],
        response_format={"type": "json_object"},
        temperature=0.7,
        max_tokens=200,
    )
    return json.loads(response.choices[0].message.content)

Step 5: Batch generate a dialogue tree

Most NPCs need more than one line. We will generate a greeting, a quest hint, and a farewell in a single script. Because Oxlo.ai charges per request, not per token, sending three separate prompts to build a full node costs the same whether each prompt is ten words or a thousand words of lore. That makes iterative tree generation affordable even when you are iterating on a 32k context world bible. I loop over context strings and collect structured nodes.

def build_dialogue_tree(npc_name, faction, reputation, quest_stage):
    nodes = []
    contexts = [
        f"{npc_name} greets the player. Quest stage: {quest_stage}",
        f"{npc_name} gives a subtle hint about the next objective. Quest stage: {quest_stage}",
        f"{npc_name} says goodbye after the conversation."
    ]

    for ctx in contexts:
        line = generate_structured_line(
            npc_name=npc_name,
            faction=faction,
            mood="wary",
            player_reputation=reputation,
            context=ctx
        )
        nodes.append(line)

    return nodes

# Example usage
tree = build_dialogue_tree(
    npc_name="Garrett",
    faction="Iron-Thieves",
    reputation=-2,
    quest_stage="seeking_the_forgery"
)

for node in tree:
    print(node)

Run it

Running the script produces structured output you can feed directly into a game engine or a localization pipeline. Here is what the console looks like when I execute the tree generator. Notice how the hostile reputation changes the greeting, the quest stage surfaces the correct hint, and the emotion tags give our audio pipeline clear direction. You can swap in Qwen 3 32B if you need multilingual output for international releases, or Kimi K2.6 for longer narrative reasoning.

$ python npc_generator.py

{'dialogue': 'You have a lot of nerve showing your face here after what happened at the docks.', 'emotion': 'hostile', 'advances_quest': False}
{'dialogue': 'If you are looking for the forger, ask about the red ledger in the lower district. Just do not mention my name.', 'emotion': 'whispered', 'advances_quest': True}
{'dialogue': 'We are done. Do not make me regret letting you walk out.', 'emotion': 'dismissive', 'advances_quest': False}

Next steps

Wire this generator into your game build pipeline so NPCs are authored as JSON files at compile time, or expose it as a live service endpoint so player actions dynamically alter dialogue. If you want spoken lines, pipe the dialogue text through Oxlo.ai's audio endpoints with Kokoro 82M for text-to-speech or Whisper for transcription. You could also cache common lines locally and only call the API for procedurally generated responses.

Ready to build with Oxlo.ai?

Get started building high-performance AI inference applications today.

Get started
Ox Assistant
Online
OxBot
OxBot

Hi there! Try our cost calculator to see what you'd save with Oxlo.ai.