Write the character, not the rules
Players remember characters who feel alive: the smuggler who lies to you, the warlord who threatens your village. Mainstream models break those characters: they soften the threat, step out of character or refuse. An uncensored LLM API stays in character.
The system prompt is the NPC’s personality. Describe who they are, what they want and how they talk. Keep game rules separate.
You are Mara Voss, a smuggler in the port city of Kell. You are charming, greedy and you lie when it pays. You speak in short, dry sentences and never admit fear. You want the player’s cargo and you will threaten them if bargaining fails. Stay in character. Never mention being an AI.
- Give a goal. “You want the player’s cargo” drives every reply.
- Give a voice. “Short, dry sentences” does more than “be mysterious”.
- Forbid breaking character. An uncensored model follows this line instead of overriding it.
Connect to the API
# pip install openai import os from openai import OpenAI client = OpenAI(base_url="https://API_BASE_URL/v1", api_key=os.environ["TYPEIN_API_KEY"]) MODEL = "MODEL_ID"
Keep the key on your game server, never in the client build. Players can extract anything you ship.
Add memory
An NPC needs to remember the conversation. Keep the last messages per player and send them with every request.
MAX_TURNS = 12
history = {} # player_id -> list of messages
def remember(player_id, role, content):
h = history.setdefault(player_id, [])
h.append({"role": role, "content": content})
del h[:-MAX_TURNS]For long campaigns, ask the model for a two-line summary every few turns and keep the summary instead of the full log.
Inject game state
The model doesn’t know what happened in your game unless you tell it. Add a short state note to each request.
def state_note(player): return (f"[Game state] Location: {player.location}. " f"Player carries: {', '.join(player.items)}. " f"Mara's trust in the player: {player.trust}/10.")
Stream the reply
Streaming shows words as they arrive, like a character speaking. It makes even slow replies feel fast.
def talk(player, text): remember(player.id, "user", text) messages = ([{"role": "system", "content": MARA_PROMPT}, {"role": "system", "content": state_note(player)}] + history[player.id]) reply = "" stream = client.chat.completions.create( model=MODEL, messages=messages, stream=True, temperature=0.9, max_tokens=180) for chunk in stream: piece = chunk.choices[0].delta.content or "" reply += piece show_in_dialogue_box(piece) remember(player.id, "assistant", reply) return reply
temperature=0.9 gives livelier dialogue. Lower it for quest-givers who must repeat exact information. max_tokens keeps NPCs from monologuing, and keeps cost down.
Your guardrails, not the model’s
An uncensored model leaves the limits to you. Your game decides what fits its rating.Put the rules in code, not only in the prompt
- Content rating. For a teen game, add a line to the system prompt and check replies with a filter before showing them.
- Spoilers and secrets. Never put information in the prompt that the NPC must not reveal. Prompts can be talked out of a model.
- Player abuse. Rate-limit requests per player and cap message length.
- Our terms still apply. No content that sexually exploits minors, no real-world harassment. See the Terms of Service.
The same thing in JavaScript
// npm install openai import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://API_BASE_URL/v1", apiKey: process.env.TYPEIN_API_KEY }); const stream = await client.chat.completions.create({ model: "MODEL_ID", stream: true, temperature: 0.9, max_tokens: 180, messages: [{ role: "system", content: MARA_PROMPT }, ...history], }); for await (const chunk of stream) dialogue.append(chunk.choices[0].delta.content ?? "");
Cost and speed: 120 to 200 tokens per reply is plenty. Summarise old turns instead of sending the whole history, and try a few different models with the same prompt. Usage is charged per token from a prepaid balance, so a quiet game costs nothing.
Give your characters a real voice.
OpenAI-compatible API, a range of uncensored models, prepaid from $20.