Creating an agent
Updated
An agent starts as an AI Agent entity with one component it cannot do without: the AI Model, which decides what answers for it. You then give it a personality with an AI Persona and a way to be talked to, such as a dialog panel. This page builds a working agent in the editor, documents the AI Model and AI Persona, and shows how a script creates and adjusts agents at run time.
Before you start
Section titled “Before you start”- An agent is an entity with components. If entities, components and
getComponentare new to you, read Components first. - Scripts hear an agent through a Signal Emitter on it, and compare events with
EventType. See Scripting. - The AI agents overview explains turns, prompts and the conversation.
Building an agent
Section titled “Building an agent”-
Add an AI Agent. Create an AI Agent entity (
ent-ai-agent) and place it where visitors will meet it, for example beside a statue. It comes with a Transform, an AI Model using the application’s default model, and an AI Flow. -
Choose the model. Leave the AI Model on its default, or pick another provider and model id in its inspector. See AI Model.
-
Give it a persona. Add an AI Persona. Set
nameto what visitors should call it, andinstructionsto how it should behave:You are the friendly guide of this gallery. Answer in two or three sentences. -
Give it a dialog. Add an AI Dialog so visitors can type to it. It shows at all times by default; see Dialog and captions to open it only when the player walks up.
-
Let scripts hear it. Add a Signal Emitter with a
signalsuch asguide:ai. -
Try it. Enter play, walk up to the agent and type a message. The reply streams into the panel. To make it speak, add AI Text To Speech and turn on the dialog’s
autoSpeak(see Speech).
An AI Agent entity also accepts a Signal Emitter, an Enable On Signal, a Don’t Unload, an Animation Timeline and an Animation Graph, and a Persistable for lasting memory (see Translation and memory).
AI Model
Section titled “AI Model”The AI Model (comp-ai-model, AIModelComponent) names the model that answers for the agent. Every agent has exactly one, and it cannot be removed. It is also where a script finds the agent’s conversation, and the component that sends every AIAgent::… event to the agent’s Signal Emitters.
| Prop | Type | Default | Description |
|---|---|---|---|
provider |
string | 'Remote' |
Which service answers. 'Remote' is the model service the application provides; the inspector lists the other providers. |
model |
string | the provider’s default model | The model id at that provider. It must not be empty. Any id the provider knows is accepted, including your own fine-tuned ones. |
reasoning |
'none', 'minimal', 'low', 'medium', 'high', 'xhigh' or 'max' |
the model’s default | How much the model reasons before answering. More effort gives more careful answers more slowly. Absent for models that do not reason. |
- Reasoning effort. For a model the built-in catalog knows, an omitted effort takes the model’s default, and setting an effort the model does not support with
setPropsthrows. Check the model’s supported efforts first (see the last example on this page). - Changes take effect from the next turn.
| Member | What it does |
|---|---|
get |
The agent’s shared conversation, or undefined in the editor, while the agent is inactive or its AI Model is off, or when the application offers no AI. See Scripting AI. |
AIModel |
Static. Suggested model definitions by provider: { id, name?, reasoning. Suggestions, not an allow-list. |
AIModel |
Static. The same model ids, as plain strings, by provider. |
AIModel |
Static. The catalog definition of that exact model, or undefined when the catalog does not know it. |
The private Context methods of the AI Model (listContexts, createContext, setContext, getContext, clearContext, getGlobalContext) are covered in Translation and memory.
AI Persona
Section titled “AI Persona”The AI Persona (comp-ai-persona, AIPersonaComponent) gives the agent a name and standing instructions. It is optional: without it, or while it is switched off, the agent still answers, it simply sends no instructions and takes its display name from the entity.
| Prop | Type | Default | Description |
|---|---|---|---|
name |
string | '' |
The display name, shown in the dialog header and in agent lists. At most 50 characters. Empty uses the entity’s name. |
instructions |
string | '' |
Standing instructions sent to the model with every turn: who the agent is, how it speaks, what it must not do. At most 4000 characters. |
| Member | What it does |
|---|---|
AIPersona |
Static. The name the runtime shows for that agent: its enabled Persona’s name, else the entity’s name, else Agent. |
Editing the persona, or switching it on or off, applies from the next turn. A line of text under the name that should never reach the model belongs in the dialog’s subtitle instead.
AI Flow
Section titled “AI Flow”The AI Flow (comp-ai-flow) is an editor view that draws how the agent’s AI components connect. A new agent has one. It does nothing in play, and removing or disabling it changes nothing about how the agent answers.
Examples
Section titled “Examples”1. Read an agent’s setup
Section titled “1. Read an agent’s setup”#pragma lifecycle(startup)
function startup() { const guide = findEntity('Guide'); if (!guide) return log('No agent named Guide.');
const model = guide.getComponent(AIModelComponent); log('Name shown to visitors:', AIPersonaComponent.displayName(guide)); log('Answered by:', model.props.provider, model.props.model);
const persona = guide.getComponent(AIPersonaComponent); if (!persona || !persona.enabled) log('This agent has no active persona.');}A script logs an AI agent's setup at startup, its display name and the provider and model that would answer it, by reading the AI Model and AI Persona props.
Read the agent's setup in the log. No conversation is started, so the example needs no AI service.
AIPersonaComponent.displayName(guide)resolves the name exactly as the dialog does, so you never have to repeat the fallback rules.model.propsholds the AI Model’s current props. Reading props never contacts a model.getComponentreturnsundefinedfor a component the entity lacks, andenabledtells whether a present one is switched on.
Snippets
Section titled “Snippets”1. Change the persona with the game
Section titled “1. Change the persona with the game”#pragma lifecycle(startup)
// Scene: an AI Agent named "Guide" with an AI Persona.// Other scripts emit 'museum/closing' a few minutes before closing time.const DAY = 'You are the friendly guide of this gallery. Answer in two or three sentences.';const CLOSING = DAY + ' The museum closes in five minutes: mention it kindly and point to the exit.';
function startup() { const persona = findEntity('Guide').getComponent(AIPersonaComponent); persona.setProps({ instructions: DAY });
on('museum/closing', () => { persona.setProps({ instructions: CLOSING }); log('The guide now mentions closing time.'); });}- The instructions are plain strings, so you can build them from constants and game state.
setPropschanges only the props you pass. The new instructions apply from the next turn; a reply being written finishes with the old ones.
2. Create an agent from a script
Section titled “2. Create an agent from a script”#pragma lifecycle(startup)
// Scene: nothing special. The script builds a helper agent in front of the spawn point.async function startup() { const helper = new AIAgentEntity(); helper.name = 'Helper'; helper.position.set(0, 1.6, -2);
helper.addComponent(new AIPersonaComponent({ name: 'Helper', instructions: 'You help new players learn the controls. Keep answers under 40 words.', })); helper.addComponent(new AIDialogComponent({ presentation: 'always', billboard: true })); helper.addComponent(new SignalEmitterComponent({ signal: 'helper:ai' })); scene.add(helper);
on('helper:ai', (event) => { if (event.eventType === EventType.AIAgent.Response) log('Helper:', event.data.text); });}new AIAgentEntity()already holds its AI Model, so the script only adds the capabilities it wants.- Each component takes its props in its constructor. The Signal Emitter’s
signalis the name the listener uses. scene.add(helper)puts the agent in the current scene; until then it is not live.
3. Pick a reasoning effort safely
Section titled “3. Pick a reasoning effort safely”#pragma lifecycle(startup)
// Scene: an AI Agent named "Guide". Players emit 'guide/detail' with true or false// from a settings switch to choose quick or detailed answers.let model;
function setEffort(effort) { const { provider, model: id } = model.props; const definition = AIModelComponent.getModelDefinition(provider, id); if (definition && !definition.reasoningEfforts.includes(effort)) { log(`${id} does not support "${effort}" reasoning; keeping its default.`); return; } model.setProps({ reasoningEffort: effort });}
function startup() { model = findEntity('Guide').getComponent(AIModelComponent); on('guide/detail', (detailed) => setEffort(detailed ? 'medium' : 'low'));}getModelDefinitionreads the static catalog. It never loads a model or starts a conversation.- Checking
reasoningEffortsfirst avoids the errorsetPropsthrows for an effort the model does not support. - A model the catalog does not know returns
undefined; the script then letssetPropsdecide.
Common mistakes
Section titled “Common mistakes”- Using the conversation in the editor.
getConversation()returnsundefinedwhile you edit. Check for it before callingsend. - Forgetting the Signal Emitter. Without one on the agent, no
AIAgent::…event reaches any script. Add it to the agent itself, not to another entity. - Putting display text in the instructions. Instructions go to the model on every turn. A tagline for the panel belongs in the dialog’s
subtitle. - Very long instructions. Instructions past 4000 characters make the agent report an error. Move task-specific detail into a Skill, which the model loads only when it needs it.
- Setting an unsupported reasoning effort.
setProps({ reasoningEffort })throws for an effort the model does not offer. CheckgetModelDefinitionfirst.