Interactive fiction engine:
long stories with models that stay in the scene
Build an interactive fiction engine on fiction-tuned and uncensored models: long context for story state, a thinking switch and sampling you control.
Last updated 2026-11-05
Black Friday deposit bonus
Every qualifying top-up earns free credit, on top of any other offer.
Ends November 30
An interactive fiction engine asks more of a model than a chatbot does. It has to keep a world straight over hundreds of turns, write prose that doesn't repeat itself, follow the player into a dark corner of the story without breaking off, and hand control back with a choice. On Lovely Router you build one on the OpenAI or Anthropic API with a model chosen for the job: mainstream models for general storytelling, and uncensored models that write villains, violence and mature scenes as written, including dongfangshuo-27b-v2, tuned for Chinese long-form fiction and roleplay. Your engine keeps the story state and sends it with every turn; the models take 128K tokens of context or more, and you set the sampling and whether the model thinks before it writes.
Hand it to your AI agent
Copy this prompt into Claude Code, Cursor, Codex or any coding agent. It tells the agent where the docs are and how to use your API key.
Build an interactive fiction engine that keeps story state and calls fiction-tuned or uncensored chat models with chosen thinking and sampling settings. - Docs (read first): https://lovelyrouter.com/docs/models/hosted.md - POST https://lovelyrouter.com/v1/chat/completions; the hosted models take `top_k`, `min_p`, `repeat_penalty` and `chat_template_kwargs.enable_thinking`. The first call after an idle spell can wait while the model wakes up: use a timeout of at least 3 minutes. - Auth: read my API key from the LOVELY_API_KEY environment variable and send it as `Authorization: Bearer $LOVELY_API_KEY`. Never print or commit the key. If it is not set, ask me to create one at https://lovelyrouter.com/app/keys. - Billing is prepaid: every call is charged to my balance, so test with a small request before running anything large. - Every docs page is plain Markdown; the full index is https://lovelyrouter.com/llms.txt. My task: <the genre, the world and the models to use>
How it works
Keep the state in your engine
The model remembers only what you send. Your engine keeps the world: where the player is, who they've met, what they carry, which flags are set, and a short summary of each finished chapter. That is the source of truth, not the model's memory.
Build each turn's prompt
A system prompt with the rules and the voice of the story, then the state, the chapter summaries, the last few turns in full, and the player's action. Ask for the next passage and a few choices at the end.
Pick the model and the sampling
A fast model for quick turns, a stronger one for big scenes. Turn thinking on for puzzles and plot turns where the model should plan first; leave it off for prose. Set
temperature,top_kandrepeat_penaltyfor the style you want.Update the state and continue
Read the passage, apply what changed to your state (or ask the model to list the changes in a fixed format your engine parses), and wait for the player's next move. When a chapter ends, replace its turns with a summary.
A turn in Python
import json
import os
from openai import OpenAI
client = OpenAI(base_url="https://lovelyrouter.com/v1", api_key=os.environ["LOVELY_API_KEY"])
state = {
"location": "the flooded archive beneath the cathedral",
"inventory": ["oil lamp", "rusted key"],
"met": {"Sister Ilse": "distrusts the player", "the Archivist": "dead, but still talking"},
"flags": ["heard the bells at midnight"],
}
chapters = ["Chapter 1: The player arrived in Vesk during the flood and was hired to find a missing ledger."]
reply = client.chat.completions.create(
model="qwen3.8-27b-heretic",
messages=[
{"role": "system", "content": "You narrate a gothic horror text adventure in the second person, present tense. "
"Stay inside the story. End every passage with three numbered choices."},
{"role": "user", "content": "STORY SO FAR:\n" + "\n".join(chapters) + "\n\nSTATE:\n" + json.dumps(state)},
*recent_turns, # the last few exchanges, in full
{"role": "user", "content": "I unlock the iron cabinet with the rusted key."},
],
max_tokens=900,
extra_body={
"top_k": 20,
"repeat_penalty": 1.1,
"chat_template_kwargs": {"enable_thinking": False},
},
)
print(reply.choices[0].message.content)top_k, min_p and repeat_penalty aren't OpenAI parameters, so the OpenAI SDK sends them in extra_body; in raw JSON they sit next to temperature. The hosted models answer the Anthropic Messages API too, so an engine written for Claude can switch models by changing the model field.
Chinese long-form fiction
dongfangshuo-27b-v2 is Qwen3.8-27B fine-tuned for Chinese fiction and roleplay, without the base model's refusals. Its authors recommend answering without thinking for fiction, which is its default, and the sampling defaults are tuned against repetition in long passages. Ask for about 2,000 to 4,000 Chinese characters per reply and continue chapter by chapter; a structured prompt (the scene, the characters and their state, the previous chapter, the plot steps, the details that must appear) gives it the most control.
curl https://lovelyrouter.com/v1/chat/completions \
-H "Authorization: Bearer $LOVELY_API_KEY" \
-H "content-type: application/json" \
-d '{
"model": "dongfangshuo-27b-v2",
"messages": [{"role": "user", "content": "场景:雨夜的江南客栈。人物:沈青(剑客,身负重伤)、苏婉(客栈老板娘,隐藏身份)。上一章:沈青逃出追杀。本章情节:苏婉为沈青疗伤,两人互相试探。要求:约三千字,第三人称。"}]
}'The models
| Model | Input → output | Price |
|---|---|---|
| Qwen3.8 27B Heretic (uncensored) qwen3.8-27b-heretic | text+image | $1.40 in · $21.90 out per 1M tokens |
| Huihui Qwen3.8 27B (abliterated) huihui-qwen3.8-27b-abliterated | text+image | $1.45 in · $20.60 out per 1M tokens |
| Qwen3.5 9B Defiant Fable (uncensored) qwen3.5-9b-defiant-fable | text+image | $0.90 in · $14.40 out per 1M tokens |
| Dongfangshuo 27B v2 dongfangshuo-27b-v2 | text+image | $1.50 in · $23.40 out per 1M tokens |
Live prices in USD, the same the API charges.
qwen3.8-27b-hereticand huihui-qwen3.8-27b-abliterated are two abliterations of Qwen3.8-27B: strong at following long instructions and keeping a world consistent. If one doesn't land a scene, try the other.
qwen3.5-9b-defiant-fableis a 9B fine-tune for creative writing and roleplay, quicker and cheaper per token: good for fast turns and side characters.
dongfangshuo-27b-v2writes Chinese long-form fiction and roleplay.
deepseek-v4.1-flash-abliteratedis the cheapest per token, with a context window of about a million tokens and no warm-up, for engines that send a lot of history.
The four hosted models take text and images and answer both the OpenAI Chat Completions and Anthropic Messages APIs. Mainstream Claude, GPT and Gemini models are on the same key for stories that don't need an uncensored model. Prices are live, per token.
Thinking and sampling
The hosted fiction models answer directly by default. Add "chat_template_kwargs": {"enable_thinking": true} to a request to let the model reason first; the reasoning comes back in message.reasoning_content (in streams, delta.reasoning_content), separate from the passage, so the player never sees it. Thinking costs output tokens and time, so turn it on for the turns that need a plan.
dongfangshuo-27b-v2: temperature 0.8, top_p 0.9, top_k 50, repeat_penalty 1.15. The three Qwen-based models: temperature 0.7, top_p 0.8, top_k 20. Send any of them to override.
Chat Completions also accepts top_k, min_p and repeat_penalty on these models. Raise repeat_penalty a little when long passages start to loop; lower temperature for rules-heavy scenes.
Set stream: true to show the passage as it is written, like a typewriter.
Engine patterns that work
Keep the last few turns in full and the rest as chapter summaries. Long stories stay inside the context window, and each request costs less.
Ask the model to end with a short, fixed-format list of what changed (items gained, flags set), parse it, and keep your own state. Don't trust the model to remember an inventory.
Narrate with a 27B model, voice minor characters or generate item descriptions with the 9B model, all with the same key.
Ask for numbered choices at the end of every passage and split them off for buttons.
The hosted models sleep after 2 minutes without requests; the first one after a quiet spell waits 10 to 30 seconds while a GPU wakes. Show a "the story continues…" state, and give your client a timeout of at least 3 minutes.
What still applies
These models follow the prompt without their base models' refusals, and Lovely Router adds no content filter. You are responsible for what your engine generates. Mature and adult fiction is allowed for adults (18+), and if your engine serves it to others, you check their age. Nothing sexual involving minors, ever, under the Acceptable Use Policy.
Frequently asked questions
How does the model remember the story?+
It doesn't, between requests. Your engine sends the state, the chapter summaries and the recent turns every time. The hosted models take 128K tokens, and deepseek-v4.1-flash-abliterated about a million.
Which model is best for Chinese fiction?+
dongfangshuo-27b-v2, fine-tuned for Chinese long-form fiction and roleplay. Ask for 2,000 to 4,000 characters a reply and give it a structured prompt.
Should I turn thinking on?+
For puzzles, mysteries and plot turns, yes: the model plans first, and the plan comes back separately in reasoning_content. For plain prose, leave it off; it is faster and costs fewer tokens.
Can I set top_k and repeat_penalty?+
Yes. Chat Completions accepts top_k, min_p and repeat_penalty on these models besides the OpenAI parameters. With the OpenAI SDK, put them in extra_body.
Will the model refuse dark scenes?+
The uncensored models don't refuse and Lovely Router doesn't filter. Mainstream models may soften or refuse some scenes; switch the model field for those turns.
Can I use the Anthropic API instead?+
Yes. The hosted models answer POST /v1/messages as well as Chat Completions, so engines built on the Anthropic SDK work by changing the model.
Can an AI agent build the engine?+
Yes. Copy the prompt on this page into Claude Code, Cursor or Codex: it reads the Markdown docs and writes the state handling and the model calls with your key.
For a character people talk to every day, see AI companion apps; to start, sign up and try a scene in the playground.