AI API
Call hosted AI models for text generation, chat, hints, narrative beats, and more, without managing your own inference stack.
For media generation, see the dedicated docs:
Migration notice:
RundotGameAPI.aiis deprecated. UseRundotGameAPI.textGeninstead; the interface is identical and both namespaces run the same model-selection cascade. Calls toRundotGameAPI.aistill work but log a one-time console warning.
Quick Start
import RundotGameAPI from '@series-inc/rundot-game-sdk/api'
const response = await RundotGameAPI.textGen.requestChatCompletionAsync({
modelClass: 'standard',
messages: [
{ role: 'user', content: 'Give me a tip for this puzzle.' },
],
})
console.log(response.choices[0].message.content)
console.log(response.model) // The exact model that actually ran.The completion resolves to an OpenAI-shaped AiChatCompletionData. The assistant text lives at response.choices[0].message.content (there is no top-level message field). See Completion Response Shape for the full object.
modelClass selects a stable model class ('quick', 'standard', or 'power') and lets the platform choose the exact model — this is the recommended pattern. Naming an exact model ID instead is an advanced, best-effort alternative; see Model Selection.
Model Selection
Pick one of three stable model classes and let the platform choose the exact model:
modelClass
Intended for
'quick'
Fast, inexpensive responses — hints, flavor text, simple lookups. This is the default when a request has neither model nor modelClass.
'standard'
General-purpose chat and gameplay logic.
'power'
The hardest reasoning and generation tasks.
Each class maps to a server-owned default model. The platform can rotate or retire the underlying models without breaking your game, so a class-only call is the recommended, most stable pattern.
Resolution cascade
Both model and modelClass are optional. The backend resolves the model that runs as follows:
model
modelClass
Result
valid
absent
exact model
valid
valid class
exact model (the class is only the fallback)
invalid
'standard'
Standard class default
absent
'power'
Power class default
invalid
absent
Quick class default
absent
absent
Quick class default
any
invalid class string
HTTP 400
"Valid" means the exact model ID exists in the platform registry, is enabled and callable, and is permitted by your game's allowedModels policy when one is set. An unknown, retired, disabled, or policy-denied exact model does not fail the request — it moves to the class fallback.
An explicit 'standard' or 'power' request never silently falls through to Quick: if that class has no eligible model, the call fails — AI_POLICY_DENIED when your policy permits nothing in the class, MODEL_CLASS_UNAVAILABLE (HTTP 503) when the class has no valid platform default.
Resolved-model reporting
The response tells you which model actually ran, after any fallback:
Non-streaming:
response.modelis the resolved model ID. It can differ from themodelyou sent.Streaming: the terminal
donechunk carries the same value asdone.model(optional — absent when the backend predates the field).
Advanced: exact model IDs
You can still request an exact model directly, optionally with a class as its fallback:
List the current exact model IDs at runtime:
Commonly available production models:
gpt-5gpt-5.6-lunaclaude-haiku-4-5claude-sonnet-4-6claude-opus-4-7deepseek/deepseek-chat
Tip: Call
getAvailableCompletionModels()at runtime to discover the current exact-ID list. PrefermodelClassover hard-coding an ID from it.
Chat Completion Parameters
Request fields:
modelClass
'quick' | 'standard' | 'power'
Stable model class. Recommended. Defaults to 'quick' when both selection fields are absent. See Model Selection.
model
string
Advanced, best-effort. Optional exact model identifier; wins while valid, otherwise falls back to modelClass or Quick. List options with getAvailableCompletionModels().
messages
AiMessage[]
Conversation messages in OpenAI chat format. Each content is a string or an array of content blocks.
maxTokens
number
Maximum tokens to generate.
maxCompletionTokens
number
Upper bound on tokens generated, including visible output and reasoning tokens. Distinct from maxTokens.
reasoningEffort
'none' | 'minimal' | 'low' | 'medium' | 'high'
Reasoning (thinking) effort for reasoning-capable models. See Reasoning Effort.
temperature
number
Sampling temperature (0.0 to 2.0).
topP
number
Nucleus sampling (0.0 to 1.0).
topK
number
Top-k sampling.
n
number
Number of completions to generate (1 to 10). Surfaces as multiple entries in choices.
stop
string | string[]
Up to 4 stop sequences.
presencePenalty
number
Presence penalty (-2.0 to 2.0).
frequencyPenalty
number
Frequency penalty (-2.0 to 2.0).
responseFormat
AiResponseFormat
Constrains output to text, JSON, or a JSON schema. See Structured Outputs.
toolChoice
'auto' | 'any' | 'none' | { type: 'tool'; name: string }
How the model selects among tools.
logitBias
Record<string, number>
Token logit biases.
seed
number
Random seed for reproducible outputs (beta).
user
string
Identifier representing your end user.
timeout
number
Timeout in seconds for the request.
logprobs
boolean
Return log probabilities of output tokens.
topLogprobs
number
Number of most likely tokens (0 to 5) to return at each position.
safetyIdentifier
string
Identifier for tracking safety-related requests.
headers
Record<string, any>
Headers sent with the request.
extraHeaders
Record<string, any>
Extra headers sent in the LLM API request.
tags
string[]
Custom tags for organizing completions.
apiKey
string
Deprecated. No longer used; stripped before the call. Will be removed in a future major version.
Reasoning Effort
Advanced, provider-specific option — see the stability note in Model Selection.
Set reasoningEffort to control how much internal reasoning ("thinking") a reasoning-capable model performs before answering:
Per-provider support:
OpenAI (gpt-5 family)
Passed through natively as reasoning_effort. Accepted values differ per model version — see the warning above.
Gemini
Translated to a thinking level (Gemini 3.x) or a thinking-token budget (Gemini 2.5). Gemini 3.x cannot disable thinking, so 'none' resolves to the lowest level that family accepts. Older Gemini models (2.0 and earlier) reject the field with HTTP 400.
Anthropic (claude-*)
Forwarded. Whether the model honours it varies: claude-haiku-4-5 does, claude-sonnet-5 accepted every level and produced ~0 reasoning tokens regardless (measured 2026-09-20). Check usage.reasoning_tokens rather than assuming a 200 means the setting applied.
DeepSeek
Forwarded. deepseek-r1 rejects 'none' — its reasoning is mandatory.
Notes:
'none'requests minimal/disabled reasoning where the model allows it. Models that cannot fully disable thinking (gemini-2.5-pro, Gemini 3.x) clamp to their minimum instead.Setting an effort can turn reasoning ON.
claude-haiku-4-5ships with thinking off and produces 0 reasoning tokens by default; asking for'low'produced 516–897 reasoning tokens across samples. On a model that does not reason by default,'low'means some where there was none.Reasoning tokens come out of your
maxTokensbudget. If the model spends the whole budget thinking you getfinish_reason: 'length'with empty content — and you are still billed for the reasoning. Lower the effort rather than raisingmaxTokens.Billing: reasoning tokens are billed as output tokens, so higher efforts increase completion-token usage and cost. When the model reasons,
usage.reasoning_tokensin the response reports how many completion tokens were spent on reasoning.For Gemini models, thinking tokens count against
maxOutputTokens, so when an effort is set the server raises the effective output ceiling (to at least 16384 forlow/medium, 32768 forhigh) unless you explicitly requested more viamaxTokens/maxCompletionTokens.Older backends that predate this field silently ignore it.
Recommended model:
gpt-5.6-lunais the recommended general-purpose model — fast (~200 output tokens/sec) and inexpensive, withreasoningEffortletting you trade speed for intelligence on the same model. Independent benchmarks place its highest-effort intelligence near GPT-5.4's top tier at a fraction of the effective cost.
Multimodal Messages (Image Input)
Pass an array of content blocks instead of a plain string when you need to send images alongside text. Use a vision-capable model; check getAvailableCompletionModels() and your provider's docs for current support.
By URL or data URI:
By raw base64 bytes:
Content block reference:
text
text: string
Plain text segment.
text
cacheControl?: { type: 'ephemeral' }
Optional Anthropic cache breakpoint. Valid on any text block, including ones inside messages[].content, so you can cache mid-conversation history alongside the system prefix. See System Prompt & Prompt Caching.
image_url
image_url.url: string
http(s):// URL or data:image/<type>;base64,… URI.
image_url
image_url.detail?: 'low' | 'high' | 'auto'
Optional vision-detail hint; respected by models that support it.
image
image.format: 'jpeg' | 'png' | 'gif' | 'webp'
Required when sending raw bytes.
image
image.data: string
Base64 bytes only; do not include the data: prefix.
tool_use
id: string, name: string, input: object
Assistant tool invocation. Replay a prior assistant tool turn back to the model. See Tool Calling.
tool_result
toolUseId: string, content: string | AiContentBlock[], isError?: boolean
Feed tool execution output back to the model on the next turn.
image_url is sent directly to OpenAI/DeepSeek-compatible providers; for Anthropic models the proxy fetches the URL and base64-encodes the bytes before forwarding. Use image (raw base64) when the source isn't a public URL.
Completion Response Shape
requestChatCompletionAsync resolves to an AiChatCompletionData object shaped like an OpenAI chat completion:
choices[].message.content
string
The assistant's text. Read text from here, not a top-level message.
choices[].message.reasoning_content
string
Optional. Present only for reasoning models (e.g. deepseek-r1): the model's thinking, kept separate from the visible content. Absent on models that don't surface reasoning.
choices[].message.toolCalls
ToolUse[]
Tool invocations, present only when the model called a tool this turn.
choices[].finish_reason
string
Why generation stopped (e.g. stop, length, tool_calls).
usage
object
Token totals; reasoning_tokens, cache_read_tokens, and cache_write_tokens appear when applicable.
cost
object
Optional prompt_cost / completion_cost for the call.
id, created, object, ullm_id
string / number
Call metadata.
model
string
The resolved model ID that actually ran, after any class fallback — can differ from the requested model.
System Prompt & Prompt Caching
system is a structured system prompt that mirrors Anthropic's system shape:
string: passed verbatim as the system instruction.Array<AiTextContent>: each block becomes a system text block.
It coexists with any role: 'system' entries in messages; both are concatenated.
To control caching yourself, set cacheControl: { type: 'ephemeral' } on the text content block(s) you want as cache breakpoints. The marker is valid on any text block: a system array entry, or a text block inside a user/assistant message in messages[].content (to cache a slice of mid-conversation history alongside the system prefix). Any explicit marker anywhere (on system or on a message block) disables the proxy's auto-cache, so your strategy is left intact. The field is ignored by non-Anthropic providers.
Structured Outputs
responseFormat constrains the model's output. The proxy enforces JSON schemas on every provider, so the same request works across OpenAI, DeepSeek, Anthropic, and Gemini.
type
Behavior
text
Free-form text (default).
json_object
Model is asked to emit valid JSON. No schema enforcement.
json_schema
Model is asked to emit JSON matching schema. Enforced server-side: native Structured Outputs on OpenAI/DeepSeek, native responseSchema on Gemini, and schema-appended-to-system plus server-side validation on Anthropic.
For json_schema, strict (default false) maps to OpenAI's strict flag and is otherwise informational.
OpenAI-native nested shape (back-compat)
Besides the flat { type: 'json_schema', schema, strict } form above, responseFormat also accepts the nested OpenAI-native shape so you can paste an OpenAI response_format.json_schema block straight in. On the OpenAI path it is passed through unchanged.
json_schema.name
string (optional)
Schema name, OpenAI metadata.
json_schema.description
string (optional)
Schema description, OpenAI metadata.
json_schema.schema
object (optional)
The JSON Schema the model must match.
json_schema.strict
boolean | null (optional)
Maps to OpenAI's strict flag.
Use the flat form (schema / strict at the top level) for new code; the nested form exists for copy-paste compatibility with existing OpenAI request bodies.
Tool Calling
Pass tools to let the model call functions you define. Each tool has a name, a description, and an inputSchema (JSON Schema describing its arguments). Use toolChoice to control selection: 'auto', 'any', 'none', or { type: 'tool', name } to force a specific tool.
Tool invocations come back on the assistant message as choices[].message.toolCalls: ToolUse[] (each { id, name, input }). To continue the conversation, echo the assistant's tool call back as a tool_use content block, then supply the result as a tool_result block:
Agent loops
TextGen is the low-level transport: you send messages, you get a completion back, and everything around that — running the tool the model asked for, persisting the conversation, recovering after a refresh, retrying a dropped stream, summarising a long history, pausing for the player's approval — is yours to build.
If you want that machinery instead of writing it, install the opt-in agent package:
Three things to know before you reach for it:
It is a separate package. Importing TextGen does not bundle the agent runtime, and the agent package does not import the SDK at runtime — it adapts
textGenandappStoragestructurally, so there is no version to keep in sync.Open mode is required. A model-authored tool loop cannot run against a templated game; the transport fails at construction with
TEMPLATED_MODE_UNSUPPORTED. Seemode: open vs templated.Use compare-and-swap storage. It prevents two tabs, devices, or workers from owning the same session at the same time. Use
singleWriter: trueonly when your application controls one writer for the full storage lifetime.appStorage is the hot tier. Sessions share the game's 128-key bucket.
createVenusSessionArchive(rundot.files)parks old journals in the Files API when the bucket is under key pressure. See Park old sessions outside appStorage.TextGen is not going anywhere. If you just want a completion, keep calling
requestChatCompletionAsyncdirectly. The agent package is additive.
Read Agent Runtime (BETA) for complete setup, tools, durable questions, approvals, reload recovery, React events, and multiple-tab conflict handling. The npm package also contains the API reference and release information: https://www.npmjs.com/package/@series-inc/rundot-agent.
Streaming
requestChatCompletionStreamAsync is the streaming sibling of requestChatCompletionAsync. It returns an AsyncIterable<AiChatCompletionStreamChunk> consumed with for await. Cancel by breaking out of the loop, calling return() on the iterator, or aborting options.signal.
Each chunk is one of four variants:
type
Fields
Meaning
delta
text: string
Incremental assistant text. Concatenate text across all delta chunks to rebuild the message.
reasoning
text: string
Optional, reasoning models only (e.g. deepseek-r1). Incremental reasoning / chain-of-thought text on a distinct channel. Concatenate text across reasoning chunks (like delta) to rebuild the trace; content-only consumers can ignore it.
tool_call_chunk
index: number, id?, name?, argumentsDelta?
Partial tool-call metadata. Buffer by index; the id and name arrive once, with argumentsDelta fragments following.
done
finishReason: string, usage, model?: string
Terminal chunk. Carries the stop reason, aggregate token totals (same shape as the non-streaming usage), and model — the resolved model ID that ran (absent on backends that predate the field). Always last.
Errors are out of band: the iterable throws.
On the playground HTTP path (HttpAiApi), the throw is a RundotApiError:
code
JSON/SSE machine code when the server sent one (CREDITS_EXHAUSTED, …). The HTTP error field is copied as-is and is sometimes prose (Too Many Requests). A stream that ends without done or error, or a 200 with no body, uses NETWORK_ERROR.
status
HTTP status when the failure had one (non-OK POST, or SSE statusCode). 0 when there was no HTTP status.
The hosted RPC path still throws a plain Error except for credits exhaustion. @series-inc/rundot-agent classifies retries from code and status. It does not parse the message string. An unrecognized code such as STREAM_FAILED does not override status.
To abort mid-stream, pass an AbortSignal via the second argument:
Templated Prompts
Some games run in templated textGen mode: instead of the game authoring model, system, and messages in code, the game's creator declares prompts server-side (in rundot/textGen.config.json) and the game invokes them by id. The server owns the model choice and the full prompt text; your game contributes only the player's turn.
Use requestPromptCompletionAsync (or its streaming sibling) with a PromptCompletionRequest:
promptId
string
Id of a server-authored prompt declared in the game's rundot/textGen.config.json.
input
string (optional)
The player's turn. It is isolation-wrapped server-side before reaching the model. Text-only in v1.
Streaming works exactly like Streaming — same chunk types, same cancellation:
Notes:
No client-supplied variables.
{{var}}slots in a server-authored prompt resolve entirely server-side from a trusted value bag — the call carries no variables. Client-named variables (e.g. a player-chosen quiz topic) are deferred to a future version; in v1, user content reaches the model only throughinput.inputis text-only in v1. Sending a non-stringinputfails with HTTP 400; multimodal templated input is deferred.Open-mode calls are rejected on templated games. Against a templated game,
requestChatCompletionAsync(and its streaming sibling) fails with HTTP 403 and error codeAI_POLICY_DENIED. The error surfaces to your game as a thrownErrorwhose message is the code string.
Configuration
rundot/textGen.config.json
textGen is default-bounded: your game can call it without any config file, but undeclared usage runs under default platform safety caps (~$500/game/day and ~$10/user/day). Creating rundot/textGen.config.json — which rundot deploy does automatically the first time it detects the service in your bundle — replaces those defaults with your own policy: the auto-created permissive {} removes the caps entirely, or you can author allowlists and caps to tighten instead. An explicit { "disabled": true } turns the service off.
Because the generic rundot.ai chat API shares this same backend, this one file governs both your game's textGen calls and any generic chat API usage.
Place your config at rundot/textGen.config.json. The file contains the settings directly (no wrapping key — the filename implies it):
Commit
rundot/to your repo; it's project config, not a build artifact, and it's env-agnostic.game.config.prod.jsonis a separate file for local CLI metadata; this config does not go there.
Common fields:
disabled
boolean
Turns the service off; every call fails with AI_POLICY_DENIED.
allowedModels
string[]
Advanced. Allowlist of permitted exact model identifiers. When set, selection (exact and class fallback) is confined to listed models; a list of only retired/invalid models removes the automatic retirement fallback and calls fail with AI_POLICY_DENIED. Omit to allow any.
dailyCreditCap
number
Approximate per-game daily credit ceiling.
perUserDailyCreditCap
number
Approximate per-user daily credit ceiling.
For textGen, allowedModels entries are exact model IDs matched during model selection — against a requested model and against class-fallback candidates alike (see Model Selection).
The two credit caps are approximate safety ceilings, not exact meters: the server reserves budget from a cost estimate before each call and reconciles the real cost afterward, so the effective ceiling can drift slightly around the configured value.
Policy resolves from your game's published (public) release — the same config your live players run against.
mode: open vs templated
mode selects how much of the request the client controls:
"open"(default) — the client sends a full chat request and the server shapes and clamps it with the open-mode fields below."templated"— only server-authored prompts run. The client cannot send rawsystem,messages, ortools; it may only invoke apromptsentry by id viarequestPromptCompletionAsync({ promptId, input }). See Templated Prompts for the client-side API.
Open-mode shaping fields
These apply in mode: "open". Every field is optional; unset means "don't shape that aspect".
systemPrompt
string
—
Server-pinned system prefix. Supports {{var}}.
systemPromptSuffix
string
—
Footer restated as a system turn after the user region (instruction framing). Supports {{var}}.
systemPromptMode
'prepend' | 'replace'
'replace'
How systemPrompt combines with the client's system content. 'replace' drops the client system; 'prepend' keeps it after the pinned prefix.
allowClientSystemPrompt
boolean
true
When false, client-supplied system content is stripped.
isolateUserInput
boolean
false
Wraps client message content in a per-request nonce-delimited <user_input_…> data block (spotlighting against prompt injection).
responseFormat
AiResponseFormat
—
Server-pinned output format.
allowClientResponseFormat
boolean
true
When false, the client's responseFormat is replaced by the pinned one.
allowClientTools
boolean
true
When false, client-supplied tools are stripped.
allowedTools
string[]
—
When set, only these tool names survive.
maxInputTokens
number
—
Reject requests estimated above this many input tokens.
maxMessages
number
—
Max messages per request.
maxMessageLength
number
—
Max characters per single message.
pinTemperature
number
—
Override the client's temperature.
pinTopP
number
—
Override the client's topP.
maxCompletions
number
—
Clamp n (number of completions).
maxOutputTokens
number
—
Clamp maxTokens / maxCompletionTokens.
Templated prompts registry (mode: "templated")
prompts is a Record<promptId, template> of server-authored prompts the client invokes by id. Each template:
system
string (required)
System instruction body. Supports {{var}}.
systemSuffix
string
Footer restated after the user region (instruction framing). Supports {{var}}.
model
string
Pin the model for this prompt (ignores any client-sent model; falls back to the first allowedModels entry when unset).
responseFormat
AiResponseFormat
Pin the output structure (JSON schema enforced server-side).
maxOutputTokens
number
Per-prompt output ceiling.
temperature
number
Pin the sampling temperature for this prompt.
Trusted variables
{{var}} placeholders in systemPrompt / systemPromptSuffix and in prompt templates resolve server-side from a trusted value bag — never from client input. Identity variables are always available:
{{user}}— the player's user id{{displayName}}— display name{{locale}}— locale (defaulten){{date}}— current date (YYYY-MM-DD)
Simulation state is opt-in via {{sim.<path>}} and requires an allowlist under variables.sim:
Only the listed dot-paths into the player's server-authoritative simulation state resolve; anything else is unavailable. Sim reads are best-effort — a failed or missing read renders the variable empty and never blocks the call. Client-supplied variables are not accepted in v1.
Examples
Open mode (shape the client's request, with a templated + framed system and trusted variables):
Templated mode (server-authored prompts only; the client calls requestPromptCompletionAsync({ promptId, input }) and cannot send raw prompts — template variables resolve server-side from identity / sim.*, and client-supplied variables are not accepted in v1):
API Reference
textGen.requestChatCompletionAsync(request)
Promise<AiChatCompletionData>
Generate a text completion. Read the text at choices[0].message.content; see Completion Response Shape.
textGen.requestChatCompletionStreamAsync(request, options?)
AsyncIterable<AiChatCompletionStreamChunk>
Stream a completion chunk by chunk via for await. See Streaming.
textGen.requestPromptCompletionAsync(request)
Promise<AiChatCompletionData>
Invoke a server-authored prompt by id (templated mode). See Templated Prompts.
textGen.requestPromptCompletionStreamAsync(request, options?)
AsyncIterable<AiChatCompletionStreamChunk>
Streaming sibling of requestPromptCompletionAsync. See Templated Prompts.
textGen.getAvailableCompletionModels()
Promise<string[]>
List available models.
Best Practices
Provide concise prompts; include relevant game context to reduce token usage.
Gracefully degrade when the API is unreachable; AI should add to, not block, core gameplay.
Respect content policies: filter user input and sanitize AI responses before showing them in-game.
Cache model selection in your state so you can update prompts on the fly.
Limits
textGen.requestChatCompletionAsync,textGen.requestChatCompletionStreamAsync,textGen.requestPromptCompletionAsync, andtextGen.requestPromptCompletionStreamAsyncare subject to per-creator rate-limit tiers; see Rate Limits.
Last updated